Skip to content

Commit

Permalink
LUCENE-9606: Wrap boolean queries generated by shape fields with a Co…
Browse files Browse the repository at this point in the history
…nstant score query (apache#2093)
  • Loading branch information
iverase authored Nov 25, 2020
1 parent c71f119 commit b63c37d
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lucene/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ Bug Fixes

* LUCENE-9595: Make Component2D#withinPoint implementations consistent with ShapeQuery logic.
(Ignacio Vera)

* LUCENE-9606: Wrap boolean queries generated by shape fields with a Constant score query. (Ignacio Vera)

Other
---------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.lucene.index.PointValues; // javadoc
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.ConstantScoreQuery;
import org.apache.lucene.search.Query;

import static org.apache.lucene.geo.GeoEncodingUtils.encodeLatitude;
Expand Down Expand Up @@ -170,7 +171,7 @@ private static Query makeContainsGeometryQuery(String field, LatLonGeometry... l
builder.add(new LatLonShapeQuery(field, QueryRelation.CONTAINS, geometry), BooleanClause.Occur.MUST);
}
}
return builder.build();
return new ConstantScoreQuery(builder.build());
}

}
3 changes: 2 additions & 1 deletion lucene/core/src/java/org/apache/lucene/document/XYShape.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.lucene.geo.XYPolygon;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.ConstantScoreQuery;
import org.apache.lucene.search.Query;

import static org.apache.lucene.geo.XYEncodingUtils.encode;
Expand Down Expand Up @@ -132,7 +133,7 @@ public static Query newGeometryQuery(String field, QueryRelation queryRelation,
for (int i = 0; i < xyGeometries.length; i++) {
builder.add(newGeometryQuery(field, queryRelation, xyGeometries[i]), BooleanClause.Occur.MUST);
}
return builder.build();
return new ConstantScoreQuery(builder.build());
}
return new XYShapeQuery(field, queryRelation, xyGeometries);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.lucene.geo.GeoUtils;
import org.apache.lucene.geo.LatLonGeometry;
import org.apache.lucene.geo.Line;
import org.apache.lucene.geo.Point;
import org.apache.lucene.geo.Polygon;
import org.apache.lucene.geo.Rectangle;
import org.apache.lucene.geo.Tessellator;
Expand All @@ -36,6 +37,7 @@
import org.apache.lucene.index.SerialMergeScheduler;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.LuceneTestCase;
Expand Down Expand Up @@ -829,6 +831,32 @@ public void testLucene9239() throws Exception {
IOUtils.close(r, dir);
}

public void testContainsWrappingBooleanQuery() throws Exception {

double[] lats = new double[] {-30, -30, 30, 30, -30};
double[] lons = new double[] {-30, 30, 30, -30, -30};
Polygon polygon = new Polygon(lats, lons);

Directory dir = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
Document document = new Document();
addPolygonsToDoc(FIELDNAME, document, polygon);
writer.addDocument(document);

//// search
IndexReader r = writer.getReader();
writer.close();
IndexSearcher s = newSearcher(r);

LatLonGeometry[] geometries = new LatLonGeometry[] { new Rectangle(0, 1, 0, 1), new Point(4, 4) };
// geometries within the polygon
Query q = LatLonShape.newGeometryQuery(FIELDNAME, QueryRelation.CONTAINS, geometries);
TopDocs topDocs = s.search(q, 1);
assertEquals(1, topDocs.scoreDocs.length);
assertEquals(1.0, topDocs.scoreDocs[0].score, 0.0);
IOUtils.close(r, dir);
}

public void testContainsIndexedGeometryCollection() throws Exception {
Directory dir = newDirectory();
RandomIndexWriter w = new RandomIndexWriter(random(), dir);
Expand Down
28 changes: 28 additions & 0 deletions lucene/core/src/test/org/apache/lucene/document/TestXYShape.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
import org.apache.lucene.geo.XYCircle;
import org.apache.lucene.geo.XYGeometry;
import org.apache.lucene.geo.XYLine;
import org.apache.lucene.geo.XYPoint;
import org.apache.lucene.geo.XYPolygon;
import org.apache.lucene.geo.XYRectangle;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.LuceneTestCase;
Expand Down Expand Up @@ -202,6 +204,32 @@ public void testPointIndexAndDistanceQuery() throws Exception {
IOUtils.close(r, dir);
}

public void testContainsWrappingBooleanQuery() throws Exception {

float[] ys = new float[] {-30, -30, 30, 30, -30};
float[] xs = new float[] {-30, 30, 30, -30, -30};
XYPolygon polygon = new XYPolygon(xs, ys);

Directory dir = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
Document document = new Document();
addPolygonsToDoc(FIELDNAME, document, polygon);
writer.addDocument(document);

//// search
IndexReader r = writer.getReader();
writer.close();
IndexSearcher s = newSearcher(r);

XYGeometry[] geometries = new XYGeometry[] { new XYRectangle(0, 1, 0, 1), new XYPoint(4, 4) };
// geometries within the polygon
Query q = XYShape.newGeometryQuery(FIELDNAME, QueryRelation.CONTAINS, geometries);
TopDocs topDocs = s.search(q, 1);
assertEquals(1, topDocs.scoreDocs.length);
assertEquals(1.0, topDocs.scoreDocs[0].score, 0.0);
IOUtils.close(r, dir);
}

public void testContainsIndexedGeometryCollection() throws Exception {
Directory dir = newDirectory();
RandomIndexWriter w = new RandomIndexWriter(random(), dir);
Expand Down

0 comments on commit b63c37d

Please sign in to comment.