use of org.opensearch.common.geo.GeoPoint in project OpenSearch by opensearch-project.
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).setId("" + 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.opensearch.common.geo.GeoPoint in project OpenSearch by opensearch-project.
the class SimpleSortIT method testSortMinValueScript.
public void testSortMinValueScript() throws IOException {
String mapping = Strings.toString(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());
assertAcked(prepareCreate("test").addMapping("type1", mapping, XContentType.JSON));
ensureGreen();
for (int i = 0; i < 10; i++) {
client().prepareIndex("test").setId("" + 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").setId("" + 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.opensearch.common.geo.GeoPoint in project OpenSearch by opensearch-project.
the class GeoDistanceSortBuilderIT method createShuffeldJSONArray.
protected void createShuffeldJSONArray(XContentBuilder builder, GeoPoint[] pointsArray) throws IOException {
List<GeoPoint> points = new ArrayList<>();
points.addAll(Arrays.asList(pointsArray));
builder.startObject();
builder.startArray(LOCATION_FIELD);
int numPoints = points.size();
for (int i = 0; i < numPoints; i++) {
builder.value(points.remove(randomInt(points.size() - 1)));
}
builder.endArray();
builder.endObject();
}
use of org.opensearch.common.geo.GeoPoint in project OpenSearch by opensearch-project.
the class GeoDistanceSortBuilderIT method testSinglePointGeoDistanceSort.
public void testSinglePointGeoDistanceSort() throws ExecutionException, InterruptedException, IOException {
assertAcked(prepareCreate("index").addMapping("type", LOCATION_FIELD, "type=geo_point"));
indexRandom(true, client().prepareIndex("index").setId("d1").setSource(jsonBuilder().startObject().startObject(LOCATION_FIELD).field("lat", 1).field("lon", 1).endObject().endObject()), client().prepareIndex("index").setId("d2").setSource(jsonBuilder().startObject().startObject(LOCATION_FIELD).field("lat", 1).field("lon", 2).endObject().endObject()));
String hashPoint = "s037ms06g7h0";
GeoDistanceSortBuilder geoDistanceSortBuilder = new GeoDistanceSortBuilder(LOCATION_FIELD, hashPoint);
SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).addSort(geoDistanceSortBuilder.sortMode(SortMode.MIN).order(SortOrder.ASC)).get();
checkCorrectSortOrderForGeoSort(searchResponse);
geoDistanceSortBuilder = new GeoDistanceSortBuilder(LOCATION_FIELD, new GeoPoint(2, 2));
searchResponse = client().prepareSearch().setQuery(matchAllQuery()).addSort(geoDistanceSortBuilder.sortMode(SortMode.MIN).order(SortOrder.ASC)).get();
checkCorrectSortOrderForGeoSort(searchResponse);
geoDistanceSortBuilder = new GeoDistanceSortBuilder(LOCATION_FIELD, 2, 2);
searchResponse = client().prepareSearch().setQuery(matchAllQuery()).addSort(geoDistanceSortBuilder.sortMode(SortMode.MIN).order(SortOrder.ASC)).get();
checkCorrectSortOrderForGeoSort(searchResponse);
searchResponse = client().prepareSearch().setSource(new SearchSourceBuilder().sort(SortBuilders.geoDistanceSort(LOCATION_FIELD, 2.0, 2.0))).get();
checkCorrectSortOrderForGeoSort(searchResponse);
searchResponse = client().prepareSearch().setSource(new SearchSourceBuilder().sort(SortBuilders.geoDistanceSort(LOCATION_FIELD, "s037ms06g7h0"))).get();
checkCorrectSortOrderForGeoSort(searchResponse);
searchResponse = client().prepareSearch().setSource(new SearchSourceBuilder().sort(SortBuilders.geoDistanceSort(LOCATION_FIELD, 2.0, 2.0))).get();
checkCorrectSortOrderForGeoSort(searchResponse);
searchResponse = client().prepareSearch().setSource(new SearchSourceBuilder().sort(SortBuilders.geoDistanceSort(LOCATION_FIELD, 2.0, 2.0).validation(GeoValidationMethod.COERCE))).get();
checkCorrectSortOrderForGeoSort(searchResponse);
}
use of org.opensearch.common.geo.GeoPoint in project OpenSearch by opensearch-project.
the class GeoDistanceSortBuilderIT method testManyToManyGeoPoints.
public void testManyToManyGeoPoints() throws ExecutionException, InterruptedException, IOException {
/**
* | q | d1 | d2
* | | |
* | | |
* | | |
* |2 o| x | x
* | | |
* |1 o| x | x
* |___________________________
* 1 2 3 4 5 6 7
*/
Version version = randomBoolean() ? Version.CURRENT : VersionUtils.randomIndexCompatibleVersion(random());
Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, version).build();
assertAcked(prepareCreate("index").setSettings(settings).addMapping("type", LOCATION_FIELD, "type=geo_point"));
XContentBuilder d1Builder = jsonBuilder();
GeoPoint[] d1Points = { new GeoPoint(3, 2), new GeoPoint(4, 1) };
createShuffeldJSONArray(d1Builder, d1Points);
XContentBuilder d2Builder = jsonBuilder();
GeoPoint[] d2Points = { new GeoPoint(5, 1), new GeoPoint(6, 2) };
createShuffeldJSONArray(d2Builder, d2Points);
logger.info("d1: {}", d1Builder);
logger.info("d2: {}", d2Builder);
indexRandom(true, client().prepareIndex("index").setId("d1").setSource(d1Builder), client().prepareIndex("index").setId("d2").setSource(d2Builder));
GeoPoint[] q = new GeoPoint[2];
if (randomBoolean()) {
q[0] = new GeoPoint(2, 1);
q[1] = new GeoPoint(2, 2);
} else {
q[1] = new GeoPoint(2, 2);
q[0] = new GeoPoint(2, 1);
}
SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).addSort(new GeoDistanceSortBuilder(LOCATION_FIELD, q).sortMode(SortMode.MIN).order(SortOrder.ASC)).get();
assertOrderedSearchHits(searchResponse, "d1", "d2");
assertThat((Double) searchResponse.getHits().getAt(0).getSortValues()[0], closeTo(GeoDistance.ARC.calculate(2, 2, 3, 2, DistanceUnit.METERS), 10d));
assertThat((Double) searchResponse.getHits().getAt(1).getSortValues()[0], closeTo(GeoDistance.ARC.calculate(2, 1, 5, 1, DistanceUnit.METERS), 10d));
searchResponse = client().prepareSearch().setQuery(matchAllQuery()).addSort(new GeoDistanceSortBuilder(LOCATION_FIELD, q).sortMode(SortMode.MIN).order(SortOrder.DESC)).get();
assertOrderedSearchHits(searchResponse, "d2", "d1");
assertThat((Double) searchResponse.getHits().getAt(0).getSortValues()[0], closeTo(GeoDistance.ARC.calculate(2, 1, 5, 1, DistanceUnit.METERS), 10d));
assertThat((Double) searchResponse.getHits().getAt(1).getSortValues()[0], closeTo(GeoDistance.ARC.calculate(2, 2, 3, 2, DistanceUnit.METERS), 10d));
searchResponse = client().prepareSearch().setQuery(matchAllQuery()).addSort(new GeoDistanceSortBuilder(LOCATION_FIELD, q).sortMode(SortMode.MAX).order(SortOrder.ASC)).get();
assertOrderedSearchHits(searchResponse, "d1", "d2");
assertThat((Double) searchResponse.getHits().getAt(0).getSortValues()[0], closeTo(GeoDistance.ARC.calculate(2, 2, 4, 1, DistanceUnit.METERS), 10d));
assertThat((Double) searchResponse.getHits().getAt(1).getSortValues()[0], closeTo(GeoDistance.ARC.calculate(2, 1, 6, 2, DistanceUnit.METERS), 10d));
searchResponse = client().prepareSearch().setQuery(matchAllQuery()).addSort(new GeoDistanceSortBuilder(LOCATION_FIELD, q).sortMode(SortMode.MAX).order(SortOrder.DESC)).get();
assertOrderedSearchHits(searchResponse, "d2", "d1");
assertThat((Double) searchResponse.getHits().getAt(0).getSortValues()[0], closeTo(GeoDistance.ARC.calculate(2, 1, 6, 2, DistanceUnit.METERS), 10d));
assertThat((Double) searchResponse.getHits().getAt(1).getSortValues()[0], closeTo(GeoDistance.ARC.calculate(2, 2, 4, 1, DistanceUnit.METERS), 10d));
}
Aggregations