Skip to content

Commit

Permalink
Adding version condition while adding geoshape doc values to the inde…
Browse files Browse the repository at this point in the history
…x, to ensure backward compability.

Signed-off-by: Navneet Verma <navneev@amazon.com>
  • Loading branch information
navneet1v committed Nov 5, 2023
1 parent 747f7d1 commit 59c2d18
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
"Insert Document with geoshape field":
- do:
bulk:
refresh: true
body:
- '{"index": {"_index": "geo_shape_index_old", "_id":191}}'
- '{"name": "NEMO Science Museum","location": {"type": "envelope","coordinates": [ [100.0, 1.0], [101.0, 0.0] ]}}'
- '{"index": {"_index": "geo_shape_index_old", "_id":219}}'
- '{"name": "NEMO Science Museum","location": {"type": "envelope","coordinates": [ [100.0, 1.0], [106.0, 0.0] ]}}'

- do:
search:
rest_total_hits_as_int: true
index: geo_shape_index_old
- match: { hits.total: 2 }
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
"Create index with Geoshape field":
- do:
indices.create:
index: geo_shape_index_old
body:
settings:
index:
number_of_replicas: 2
mappings:
"properties":
"location":
"type": "geo_shape"

- do:
bulk:
refresh: true
body:
- '{"index": {"_index": "geo_shape_index_old", "_id":191}}'
- '{"name": "NEMO Science Museum","location": {"type": "envelope","coordinates": [ [100.0, 1.0], [101.0, 0.0] ]}}'
- '{"index": {"_index": "geo_shape_index_old", "_id":219}}'
- '{"name": "NEMO Science Museum","location": {"type": "envelope","coordinates": [ [100.0, 1.0], [106.0, 0.0] ]}}'

- do:
search:
rest_total_hits_as_int: true
index: geo_shape_index_old
- match: { hits.total: 2 }
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
"Validate we are able to index documents after upgrade":
- do:
bulk:
refresh: true
body:
- '{"index": {"_index": "geo_shape_index_old", "_id":191}}'
- '{"name": "NEMO Science Museum","location": {"type": "envelope","coordinates": [ [100.0, 1.0], [101.0, 0.0] ]}}'
- '{"index": {"_index": "geo_shape_index_old", "_id":219}}'
- '{"name": "NEMO Science Museum","location": {"type": "envelope","coordinates": [ [100.0, 1.0], [106.0, 0.0] ]}}'

- do:
search:
rest_total_hits_as_int: true
index: geo_shape_index_old
- match: { hits.total: 2 }


---
"Create index with Geoshape field in new cluster":
- do:
indices.create:
index: geo_shape_index_new
body:
settings:
index:
number_of_replicas: 2
mappings:
"properties":
"location":
"type": "geo_shape"

- do:
bulk:
refresh: true
body:
- '{"index": {"_index": "geo_shape_index_new", "_id":191}}'
- '{"name": "NEMO Science Museum","location": {"type": "envelope","coordinates": [ [100.0, 1.0], [101.0, 0.0] ]}}'
- '{"index": {"_index": "geo_shape_index_new", "_id":219}}'
- '{"name": "NEMO Science Museum","location": {"type": "envelope","coordinates": [ [100.0, 1.0], [106.0, 0.0] ]}}'

- do:
search:
rest_total_hits_as_int: true
index: geo_shape_index_new
- match: { hits.total: 2 }

- do:
search:
rest_total_hits_as_int: true
index: geo_shape_index_new
body:
aggregations:
myaggregation:
geo_bounds:
field: "location"
- match: { hits.total: 2 }
- match: { aggregations.myaggregation.bounds.top_left.lat: 0.9999999823048711 }
- match: { aggregations.myaggregation.bounds.top_left.lon: 99.99999999068677 }
- match: { aggregations.myaggregation.bounds.bottom_right.lat: 0.0 }
- match: { aggregations.myaggregation.bounds.bottom_right.lon: 105.99999996833503 }
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@

package org.opensearch.index.mapper;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.document.LatLonShape;
import org.apache.lucene.index.IndexOptions;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.search.Query;
import org.opensearch.Version;
import org.opensearch.common.Explicit;
import org.opensearch.common.geo.GeometryParser;
import org.opensearch.common.geo.ShapeRelation;
Expand Down Expand Up @@ -77,6 +80,7 @@
* @opensearch.internal
*/
public class GeoShapeFieldMapper extends AbstractShapeGeometryFieldMapper<Geometry, Geometry> {
private static final Logger logger = LogManager.getLogger(GeoShapeFieldMapper.class);
public static final String CONTENT_TYPE = "geo_shape";
public static final FieldType FIELD_TYPE = new FieldType();
static {
Expand Down Expand Up @@ -205,9 +209,24 @@ protected void addDocValuesFields(
final List<IndexableField> indexableFields,
final ParseContext context
) {
Field[] fieldsArray = new Field[indexableFields.size()];
fieldsArray = indexableFields.toArray(fieldsArray);
context.doc().add(LatLonShape.createDocValueField(name, fieldsArray));
/*
* We are adding the doc values for GeoShape only if the index is created with 2.9 and above version of
* OpenSearch. If we don't do that after the upgrade of OpenSearch customers are not able to index documents
* with GeoShape fields. Github issue: https://github.com/opensearch-project/OpenSearch/issues/10958,
* https://github.com/opensearch-project/OpenSearch/issues/10795
*/
if (context.indexSettings().getIndexVersionCreated().onOrAfter(Version.V_2_9_0)) {
Field[] fieldsArray = new Field[indexableFields.size()];
fieldsArray = indexableFields.toArray(fieldsArray);
context.doc().add(LatLonShape.createDocValueField(name, fieldsArray));
} else {
logger.warn(
"The index was created with Version : {}, for geoshape doc values to work index must be "
+ "created with OpenSearch Version : {} or above",
context.indexSettings().getIndexVersionCreated(),
Version.V_2_9_0
);
}
}

@Override
Expand Down

0 comments on commit 59c2d18

Please sign in to comment.