Skip to content

Commit e2c5823

Browse files
authored
Fix ignore_unmapped setting when using geo_shape query with a pre-indexed shape (#136961) (#136973)
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.
1 parent e128498 commit e2c5823

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-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: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,17 @@ public ShapeRelation relation() {
279279
* {@link MatchNoDocsQuery} in place of this query) or throw an exception if
280280
* the field is unmapped.
281281
*/
282-
public AbstractGeometryQueryBuilder<QB> ignoreUnmapped(boolean ignoreUnmapped) {
282+
@SuppressWarnings("unchecked")
283+
public QB ignoreUnmapped(boolean ignoreUnmapped) {
283284
this.ignoreUnmapped = ignoreUnmapped;
284-
return this;
285+
return (QB) this;
286+
}
287+
288+
/**
289+
* @return whether the query builder should ignore unmapped fields
290+
*/
291+
public boolean ignoreUnmapped() {
292+
return ignoreUnmapped;
285293
}
286294

287295
/** builds the appropriate lucene shape query */
@@ -438,7 +446,9 @@ protected int doHashCode() {
438446
@Override
439447
protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws IOException {
440448
if (supplier != null) {
441-
return supplier.get() == null ? this : newShapeQueryBuilder(this.fieldName, supplier.get()).relation(relation);
449+
return supplier.get() == null
450+
? this
451+
: newShapeQueryBuilder(this.fieldName, supplier.get()).relation(relation).ignoreUnmapped(ignoreUnmapped);
442452
} else if (this.shape == null) {
443453
SetOnce<Geometry> supplier = new SetOnce<>();
444454
queryRewriteContext.registerAsyncAction((client, listener) -> {
@@ -449,7 +459,8 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws
449459
listener.onResponse(null);
450460
}));
451461
});
452-
return newShapeQueryBuilder(this.fieldName, supplier::get, this.indexedShapeId).relation(relation);
462+
return newShapeQueryBuilder(this.fieldName, supplier::get, this.indexedShapeId).relation(relation)
463+
.ignoreUnmapped(ignoreUnmapped);
453464
}
454465
return this;
455466
}

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)