Skip to content

Commit

Permalink
[FIX] Make GeoContext mapping idempotent
Browse files Browse the repository at this point in the history
closes #10581
closes #8937
  • Loading branch information
areek committed Apr 15, 2015
1 parent 5dcafca commit 24a0374
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
Expand Up @@ -591,7 +591,9 @@ public GeolocationContextMapping build() {
if(precisions.isEmpty()) {
precisions.add(GeoHashUtils.PRECISION);
}
return new GeolocationContextMapping(name, precisions.toArray(), neighbors, defaultLocations, fieldName);
int[] precisionArray = precisions.toArray();
Arrays.sort(precisionArray);
return new GeolocationContextMapping(name, precisionArray, neighbors, defaultLocations, fieldName);
}

}
Expand Down
Expand Up @@ -24,8 +24,10 @@
import org.elasticsearch.action.suggest.SuggestRequest;
import org.elasticsearch.action.suggest.SuggestRequestBuilder;
import org.elasticsearch.action.suggest.SuggestResponse;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.geo.GeoHashUtils;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.mapper.MapperParsingException;
Expand Down Expand Up @@ -153,7 +155,43 @@ public void testMultiLevelGeo() throws Exception {
assertEquals("Hotel Amsterdam in Berlin", suggestResponse.getSuggest().getSuggestion(suggestionName).iterator().next()
.getOptions().iterator().next().getText().string());
}
}
}

@Test
public void testMappingIdempotency() throws Exception {
List<Integer> precisions = new ArrayList<>();
for (int i = 0; i < randomIntBetween(4, 12); i++) {
precisions.add(i+1);
}
Collections.shuffle(precisions, getRandom());
XContentBuilder mapping = jsonBuilder().startObject().startObject(TYPE)
.startObject("properties").startObject("completion")
.field("type", "completion")
.startObject("context")
.startObject("location")
.field("type", "geo")
.array("precision", precisions.toArray(new Integer[precisions.size()]))
.endObject()
.endObject().endObject()
.endObject().endObject();

assertAcked(prepareCreate(INDEX).addMapping(TYPE, mapping.string()));
ensureYellow();

Collections.shuffle(precisions, getRandom());
mapping = jsonBuilder().startObject().startObject(TYPE)
.startObject("properties").startObject("completion")
.field("type", "completion")
.startObject("context")
.startObject("location")
.field("type", "geo")
.array("precision", precisions.toArray(new Integer[precisions.size()]))
.endObject()
.endObject().endObject()
.endObject().endObject();
assertAcked(client().admin().indices().preparePutMapping(INDEX).setType(TYPE).setSource(mapping.string()).get());
}


@Test
public void testGeoField() throws Exception {
Expand Down

0 comments on commit 24a0374

Please sign in to comment.