use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.
the class AbstractGeoTestCase method setupSuiteScopeCluster.
@Override
public void setupSuiteScopeCluster() throws Exception {
createIndex(UNMAPPED_IDX_NAME);
assertAcked(prepareCreate(IDX_NAME).addMapping("type", SINGLE_VALUED_FIELD_NAME, "type=geo_point", MULTI_VALUED_FIELD_NAME, "type=geo_point", NUMBER_FIELD_NAME, "type=long", "tag", "type=keyword"));
singleTopLeft = new GeoPoint(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
singleBottomRight = new GeoPoint(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY);
multiTopLeft = new GeoPoint(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
multiBottomRight = new GeoPoint(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY);
singleCentroid = new GeoPoint(0, 0);
multiCentroid = new GeoPoint(0, 0);
unmappedCentroid = new GeoPoint(0, 0);
numDocs = randomIntBetween(6, 20);
numUniqueGeoPoints = randomIntBetween(1, numDocs);
expectedDocCountsForGeoHash = new ObjectIntHashMap<>(numDocs * 2);
expectedCentroidsForGeoHash = new ObjectObjectHashMap<>(numDocs * 2);
singleValues = new GeoPoint[numUniqueGeoPoints];
for (int i = 0; i < singleValues.length; i++) {
singleValues[i] = RandomGeoGenerator.randomPoint(random());
updateBoundsTopLeft(singleValues[i], singleTopLeft);
updateBoundsBottomRight(singleValues[i], singleBottomRight);
}
multiValues = new GeoPoint[numUniqueGeoPoints];
for (int i = 0; i < multiValues.length; i++) {
multiValues[i] = RandomGeoGenerator.randomPoint(random());
updateBoundsTopLeft(multiValues[i], multiTopLeft);
updateBoundsBottomRight(multiValues[i], multiBottomRight);
}
List<IndexRequestBuilder> builders = new ArrayList<>();
GeoPoint singleVal;
final GeoPoint[] multiVal = new GeoPoint[2];
double newMVLat, newMVLon;
for (int i = 0; i < numDocs; i++) {
singleVal = singleValues[i % numUniqueGeoPoints];
multiVal[0] = multiValues[i % numUniqueGeoPoints];
multiVal[1] = multiValues[(i + 1) % numUniqueGeoPoints];
builders.add(client().prepareIndex(IDX_NAME, "type").setSource(jsonBuilder().startObject().array(SINGLE_VALUED_FIELD_NAME, singleVal.lon(), singleVal.lat()).startArray(MULTI_VALUED_FIELD_NAME).startArray().value(multiVal[0].lon()).value(multiVal[0].lat()).endArray().startArray().value(multiVal[1].lon()).value(multiVal[1].lat()).endArray().endArray().field(NUMBER_FIELD_NAME, i).field("tag", "tag" + i).endObject()));
singleCentroid = singleCentroid.reset(singleCentroid.lat() + (singleVal.lat() - singleCentroid.lat()) / (i + 1), singleCentroid.lon() + (singleVal.lon() - singleCentroid.lon()) / (i + 1));
newMVLat = (multiVal[0].lat() + multiVal[1].lat()) / 2d;
newMVLon = (multiVal[0].lon() + multiVal[1].lon()) / 2d;
multiCentroid = multiCentroid.reset(multiCentroid.lat() + (newMVLat - multiCentroid.lat()) / (i + 1), multiCentroid.lon() + (newMVLon - multiCentroid.lon()) / (i + 1));
}
assertAcked(prepareCreate(EMPTY_IDX_NAME).addMapping("type", SINGLE_VALUED_FIELD_NAME, "type=geo_point"));
assertAcked(prepareCreate(DATELINE_IDX_NAME).addMapping("type", SINGLE_VALUED_FIELD_NAME, "type=geo_point", MULTI_VALUED_FIELD_NAME, "type=geo_point", NUMBER_FIELD_NAME, "type=long", "tag", "type=keyword"));
GeoPoint[] geoValues = new GeoPoint[5];
geoValues[0] = new GeoPoint(38, 178);
geoValues[1] = new GeoPoint(12, -179);
geoValues[2] = new GeoPoint(-24, 170);
geoValues[3] = new GeoPoint(32, -175);
geoValues[4] = new GeoPoint(-11, 178);
for (int i = 0; i < 5; i++) {
builders.add(client().prepareIndex(DATELINE_IDX_NAME, "type").setSource(jsonBuilder().startObject().array(SINGLE_VALUED_FIELD_NAME, geoValues[i].lon(), geoValues[i].lat()).field(NUMBER_FIELD_NAME, i).field("tag", "tag" + i).endObject()));
}
assertAcked(prepareCreate(HIGH_CARD_IDX_NAME).setSettings(Settings.builder().put("number_of_shards", 2)).addMapping("type", SINGLE_VALUED_FIELD_NAME, "type=geo_point", MULTI_VALUED_FIELD_NAME, "type=geo_point", NUMBER_FIELD_NAME, "type=long,store=true", "tag", "type=keyword"));
for (int i = 0; i < 2000; i++) {
singleVal = singleValues[i % numUniqueGeoPoints];
builders.add(client().prepareIndex(HIGH_CARD_IDX_NAME, "type").setSource(jsonBuilder().startObject().array(SINGLE_VALUED_FIELD_NAME, singleVal.lon(), singleVal.lat()).startArray(MULTI_VALUED_FIELD_NAME).startArray().value(multiValues[i % numUniqueGeoPoints].lon()).value(multiValues[i % numUniqueGeoPoints].lat()).endArray().startArray().value(multiValues[(i + 1) % numUniqueGeoPoints].lon()).value(multiValues[(i + 1) % numUniqueGeoPoints].lat()).endArray().endArray().field(NUMBER_FIELD_NAME, i).field("tag", "tag" + i).endObject()));
updateGeohashBucketsCentroid(singleVal);
}
builders.add(client().prepareIndex(IDX_ZERO_NAME, "type").setSource(jsonBuilder().startObject().array(SINGLE_VALUED_FIELD_NAME, 0.0, 1.0).endObject()));
assertAcked(prepareCreate(IDX_ZERO_NAME).addMapping("type", SINGLE_VALUED_FIELD_NAME, "type=geo_point"));
indexRandom(true, builders);
ensureSearchable();
// Added to debug a test failure where the terms aggregation seems to be reporting two documents with the same value for NUMBER_FIELD_NAME. This will check that after
// random indexing each document only has 1 value for NUMBER_FIELD_NAME and it is the correct value. Following this initial change its seems that this call was getting
// more that 2000 hits (actual value was 2059) so now it will also check to ensure all hits have the correct index and type
SearchResponse response = client().prepareSearch(HIGH_CARD_IDX_NAME).addStoredField(NUMBER_FIELD_NAME).addSort(SortBuilders.fieldSort(NUMBER_FIELD_NAME).order(SortOrder.ASC)).setSize(5000).get();
assertSearchResponse(response);
long totalHits = response.getHits().getTotalHits();
XContentBuilder builder = XContentFactory.jsonBuilder();
response.toXContent(builder, ToXContent.EMPTY_PARAMS);
logger.info("Full high_card_idx Response Content:\n{ {} }", builder.string());
for (int i = 0; i < totalHits; i++) {
SearchHit searchHit = response.getHits().getAt(i);
assertThat("Hit " + i + " with id: " + searchHit.getId(), searchHit.getIndex(), equalTo("high_card_idx"));
assertThat("Hit " + i + " with id: " + searchHit.getId(), searchHit.getType(), equalTo("type"));
SearchHitField hitField = searchHit.field(NUMBER_FIELD_NAME);
assertThat("Hit " + i + " has wrong number of values", hitField.getValues().size(), equalTo(1));
Long value = hitField.getValue();
assertThat("Hit " + i + " has wrong value", value.intValue(), equalTo(i));
}
assertThat(totalHits, equalTo(2000L));
}
use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.
the class AbstractGeoTestCase method updateGeohashBucketsCentroid.
private void updateGeohashBucketsCentroid(final GeoPoint location) {
String hash = GeoHashUtils.stringEncode(location.lon(), location.lat(), GeoHashUtils.PRECISION);
for (int precision = GeoHashUtils.PRECISION; precision > 0; --precision) {
final String h = hash.substring(0, precision);
expectedDocCountsForGeoHash.put(h, expectedDocCountsForGeoHash.getOrDefault(h, 0) + 1);
expectedCentroidsForGeoHash.put(h, updateHashCentroid(h, location));
}
}
use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.
the class ContextCompletionSuggestSearchIT method testGeoPointContext.
public void testGeoPointContext() throws Exception {
LinkedHashMap<String, ContextMapping> map = new LinkedHashMap<>();
map.put("geo", ContextBuilder.geo("geo").build());
final CompletionMappingBuilder mapping = new CompletionMappingBuilder().context(map);
createIndexAndMapping(mapping);
int numDocs = 10;
List<IndexRequestBuilder> indexRequestBuilders = new ArrayList<>();
for (int i = 0; i < numDocs; i++) {
XContentBuilder source = jsonBuilder().startObject().startObject(FIELD).field("input", "suggestion" + i).field("weight", i + 1).startObject("contexts").startObject("geo").field("lat", 52.22).field("lon", 4.53).endObject().endObject().endObject().endObject();
indexRequestBuilders.add(client().prepareIndex(INDEX, TYPE, "" + i).setSource(source));
}
indexRandom(true, indexRequestBuilders);
CompletionSuggestionBuilder prefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg").contexts(Collections.singletonMap("geo", Collections.singletonList(GeoQueryContext.builder().setGeoPoint(new GeoPoint(52.2263, 4.543)).build())));
assertSuggestions("foo", prefix, "suggestion9", "suggestion8", "suggestion7", "suggestion6", "suggestion5");
}
use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.
the class SimpleSortIT method testSortMinValueScript.
public void testSortMinValueScript() throws IOException {
String mapping = jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("lvalue").field("type", "long").endObject().startObject("dvalue").field("type", "double").endObject().startObject("svalue").field("type", "keyword").endObject().startObject("gvalue").field("type", "geo_point").endObject().endObject().endObject().endObject().string();
assertAcked(prepareCreate("test").addMapping("type1", mapping, XContentType.JSON));
ensureGreen();
for (int i = 0; i < 10; i++) {
client().prepareIndex("test", "type1", "" + i).setSource(jsonBuilder().startObject().field("ord", i).array("svalue", new String[] { "" + i, "" + (i + 1), "" + (i + 2) }).array("lvalue", new long[] { i, i + 1, i + 2 }).array("dvalue", new double[] { i, i + 1, i + 2 }).startObject("gvalue").field("lat", (double) i + 1).field("lon", (double) i).endObject().endObject()).get();
}
for (int i = 10; i < 20; i++) {
// add some docs that don't have values in those fields
client().prepareIndex("test", "type1", "" + i).setSource(jsonBuilder().startObject().field("ord", i).endObject()).get();
}
client().admin().indices().prepareRefresh("test").get();
// test the long values
SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).addScriptField("min", new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "get min long", Collections.emptyMap())).addSort(SortBuilders.fieldSort("ord").order(SortOrder.ASC).unmappedType("long")).setSize(10).get();
assertNoFailures(searchResponse);
assertHitCount(searchResponse, 20L);
for (int i = 0; i < 10; i++) {
SearchHit searchHit = searchResponse.getHits().getAt(i);
assertThat("res: " + i + " id: " + searchHit.getId(), searchHit.field("min").getValue(), equalTo((long) i));
}
// test the double values
searchResponse = client().prepareSearch().setQuery(matchAllQuery()).addScriptField("min", new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "get min double", Collections.emptyMap())).addSort(SortBuilders.fieldSort("ord").order(SortOrder.ASC).unmappedType("long")).setSize(10).get();
assertNoFailures(searchResponse);
assertHitCount(searchResponse, 20L);
for (int i = 0; i < 10; i++) {
SearchHit searchHit = searchResponse.getHits().getAt(i);
assertThat("res: " + i + " id: " + searchHit.getId(), searchHit.field("min").getValue(), equalTo((double) i));
}
// test the string values
searchResponse = client().prepareSearch().setQuery(matchAllQuery()).addScriptField("min", new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "get min string", Collections.emptyMap())).addSort(SortBuilders.fieldSort("ord").order(SortOrder.ASC).unmappedType("long")).setSize(10).get();
assertNoFailures(searchResponse);
assertHitCount(searchResponse, 20L);
for (int i = 0; i < 10; i++) {
SearchHit searchHit = searchResponse.getHits().getAt(i);
assertThat("res: " + i + " id: " + searchHit.getId(), searchHit.field("min").getValue(), equalTo(i));
}
// test the geopoint values
searchResponse = client().prepareSearch().setQuery(matchAllQuery()).addScriptField("min", new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "get min geopoint lon", Collections.emptyMap())).addSort(SortBuilders.fieldSort("ord").order(SortOrder.ASC).unmappedType("long")).setSize(10).get();
assertNoFailures(searchResponse);
assertHitCount(searchResponse, 20L);
for (int i = 0; i < 10; i++) {
SearchHit searchHit = searchResponse.getHits().getAt(i);
assertThat("res: " + i + " id: " + searchHit.getId(), searchHit.field("min").getValue(), closeTo(i, GeoUtils.TOLERANCE));
}
}
use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.
the class ExternalFieldMapperTests method testExternalValuesWithMultifield.
public void testExternalValuesWithMultifield() throws Exception {
Version version = VersionUtils.randomVersionBetween(random(), Version.V_2_0_0, Version.CURRENT);
Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build();
IndexService indexService = createIndex("test", settings);
Map<String, Mapper.TypeParser> mapperParsers = new HashMap<>();
mapperParsers.put(ExternalMapperPlugin.EXTERNAL, new ExternalMapper.TypeParser(ExternalMapperPlugin.EXTERNAL, "foo"));
mapperParsers.put(TextFieldMapper.CONTENT_TYPE, new TextFieldMapper.TypeParser());
mapperParsers.put(KeywordFieldMapper.CONTENT_TYPE, new KeywordFieldMapper.TypeParser());
MapperRegistry mapperRegistry = new MapperRegistry(mapperParsers, Collections.emptyMap());
Supplier<QueryShardContext> queryShardContext = () -> {
return indexService.newQueryShardContext(0, null, () -> {
throw new UnsupportedOperationException();
});
};
DocumentMapperParser parser = new DocumentMapperParser(indexService.getIndexSettings(), indexService.mapperService(), indexService.getIndexAnalyzers(), indexService.xContentRegistry(), indexService.similarityService(), mapperRegistry, queryShardContext);
DocumentMapper documentMapper = parser.parse("type", new CompressedXContent(XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field").field("type", ExternalMapperPlugin.EXTERNAL).startObject("fields").startObject("field").field("type", "text").field("store", true).startObject("fields").startObject("raw").field("type", "keyword").field("store", true).endObject().endObject().endObject().endObject().endObject().endObject().endObject().endObject().string()));
ParsedDocument doc = documentMapper.parse("test", "type", "1", XContentFactory.jsonBuilder().startObject().field("field", "1234").endObject().bytes());
assertThat(doc.rootDoc().getField("field.bool"), notNullValue());
assertThat(doc.rootDoc().getField("field.bool").stringValue(), is("T"));
assertThat(doc.rootDoc().getField("field.point"), notNullValue());
GeoPoint point = new GeoPoint().resetFromIndexableField(doc.rootDoc().getField("field.point"));
assertThat(point.lat(), closeTo(42.0, 1E-5));
assertThat(point.lon(), closeTo(51.0, 1E-5));
IndexableField shape = doc.rootDoc().getField("field.shape");
assertThat(shape, notNullValue());
IndexableField field = doc.rootDoc().getField("field.field");
assertThat(field, notNullValue());
assertThat(field.stringValue(), is("foo"));
IndexableField raw = doc.rootDoc().getField("field.field.raw");
assertThat(raw, notNullValue());
assertThat(raw.binaryValue(), is(new BytesRef("foo")));
}
Aggregations