Skip to content

Commit 58af58d

Browse files
authored
Adding version condition while adding geoshape doc values to the index, to ensure backward compatibility. (opensearch-project#11190)
Signed-off-by: Navneet Verma <navneev@amazon.com>
1 parent 2eb43d3 commit 58af58d

File tree

6 files changed

+131
-6
lines changed

6 files changed

+131
-6
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1717

1818
### Fixed
1919
- [BUG] Disable sort optimization for HALF_FLOAT ([#10999](https://github.com/opensearch-project/OpenSearch/pull/10999))
20+
- Adding version condition while adding geoshape doc values to the index, to ensure backward compatibility.([#11095](https://github.com/opensearch-project/OpenSearch/pull/11095))
2021

2122
### Security
2223

modules/geo/src/internalClusterTest/java/org/opensearch/geo/search/aggregations/bucket/AbstractGeoBucketAggregationIntegTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ protected boolean forbidPrivateIndexSettings() {
8787
*/
8888
protected void prepareGeoShapeIndexForAggregations(final Random random) throws Exception {
8989
expectedDocsCountForGeoShapes = new HashMap<>();
90-
final Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, version).build();
90+
final Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).build();
9191
final List<IndexRequestBuilder> geoshapes = new ArrayList<>();
9292
assertAcked(prepareCreate(GEO_SHAPE_INDEX_NAME).setSettings(settings).setMapping(GEO_SHAPE_FIELD_NAME, "type" + "=geo_shape"));
9393
boolean isShapeIntersectingBB = false;
@@ -136,7 +136,7 @@ protected void prepareSingleValueGeoPointIndex(final Random random) throws Excep
136136
expectedDocCountsForSingleGeoPoint = new HashMap<>();
137137
createIndex("idx_unmapped");
138138
final Settings settings = Settings.builder()
139-
.put(IndexMetadata.SETTING_VERSION_CREATED, version)
139+
.put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)
140140
.put("index.number_of_shards", 4)
141141
.put("index.number_of_replicas", 0)
142142
.build();
@@ -160,7 +160,7 @@ protected void prepareSingleValueGeoPointIndex(final Random random) throws Excep
160160

161161
protected void prepareMultiValuedGeoPointIndex(final Random random) throws Exception {
162162
multiValuedExpectedDocCountsGeoPoint = new HashMap<>();
163-
final Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, version).build();
163+
final Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).build();
164164
final List<IndexRequestBuilder> cities = new ArrayList<>();
165165
assertAcked(
166166
prepareCreate("multi_valued_idx").setSettings(settings)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
"Insert Document with geoshape field":
3+
- do:
4+
bulk:
5+
refresh: true
6+
body:
7+
- '{"index": {"_index": "geo_shape_index_old", "_id":191}}'
8+
- '{"name": "NEMO Science Museum","location": {"type": "envelope","coordinates": [ [100.0, 1.0], [101.0, 0.0] ]}}'
9+
- '{"index": {"_index": "geo_shape_index_old", "_id":219}}'
10+
- '{"name": "NEMO Science Museum","location": {"type": "envelope","coordinates": [ [100.0, 1.0], [106.0, 0.0] ]}}'
11+
12+
- do:
13+
search:
14+
rest_total_hits_as_int: true
15+
index: geo_shape_index_old
16+
- match: { hits.total: 2 }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
"Create index with Geoshape field":
3+
- do:
4+
indices.create:
5+
index: geo_shape_index_old
6+
body:
7+
settings:
8+
index:
9+
number_of_replicas: 2
10+
mappings:
11+
"properties":
12+
"location":
13+
"type": "geo_shape"
14+
15+
- do:
16+
bulk:
17+
refresh: true
18+
body:
19+
- '{"index": {"_index": "geo_shape_index_old", "_id":191}}'
20+
- '{"name": "NEMO Science Museum","location": {"type": "envelope","coordinates": [ [100.0, 1.0], [101.0, 0.0] ]}}'
21+
- '{"index": {"_index": "geo_shape_index_old", "_id":219}}'
22+
- '{"name": "NEMO Science Museum","location": {"type": "envelope","coordinates": [ [100.0, 1.0], [106.0, 0.0] ]}}'
23+
24+
- do:
25+
search:
26+
rest_total_hits_as_int: true
27+
index: geo_shape_index_old
28+
- match: { hits.total: 2 }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
"Validate we are able to index documents after upgrade":
3+
- do:
4+
bulk:
5+
refresh: true
6+
body:
7+
- '{"index": {"_index": "geo_shape_index_old", "_id":191}}'
8+
- '{"name": "NEMO Science Museum","location": {"type": "envelope","coordinates": [ [100.0, 1.0], [101.0, 0.0] ]}}'
9+
- '{"index": {"_index": "geo_shape_index_old", "_id":219}}'
10+
- '{"name": "NEMO Science Museum","location": {"type": "envelope","coordinates": [ [100.0, 1.0], [106.0, 0.0] ]}}'
11+
12+
- do:
13+
search:
14+
rest_total_hits_as_int: true
15+
index: geo_shape_index_old
16+
- match: { hits.total: 2 }
17+
18+
19+
---
20+
"Create index with Geoshape field in new cluster":
21+
- do:
22+
indices.create:
23+
index: geo_shape_index_new
24+
body:
25+
settings:
26+
index:
27+
number_of_replicas: 2
28+
mappings:
29+
"properties":
30+
"location":
31+
"type": "geo_shape"
32+
33+
- do:
34+
bulk:
35+
refresh: true
36+
body:
37+
- '{"index": {"_index": "geo_shape_index_new", "_id":191}}'
38+
- '{"name": "NEMO Science Museum","location": {"type": "envelope","coordinates": [ [100.0, 1.0], [101.0, 0.0] ]}}'
39+
- '{"index": {"_index": "geo_shape_index_new", "_id":219}}'
40+
- '{"name": "NEMO Science Museum","location": {"type": "envelope","coordinates": [ [100.0, 1.0], [106.0, 0.0] ]}}'
41+
42+
- do:
43+
search:
44+
rest_total_hits_as_int: true
45+
index: geo_shape_index_new
46+
- match: { hits.total: 2 }
47+
48+
- do:
49+
search:
50+
rest_total_hits_as_int: true
51+
index: geo_shape_index_new
52+
body:
53+
aggregations:
54+
myaggregation:
55+
geo_bounds:
56+
field: "location"
57+
- match: { hits.total: 2 }
58+
- match: { aggregations.myaggregation.bounds.top_left.lat: 0.9999999823048711 }
59+
- match: { aggregations.myaggregation.bounds.top_left.lon: 99.99999999068677 }
60+
- match: { aggregations.myaggregation.bounds.bottom_right.lat: 0.0 }
61+
- match: { aggregations.myaggregation.bounds.bottom_right.lon: 105.99999996833503 }

server/src/main/java/org/opensearch/index/mapper/GeoShapeFieldMapper.java

+22-3
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,15 @@
3131

3232
package org.opensearch.index.mapper;
3333

34+
import org.apache.logging.log4j.LogManager;
35+
import org.apache.logging.log4j.Logger;
3436
import org.apache.lucene.document.Field;
3537
import org.apache.lucene.document.FieldType;
3638
import org.apache.lucene.document.LatLonShape;
3739
import org.apache.lucene.index.IndexOptions;
3840
import org.apache.lucene.index.IndexableField;
3941
import org.apache.lucene.search.Query;
42+
import org.opensearch.Version;
4043
import org.opensearch.common.Explicit;
4144
import org.opensearch.common.geo.GeometryParser;
4245
import org.opensearch.common.geo.ShapeRelation;
@@ -77,6 +80,7 @@
7780
* @opensearch.internal
7881
*/
7982
public class GeoShapeFieldMapper extends AbstractShapeGeometryFieldMapper<Geometry, Geometry> {
83+
private static final Logger logger = LogManager.getLogger(GeoShapeFieldMapper.class);
8084
public static final String CONTENT_TYPE = "geo_shape";
8185
public static final FieldType FIELD_TYPE = new FieldType();
8286
static {
@@ -205,9 +209,24 @@ protected void addDocValuesFields(
205209
final List<IndexableField> indexableFields,
206210
final ParseContext context
207211
) {
208-
Field[] fieldsArray = new Field[indexableFields.size()];
209-
fieldsArray = indexableFields.toArray(fieldsArray);
210-
context.doc().add(LatLonShape.createDocValueField(name, fieldsArray));
212+
/*
213+
* We are adding the doc values for GeoShape only if the index is created with 2.9 and above version of
214+
* OpenSearch. If we don't do that after the upgrade of OpenSearch customers are not able to index documents
215+
* with GeoShape fields. Github issue: https://github.com/opensearch-project/OpenSearch/issues/10958,
216+
* https://github.com/opensearch-project/OpenSearch/issues/10795
217+
*/
218+
if (context.indexSettings().getIndexVersionCreated().onOrAfter(Version.V_2_9_0)) {
219+
Field[] fieldsArray = new Field[indexableFields.size()];
220+
fieldsArray = indexableFields.toArray(fieldsArray);
221+
context.doc().add(LatLonShape.createDocValueField(name, fieldsArray));
222+
} else {
223+
logger.warn(
224+
"The index was created with Version : {}, for geoshape doc values to work index must be "
225+
+ "created with OpenSearch Version : {} or above",
226+
context.indexSettings().getIndexVersionCreated(),
227+
Version.V_2_9_0
228+
);
229+
}
211230
}
212231

213232
@Override

0 commit comments

Comments
 (0)