Skip to content

Commit 587acce

Browse files
authored
[8.19] Fix ignore_unmapped setting when using geo_shape query with a pre-indexed shape (#136961) (#136974)
* Fix ignore_unmapped setting when using geo_shape query with a pre-indexed shape (#136961) GeoShapeQueryBuilder ignores the value of ignoreUnmapped when rewriting the query so those queries always get a false value. This commit make sure the variable gets correctly assigned. * iter
1 parent 823d7f9 commit 587acce

File tree

4 files changed

+25
-8
lines changed

4 files changed

+25
-8
lines changed

docs/changelog/136961.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
pr: 136961
2+
summary: Fix `ignore_unmapped` setting when using `geo_shape` query with a pre-indexed
3+
shape
4+
area: Geo
5+
type: bug
6+
issues:
7+
- 136954

server/src/main/java/org/elasticsearch/index/query/AbstractGeometryQueryBuilder.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,10 @@ public ShapeRelation relation() {
318318
* {@link MatchNoDocsQuery} in place of this query) or throw an exception if
319319
* the field is unmapped.
320320
*/
321-
public AbstractGeometryQueryBuilder<QB> ignoreUnmapped(boolean ignoreUnmapped) {
321+
@SuppressWarnings("unchecked")
322+
public QB ignoreUnmapped(boolean ignoreUnmapped) {
322323
this.ignoreUnmapped = ignoreUnmapped;
323-
return this;
324+
return (QB) this;
324325
}
325326

326327
/**
@@ -489,7 +490,9 @@ protected int doHashCode() {
489490
@Override
490491
protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws IOException {
491492
if (supplier != null) {
492-
return supplier.get() == null ? this : newShapeQueryBuilder(this.fieldName, supplier.get()).relation(relation);
493+
return supplier.get() == null
494+
? this
495+
: newShapeQueryBuilder(this.fieldName, supplier.get()).relation(relation).ignoreUnmapped(ignoreUnmapped);
493496
} else if (this.shape == null) {
494497
SetOnce<Geometry> supplier = new SetOnce<>();
495498
queryRewriteContext.registerAsyncAction((client, listener) -> {
@@ -500,7 +503,8 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws
500503
listener.onResponse(null);
501504
}));
502505
});
503-
return newShapeQueryBuilder(this.fieldName, supplier::get, this.indexedShapeId).relation(relation);
506+
return newShapeQueryBuilder(this.fieldName, supplier::get, this.indexedShapeId).relation(relation)
507+
.ignoreUnmapped(ignoreUnmapped);
504508
}
505509
return this;
506510
}

test/framework/src/main/java/org/elasticsearch/search/geo/GeoShapeQueryBuilderTestCase.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,9 @@ public void testMustRewrite() throws IOException {
169169
);
170170
assertEquals("query must be rewritten first", e.getMessage());
171171
QueryBuilder rewrite = rewriteAndFetch(query, createSearchExecutionContext());
172-
GeoShapeQueryBuilder geoShapeQueryBuilder = new GeoShapeQueryBuilder(query.fieldName(), indexedShapeToReturn);
172+
GeoShapeQueryBuilder geoShapeQueryBuilder = new GeoShapeQueryBuilder(query.fieldName(), indexedShapeToReturn).ignoreUnmapped(
173+
query.ignoreUnmapped()
174+
);
173175
geoShapeQueryBuilder.strategy(query.strategy());
174176
geoShapeQueryBuilder.relation(query.relation());
175177
assertEquals(geoShapeQueryBuilder, rewrite);
@@ -180,7 +182,9 @@ public void testMultipleRewrite() {
180182
QueryBuilder builder = new BoolQueryBuilder().should(shape).should(shape);
181183

182184
builder = rewriteAndFetch(builder, createSearchExecutionContext());
183-
GeoShapeQueryBuilder expectedShape = new GeoShapeQueryBuilder(shape.fieldName(), indexedShapeToReturn);
185+
GeoShapeQueryBuilder expectedShape = new GeoShapeQueryBuilder(shape.fieldName(), indexedShapeToReturn).ignoreUnmapped(
186+
shape.ignoreUnmapped()
187+
);
184188
expectedShape.strategy(shape.strategy());
185189
expectedShape.relation(shape.relation());
186190
QueryBuilder expected = new BoolQueryBuilder().should(expectedShape).should(expectedShape);

x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/ShapeQueryBuilderTests.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,9 @@ public void testMustRewrite() {
182182
);
183183
assertEquals("query must be rewritten first", e.getMessage());
184184
QueryBuilder rewrite = rewriteAndFetch(query, createSearchExecutionContext());
185-
ShapeQueryBuilder geoShapeQueryBuilder = new ShapeQueryBuilder(fieldName(), indexedShapeToReturn);
185+
ShapeQueryBuilder geoShapeQueryBuilder = new ShapeQueryBuilder(fieldName(), indexedShapeToReturn).ignoreUnmapped(
186+
query.ignoreUnmapped()
187+
);
186188
geoShapeQueryBuilder.relation(query.relation());
187189
assertEquals(geoShapeQueryBuilder, rewrite);
188190
}
@@ -193,7 +195,7 @@ public void testMultipleRewrite() {
193195

194196
builder = rewriteAndFetch(builder, createSearchExecutionContext());
195197

196-
ShapeQueryBuilder expectedShape = new ShapeQueryBuilder(fieldName(), indexedShapeToReturn);
198+
ShapeQueryBuilder expectedShape = new ShapeQueryBuilder(fieldName(), indexedShapeToReturn).ignoreUnmapped(shape.ignoreUnmapped());
197199
expectedShape.relation(shape.relation());
198200
QueryBuilder expected = new BoolQueryBuilder().should(expectedShape).should(expectedShape);
199201
assertEquals(expected, builder);

0 commit comments

Comments
 (0)