LUCENE-7381: Fix equals relation in RangeFieldQuery. Fix relation logic in BaseRangeFieldQueryTestCase.

This commit is contained in:
Nicholas Knize 2016-07-22 10:59:48 -05:00
parent 7a4f800388
commit 1a94c25a04
2 changed files with 35 additions and 9 deletions

View File

@ -124,8 +124,9 @@ abstract class RangeFieldQuery extends Query {
@Override @Override
public void visit(int docID, byte[] leaf) throws IOException { public void visit(int docID, byte[] leaf) throws IOException {
// add the document iff: // add the document iff:
if (// target is within cell and queryType is INTERSECTS or CONTAINS: if (Arrays.equals(ranges, leaf)
(comparator.isWithin(leaf) && queryType != QueryType.WITHIN) // target is within cell and queryType is INTERSECTS or CONTAINS:
|| (comparator.isWithin(leaf) && queryType != QueryType.WITHIN)
// target contains cell and queryType is INTERSECTS or WITHIN: // target contains cell and queryType is INTERSECTS or WITHIN:
|| (comparator.contains(leaf) && queryType != QueryType.CONTAINS) || (comparator.contains(leaf) && queryType != QueryType.CONTAINS)
// target is not disjoint (crosses) and queryType is INTERSECTS // target is not disjoint (crosses) and queryType is INTERSECTS
@ -139,12 +140,12 @@ abstract class RangeFieldQuery extends Query {
// compute range relation for BKD traversal // compute range relation for BKD traversal
if (comparator.isDisjoint(node)) { if (comparator.isDisjoint(node)) {
return Relation.CELL_OUTSIDE_QUERY; return Relation.CELL_OUTSIDE_QUERY;
} else if (comparator.contains(node)) {
// target contains cell; add iff queryType is not a CONTAINS query:
return (queryType == QueryType.CONTAINS) ? Relation.CELL_OUTSIDE_QUERY : Relation.CELL_INSIDE_QUERY;
} else if (comparator.isWithin(node)) { } else if (comparator.isWithin(node)) {
// target within cell; continue traversing: // target within cell; continue traversing:
return Relation.CELL_CROSSES_QUERY; return Relation.CELL_CROSSES_QUERY;
} else if (comparator.contains(node)) {
// target contains cell; add iff queryType is not a CONTAINS query:
return (queryType == QueryType.CONTAINS) ? Relation.CELL_OUTSIDE_QUERY : Relation.CELL_INSIDE_QUERY;
} }
// target intersects cell; continue traversing: // target intersects cell; continue traversing:
return Relation.CELL_CROSSES_QUERY; return Relation.CELL_CROSSES_QUERY;
@ -170,8 +171,9 @@ abstract class RangeFieldQuery extends Query {
if (values.getDocCount(field) == reader.maxDoc()) { if (values.getDocCount(field) == reader.maxDoc()) {
// if query crosses, docs need to be further scrutinized // if query crosses, docs need to be further scrutinized
byte[] range = getInternalRange(values.getMinPackedValue(field), values.getMaxPackedValue(field)); byte[] range = getInternalRange(values.getMinPackedValue(field), values.getMaxPackedValue(field));
// if the internal node is not contained by the query, all docs do not match // if the internal node is not equal and not contained by the query, all docs do not match
if (((comparator.contains(range) && queryType == QueryType.CONTAINS)) == false) { if ((!Arrays.equals(ranges, range)
&& (comparator.contains(range) && queryType != QueryType.CONTAINS)) == false) {
allDocsMatch = false; allDocsMatch = false;
} }
} else { } else {

View File

@ -17,6 +17,7 @@
package org.apache.lucene.search; package org.apache.lucene.search;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
@ -262,10 +263,11 @@ public abstract class BaseRangeFieldQueryTestCase extends LuceneTestCase {
if (hits.get(docID) != expected) { if (hits.get(docID) != expected) {
StringBuilder b = new StringBuilder(); StringBuilder b = new StringBuilder();
b.append("FAIL (iter " + iter + "): ");
if (expected == true) { if (expected == true) {
b.append("FAILS: id=" + id + (boxes[id].length > 1 ? " (MultiValue) " : " ") + "should match but did not\n"); b.append("id=" + id + (boxes[id].length > 1 ? " (MultiValue) " : " ") + "should match but did not\n");
} else { } else {
b.append("FAIL: id=" + id + " should not match but did\n"); b.append("id=" + id + " should not match but did\n");
} }
b.append(" queryBox=" + queryBox + "\n"); b.append(" queryBox=" + queryBox + "\n");
b.append(" box" + ((boxes[id].length > 1) ? "es=" : "=" ) + boxes[id][0]); b.append(" box" + ((boxes[id].length > 1) ? "es=" : "=" ) + boxes[id][0]);
@ -292,6 +294,9 @@ public abstract class BaseRangeFieldQueryTestCase extends LuceneTestCase {
} }
protected boolean expectedBBoxQueryResult(Box queryBox, Box box, Box.QueryType queryType) { protected boolean expectedBBoxQueryResult(Box queryBox, Box box, Box.QueryType queryType) {
if (box.equals(queryBox)) {
return true;
}
Box.QueryType relation = box.relate(queryBox); Box.QueryType relation = box.relate(queryBox);
if (queryType == Box.QueryType.INTERSECTS) { if (queryType == Box.QueryType.INTERSECTS) {
return relation != null; return relation != null;
@ -345,6 +350,25 @@ public abstract class BaseRangeFieldQueryTestCase extends LuceneTestCase {
} }
} }
@Override
public boolean equals(Object o) {
return o != null
&& getClass() == o.getClass()
&& equalTo(getClass().cast(o));
}
private boolean equalTo(Box o) {
return Arrays.equals(min, o.min)
&& Arrays.equals(max, o.max);
}
@Override
public int hashCode() {
int result = Arrays.hashCode(min);
result = 31 * result + Arrays.hashCode(max);
return result;
}
QueryType relate(Box other) { QueryType relate(Box other) {
// check disjoint // check disjoint
for (int d=0; d<this.min.length; ++d) { for (int d=0; d<this.min.length; ++d) {