Skip to content

Allow docvalues-only search on geo_shape #94396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog/94396.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 94396
summary: Allow docvalues-only search on `geo_shape`
area: Geo
type: enhancement
issues: []
8 changes: 8 additions & 0 deletions docs/reference/mapping/types/geo-shape.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ and reject the whole document.
|`coerce` |If `true` unclosed linear rings in polygons will be automatically closed.
| `false`

|`index` |Should the field be quickly searchable? Accepts `true` (default) and `false`.
Fields that only have <<doc-values,`doc_values`>> enabled can still be queried, albeit slower.
| `true`

|`doc_values` |Should the field be stored on disk in a column-stride fashion,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can both index and doc_values be set to false?

so that it can later be used for aggregations or scripting?
| `true`

|=======================================================================


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,17 +197,23 @@ public String typeName() {

@Override
public Query geoShapeQuery(SearchExecutionContext context, String fieldName, ShapeRelation relation, LatLonGeometry... geometries) {
failIfNotIndexedNorDocValuesFallback(context);
// CONTAINS queries are not supported by VECTOR strategy for indices created before version 7.5.0 (Lucene 8.3.0)
if (relation == ShapeRelation.CONTAINS && context.indexVersionCreated().before(Version.V_7_5_0)) {
throw new QueryShardException(
context,
ShapeRelation.CONTAINS + " query relation not supported for Field [" + fieldName + "]."
);
}
Query query = LatLonShape.newGeometryQuery(fieldName, relation.getLuceneRelation(), geometries);
if (hasDocValues()) {
final Query queryDocValues = new LatLonShapeDocValuesQuery(fieldName, relation.getLuceneRelation(), geometries);
query = new IndexOrDocValuesQuery(query, queryDocValues);
Query query;
if (isIndexed()) {
query = LatLonShape.newGeometryQuery(fieldName, relation.getLuceneRelation(), geometries);
if (hasDocValues()) {
final Query queryDocValues = new LatLonShapeDocValuesQuery(fieldName, relation.getLuceneRelation(), geometries);
query = new IndexOrDocValuesQuery(query, queryDocValues);
}
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if both indexed and docValues are false? Or perhaps that combination is disallowed? The docs you added do not specify that at least one needs to be positive.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking more carefully I see that null will be returned as the query, so perhaps this case is handled by the caller?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case is handle by failIfNotIndexedNorDocValuesFallback(context);

query = new LatLonShapeDocValuesQuery(fieldName, relation.getLuceneRelation(), geometries);
}
return query;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.tests.index.RandomIndexWriter;
import org.elasticsearch.Version;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.geo.GeoJson;
import org.elasticsearch.common.geo.Orientation;
Expand Down Expand Up @@ -210,6 +211,7 @@ public void testGeoShapeQueryAcrossDateline() throws IOException {

SearchExecutionContext mockedContext = mock(SearchExecutionContext.class);
when(mockedContext.getFieldType(any())).thenReturn(shapeType);
when(mockedContext.indexVersionCreated()).thenReturn(Version.CURRENT);
Query sameShapeQuery = shapeType.geoShapeQuery(mockedContext, fieldName, ShapeRelation.INTERSECTS, geometry);
Query pointOnDatelineQuery = shapeType.geoShapeQuery(
mockedContext,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
setup:
- do:
indices.create:
index: shapes
body:
mappings:
properties:
default:
type: geo_shape
no_doc_values:
type: geo_shape
doc_values: false
no_index:
type: geo_shape
index: false
no_doc_values_no_index:
type: geo_shape
doc_values: false
index: false
- do:
bulk:
refresh: true
body:
- index:
_index: shapes
_id: 1
- '{"default": "POINT(4.912350 52.374081)", "no_doc_values": "POINT(4.912350 52.374081)", "no_index": "POINT(4.912350 52.374081)", "no_doc_values_no_index": "POINT(4.912350 52.374081)"}'
- index:
_index: shapes
_id: 2
- '{"default": "POINT(2.327000 48.860000)", "no_doc_values": "POINT(2.327000 48.860000)", "no_index": "POINT(2.327000 48.860000)", "no_doc_values_no_index": "POINT(2.327000 48.860000)"}'
- do:
indices.refresh: {}

---
"Test field mapping":
- do:
indices.get_mapping:
index: shapes

- match: {shapes.mappings.properties.default.type: geo_shape }
- match: {shapes.mappings.properties.no_doc_values.type: geo_shape }
- match: {shapes.mappings.properties.no_doc_values.doc_values: false }
- match: {shapes.mappings.properties.no_index.type: geo_shape }
- match: {shapes.mappings.properties.no_index.index: false }
- match: {shapes.mappings.properties.no_doc_values_no_index.type: geo_shape }
- match: {shapes.mappings.properties.no_doc_values_no_index.index: false }
- match: {shapes.mappings.properties.no_doc_values_no_index.doc_values: false }

---
"Test query default field":
- do:
search:
index: shapes
body:
query:
geo_bounding_box:
default:
top_left:
lat: 55
lon: 4
bottom_right:
lat: 50
lon: 5

- match: { hits.total.value: 1 }


---
"Test query no_doc_values field":
- do:
search:
index: shapes
body:
query:
geo_bounding_box:
no_doc_values:
top_left:
lat: 55
lon: 4
bottom_right:
lat: 50
lon: 5

- match: { hits.total.value: 1 }


---
"Test query no_index field":
- do:
search:
index: shapes
body:
query:
geo_bounding_box:
no_index:
top_left:
lat: 55
lon: 4
bottom_right:
lat: 50
lon: 5

- match: { hits.total.value: 1 }

---
"Test query no_doc_values_no_index field":
- do:
catch: bad_request
search:
index: shapes
body:
query:
geo_bounding_box:
no_doc_values_no_index:
top_left:
lat: 55
lon: 4
bottom_right:
lat: 50
lon: 5

- match: {error.type: search_phase_execution_exception}
- match: {error.reason: "all shards failed"}
- match: {error.phase: query}
- match: {error.failed_shards.0.reason.type: query_shard_exception}
- match: {error.failed_shards.0.reason.reason: "failed to create query: Cannot search on field [no_doc_values_no_index] since it is not indexed nor has doc values."}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I see there is already error handling for the case I was worried about above. Perhaps a comment in the docs is a good idea?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is something generic and I don't think we should explicitly document it on this field mapping.