LUCENE-9606: Wrap boolean queries generated by shape fields with a Constant score query (#2093)

This commit is contained in:
Ignacio Vera 2020-11-25 08:40:38 +01:00 committed by GitHub
parent c71f119e9a
commit b63c37d1e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 62 additions and 2 deletions

View File

@ -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
---------------------

View File

@ -32,6 +32,7 @@ import org.apache.lucene.geo.Tessellator;
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;
@ -170,7 +171,7 @@ public class LatLonShape {
builder.add(new LatLonShapeQuery(field, QueryRelation.CONTAINS, geometry), BooleanClause.Occur.MUST);
}
}
return builder.build();
return new ConstantScoreQuery(builder.build());
}
}

View File

@ -31,6 +31,7 @@ import org.apache.lucene.geo.XYLine;
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;
@ -132,7 +133,7 @@ public class XYShape {
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);
}

View File

@ -25,6 +25,7 @@ import org.apache.lucene.geo.GeoTestUtil;
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;
@ -36,6 +37,7 @@ import org.apache.lucene.index.RandomIndexWriter;
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;
@ -829,6 +831,32 @@ public class TestLatLonShape extends LuceneTestCase {
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);

View File

@ -25,12 +25,14 @@ import org.apache.lucene.geo.Tessellator;
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;
@ -202,6 +204,32 @@ public class TestXYShape extends LuceneTestCase {
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);