1 package org.apache.lucene.search.spans;
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache License, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import org.apache.lucene.util.ToStringUtils;
21 import java.io.IOException;
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.Iterator;
28 * Only return those matches that have a specific payload at
31 * Do not use this with an SpanQuery that contains a {@link org.apache.lucene.search.spans.SpanNearQuery}. Instead, use
32 * {@link SpanNearPayloadCheckQuery} since it properly handles the fact that payloads
33 * aren't ordered by {@link org.apache.lucene.search.spans.SpanNearQuery}.
36 public class SpanPayloadCheckQuery extends SpanPositionCheckQuery{
37 protected final Collection<byte[]> payloadToMatch;
41 * @param match The underlying {@link org.apache.lucene.search.spans.SpanQuery} to check
42 * @param payloadToMatch The {@link java.util.Collection} of payloads to match
44 public SpanPayloadCheckQuery(SpanQuery match, Collection<byte[]> payloadToMatch) {
46 if (match instanceof SpanNearQuery){
47 throw new IllegalArgumentException("SpanNearQuery not allowed");
49 this.payloadToMatch = payloadToMatch;
53 protected AcceptStatus acceptPosition(Spans spans) throws IOException {
54 boolean result = spans.isPayloadAvailable();
56 Collection<byte[]> candidate = spans.getPayload();
57 if (candidate.size() == payloadToMatch.size()){
58 //TODO: check the byte arrays are the same
59 Iterator<byte[]> toMatchIter = payloadToMatch.iterator();
60 //check each of the byte arrays, in order
61 //hmm, can't rely on order here
62 for (byte[] candBytes : candidate) {
63 //if one is a mismatch, then return false
64 if (Arrays.equals(candBytes, toMatchIter.next()) == false){
65 return AcceptStatus.NO;
68 //we've verified all the bytes
69 return AcceptStatus.YES;
71 return AcceptStatus.NO;
74 return AcceptStatus.YES;
78 public String toString(String field) {
79 StringBuilder buffer = new StringBuilder();
80 buffer.append("spanPayCheck(");
81 buffer.append(match.toString(field));
82 buffer.append(", payloadRef: ");
83 for (byte[] bytes : payloadToMatch) {
84 ToStringUtils.byteArray(buffer, bytes);
88 buffer.append(ToStringUtils.boost(getBoost()));
89 return buffer.toString();
93 public Object clone() {
94 SpanPayloadCheckQuery result = new SpanPayloadCheckQuery((SpanQuery) match.clone(), payloadToMatch);
95 result.setBoost(getBoost());
100 public boolean equals(Object o) {
101 if (this == o) return true;
102 if (!(o instanceof SpanPayloadCheckQuery)) return false;
104 SpanPayloadCheckQuery other = (SpanPayloadCheckQuery)o;
105 return this.payloadToMatch.equals(other.payloadToMatch)
106 && this.match.equals(other.match)
107 && this.getBoost() == other.getBoost();
111 public int hashCode() {
112 int h = match.hashCode();
113 h ^= (h << 8) | (h >>> 25); // reversible
114 //TODO: is this right?
115 h ^= payloadToMatch.hashCode();
116 h ^= Float.floatToRawIntBits(getBoost()) ;