From c8038b5010dfa2c8ed6df9130051914661244d6e Mon Sep 17 00:00:00 2001 From: iverase Date: Tue, 13 Jul 2021 09:25:24 +0200 Subject: [PATCH 1/2] Use contains relation for geo_point intersection over geo_point field --- .../index/mapper/GeoPointFieldMapper.java | 10 ++++++++-- .../search/geo/GeoPointShapeQueryTests.java | 1 - 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/index/mapper/GeoPointFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/GeoPointFieldMapper.java index eae0dcf7efad1..6e0e90b90bf65 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/GeoPointFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/GeoPointFieldMapper.java @@ -9,6 +9,7 @@ import org.apache.lucene.document.LatLonDocValuesField; import org.apache.lucene.document.LatLonPoint; +import org.apache.lucene.document.ShapeField; import org.apache.lucene.document.StoredField; import org.apache.lucene.geo.LatLonGeometry; import org.apache.lucene.index.LeafReaderContext; @@ -28,6 +29,7 @@ import org.elasticsearch.common.xcontent.support.MapXContentParser; import org.elasticsearch.geometry.Geometry; import org.elasticsearch.geometry.Point; +import org.elasticsearch.geometry.ShapeType; import org.elasticsearch.index.fielddata.IndexFieldData; import org.elasticsearch.index.fielddata.plain.AbstractLatLonPointIndexFieldData; import org.elasticsearch.index.query.SearchExecutionContext; @@ -260,9 +262,13 @@ public Query geoShapeQuery(Geometry shape, String fieldName, ShapeRelation relat if (luceneGeometries.length == 0) { return new MatchNoDocsQuery(); } - Query query = LatLonPoint.newGeometryQuery(fieldName, relation.getLuceneRelation(), luceneGeometries); + // For point queries and intersects, lucene does not match points that are encoded to Integer.MAX_VALUE. Use contains + // instead that returns the expected results. + ShapeField.QueryRelation luceneRelation = relation == ShapeRelation.INTERSECTS && shape.type() == ShapeType.POINT ? + ShapeField.QueryRelation.CONTAINS : relation.getLuceneRelation(); + Query query = LatLonPoint.newGeometryQuery(fieldName, luceneRelation, luceneGeometries); if (hasDocValues()) { - Query dvQuery = LatLonDocValuesField.newSlowGeometryQuery(fieldName, relation.getLuceneRelation(), luceneGeometries); + Query dvQuery = LatLonDocValuesField.newSlowGeometryQuery(fieldName, luceneRelation, luceneGeometries); query = new IndexOrDocValuesQuery(query, dvQuery); } return query; diff --git a/server/src/test/java/org/elasticsearch/search/geo/GeoPointShapeQueryTests.java b/server/src/test/java/org/elasticsearch/search/geo/GeoPointShapeQueryTests.java index 2066921e0afb1..89a4551f3d3b0 100644 --- a/server/src/test/java/org/elasticsearch/search/geo/GeoPointShapeQueryTests.java +++ b/server/src/test/java/org/elasticsearch/search/geo/GeoPointShapeQueryTests.java @@ -34,7 +34,6 @@ protected void createMapping(String indexName, String fieldName, Settings settin client().admin().indices().prepareCreate(indexName).setMapping(xcb).setSettings(settings).get(); } - @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/75103") public void testFieldAlias() throws IOException { String mapping = Strings.toString(XContentFactory.jsonBuilder() .startObject() From 8541134947ffc92dab1ff3de901e0c7d9e6f5a3c Mon Sep 17 00:00:00 2001 From: iverase Date: Wed, 14 Jul 2021 07:42:27 +0200 Subject: [PATCH 2/2] expand if for clarity --- .../index/mapper/GeoPointFieldMapper.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/index/mapper/GeoPointFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/GeoPointFieldMapper.java index 6e0e90b90bf65..121b0eb0da2b4 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/GeoPointFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/GeoPointFieldMapper.java @@ -262,10 +262,14 @@ public Query geoShapeQuery(Geometry shape, String fieldName, ShapeRelation relat if (luceneGeometries.length == 0) { return new MatchNoDocsQuery(); } - // For point queries and intersects, lucene does not match points that are encoded to Integer.MAX_VALUE. Use contains - // instead that returns the expected results. - ShapeField.QueryRelation luceneRelation = relation == ShapeRelation.INTERSECTS && shape.type() == ShapeType.POINT ? - ShapeField.QueryRelation.CONTAINS : relation.getLuceneRelation(); + final ShapeField.QueryRelation luceneRelation; + if (shape.type() == ShapeType.POINT && relation == ShapeRelation.INTERSECTS) { + // For point queries and intersects, lucene does not match points that are encoded to Integer.MAX_VALUE. + // We use contains instead. + luceneRelation = ShapeField.QueryRelation.CONTAINS; + } else { + luceneRelation = relation.getLuceneRelation(); + } Query query = LatLonPoint.newGeometryQuery(fieldName, luceneRelation, luceneGeometries); if (hasDocValues()) { Query dvQuery = LatLonDocValuesField.newSlowGeometryQuery(fieldName, luceneRelation, luceneGeometries);