Skip to content

Commit 0e97999

Browse files
authored
Fix ignore_unmapped setting when using geo_shape query with a pre-indexed shape (elastic#136961) (elastic#136975)
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 8e0e3fa commit 0e97999

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
@@ -288,9 +288,17 @@ public ShapeRelation relation() {
288288
* {@link MatchNoDocsQuery} in place of this query) or throw an exception if
289289
* the field is unmapped.
290290
*/
291-
public AbstractGeometryQueryBuilder<QB> ignoreUnmapped(boolean ignoreUnmapped) {
291+
@SuppressWarnings("unchecked")
292+
public QB ignoreUnmapped(boolean ignoreUnmapped) {
292293
this.ignoreUnmapped = ignoreUnmapped;
293-
return this;
294+
return (QB) this;
295+
}
296+
297+
/**
298+
* @return whether the query builder should ignore unmapped fields
299+
*/
300+
public boolean ignoreUnmapped() {
301+
return ignoreUnmapped;
294302
}
295303

296304
/** builds the appropriate lucene shape query */
@@ -447,7 +455,9 @@ protected int doHashCode() {
447455
@Override
448456
protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws IOException {
449457
if (supplier != null) {
450-
return supplier.get() == null ? this : newShapeQueryBuilder(this.fieldName, supplier.get()).relation(relation);
458+
return supplier.get() == null
459+
? this
460+
: newShapeQueryBuilder(this.fieldName, supplier.get()).relation(relation).ignoreUnmapped(ignoreUnmapped);
451461
} else if (this.shape == null) {
452462
SetOnce<Geometry> supplier = new SetOnce<>();
453463
queryRewriteContext.registerAsyncAction((client, listener) -> {
@@ -458,7 +468,8 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws
458468
listener.onResponse(null);
459469
}));
460470
});
461-
return newShapeQueryBuilder(this.fieldName, supplier::get, this.indexedShapeId).relation(relation);
471+
return newShapeQueryBuilder(this.fieldName, supplier::get, this.indexedShapeId).relation(relation)
472+
.ignoreUnmapped(ignoreUnmapped);
462473
}
463474
return this;
464475
}

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)