use of org.opensearch.common.geo.GeoPoint in project OpenSearch by opensearch-project.
the class GeoBoundsIT method testPartiallyUnmapped.
public void testPartiallyUnmapped() throws Exception {
SearchResponse response = client().prepareSearch(IDX_NAME, UNMAPPED_IDX_NAME).addAggregation(geoBounds(aggName).field(SINGLE_VALUED_FIELD_NAME).wrapLongitude(false)).get();
assertSearchResponse(response);
GeoBounds geoBounds = response.getAggregations().get(aggName);
assertThat(geoBounds, notNullValue());
assertThat(geoBounds.getName(), equalTo(aggName));
GeoPoint topLeft = geoBounds.topLeft();
GeoPoint bottomRight = geoBounds.bottomRight();
assertThat(topLeft.lat(), closeTo(singleTopLeft.lat(), GEOHASH_TOLERANCE));
assertThat(topLeft.lon(), closeTo(singleTopLeft.lon(), GEOHASH_TOLERANCE));
assertThat(bottomRight.lat(), closeTo(singleBottomRight.lat(), GEOHASH_TOLERANCE));
assertThat(bottomRight.lon(), closeTo(singleBottomRight.lon(), GEOHASH_TOLERANCE));
}
use of org.opensearch.common.geo.GeoPoint in project OpenSearch by opensearch-project.
the class ContextCompletionSuggestSearchIT method testGeoFiltering.
public void testGeoFiltering() 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<>();
GeoPoint[] geoPoints = new GeoPoint[] { new GeoPoint("ezs42e44yx96"), new GeoPoint("u4pruydqqvj8") };
for (int i = 0; i < numDocs; i++) {
XContentBuilder source = jsonBuilder().startObject().startObject(FIELD).field("input", "suggestion" + i).field("weight", i + 1).startObject("contexts").field("geo", (i % 2 == 0) ? geoPoints[0].getGeohash() : geoPoints[1].getGeohash()).endObject().endObject().endObject();
indexRequestBuilders.add(client().prepareIndex(INDEX).setId("" + i).setSource(source));
}
indexRandom(true, indexRequestBuilders);
CompletionSuggestionBuilder geoFilteringPrefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg").contexts(Collections.singletonMap("geo", Collections.singletonList(GeoQueryContext.builder().setGeoPoint(new GeoPoint(geoPoints[0])).build())));
assertSuggestions("foo", geoFilteringPrefix, "suggestion8", "suggestion6", "suggestion4", "suggestion2", "suggestion0");
}
use of org.opensearch.common.geo.GeoPoint in project OpenSearch by opensearch-project.
the class GeoDistanceSortBuilderIT method testManyToManyGeoPointsWithDifferentFormats.
public void testManyToManyGeoPointsWithDifferentFormats() throws ExecutionException, InterruptedException, IOException {
/**
* q d1 d2
* |4 o| x | x
* | | |
* |3 o| x | x
* | | |
* |2 o| x | x
* | | |
* |1 o|x |x
* |______________________
* 1 2 3 4 5 6
*/
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(2.5, 1), new GeoPoint(2.75, 2), new GeoPoint(3, 3), new GeoPoint(3.25, 4) };
createShuffeldJSONArray(d1Builder, d1Points);
XContentBuilder d2Builder = jsonBuilder();
GeoPoint[] d2Points = { new GeoPoint(4.5, 1), new GeoPoint(4.75, 2), new GeoPoint(5, 3), new GeoPoint(5.25, 4) };
createShuffeldJSONArray(d2Builder, d2Points);
indexRandom(true, client().prepareIndex("index").setId("d1").setSource(d1Builder), client().prepareIndex("index").setId("d2").setSource(d2Builder));
List<GeoPoint> qPoints = Arrays.asList(new GeoPoint(2, 1), new GeoPoint(2, 2), new GeoPoint(2, 3), new GeoPoint(2, 4));
Collections.shuffle(qPoints, random());
GeoDistanceSortBuilder geoDistanceSortBuilder = null;
for (GeoPoint point : qPoints) {
if (geoDistanceSortBuilder == null) {
geoDistanceSortBuilder = new GeoDistanceSortBuilder(LOCATION_FIELD, point);
} else {
geoDistanceSortBuilder.points(point);
}
}
SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).addSort(geoDistanceSortBuilder.sortMode(SortMode.MIN).order(SortOrder.ASC)).get();
assertOrderedSearchHits(searchResponse, "d1", "d2");
assertThat((Double) searchResponse.getHits().getAt(0).getSortValues()[0], closeTo(GeoDistance.ARC.calculate(2.5, 1, 2, 1, DistanceUnit.METERS), 1.e-1));
assertThat((Double) searchResponse.getHits().getAt(1).getSortValues()[0], closeTo(GeoDistance.ARC.calculate(4.5, 1, 2, 1, DistanceUnit.METERS), 1.e-1));
searchResponse = client().prepareSearch().setQuery(matchAllQuery()).addSort(geoDistanceSortBuilder.sortMode(SortMode.MAX).order(SortOrder.ASC)).get();
assertOrderedSearchHits(searchResponse, "d1", "d2");
assertThat((Double) searchResponse.getHits().getAt(0).getSortValues()[0], closeTo(GeoDistance.ARC.calculate(3.25, 4, 2, 1, DistanceUnit.METERS), 1.e-1));
assertThat((Double) searchResponse.getHits().getAt(1).getSortValues()[0], closeTo(GeoDistance.ARC.calculate(5.25, 4, 2, 1, DistanceUnit.METERS), 1.e-1));
}
use of org.opensearch.common.geo.GeoPoint in project OpenSearch by opensearch-project.
the class GeoDistanceSortBuilderIT method testSingeToManyAvgMedian.
public void testSingeToManyAvgMedian() throws ExecutionException, InterruptedException, IOException {
/**
* q = (0, 0)
*
* d1 = (0, 1), (0, 4), (0, 10); so avg. distance is 5, median distance is 4
* d2 = (0, 1), (0, 5), (0, 6); so avg. distance is 4, median distance is 5
*/
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(0, 1), new GeoPoint(0, 4), new GeoPoint(0, 10) };
createShuffeldJSONArray(d1Builder, d1Points);
XContentBuilder d2Builder = jsonBuilder();
GeoPoint[] d2Points = { new GeoPoint(0, 1), new GeoPoint(0, 5), new GeoPoint(0, 6) };
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(0, 0);
SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).addSort(new GeoDistanceSortBuilder(LOCATION_FIELD, q).sortMode(SortMode.AVG).order(SortOrder.ASC)).get();
assertOrderedSearchHits(searchResponse, "d2", "d1");
assertThat((Double) searchResponse.getHits().getAt(0).getSortValues()[0], closeTo(GeoDistance.ARC.calculate(0, 0, 0, 4, DistanceUnit.METERS), 10d));
assertThat((Double) searchResponse.getHits().getAt(1).getSortValues()[0], closeTo(GeoDistance.ARC.calculate(0, 0, 0, 5, DistanceUnit.METERS), 10d));
searchResponse = client().prepareSearch().setQuery(matchAllQuery()).addSort(new GeoDistanceSortBuilder(LOCATION_FIELD, q).sortMode(SortMode.MEDIAN).order(SortOrder.ASC)).get();
assertOrderedSearchHits(searchResponse, "d1", "d2");
assertThat((Double) searchResponse.getHits().getAt(0).getSortValues()[0], closeTo(GeoDistance.ARC.calculate(0, 0, 0, 4, DistanceUnit.METERS), 10d));
assertThat((Double) searchResponse.getHits().getAt(1).getSortValues()[0], closeTo(GeoDistance.ARC.calculate(0, 0, 0, 5, DistanceUnit.METERS), 10d));
}
use of org.opensearch.common.geo.GeoPoint in project OpenSearch by opensearch-project.
the class DecayFunctionScoreIT method testParseGeoPoint.
public void testParseGeoPoint() throws Exception {
assertAcked(prepareCreate("test").addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("test").field("type", "text").endObject().startObject("loc").field("type", "geo_point").endObject().endObject().endObject().endObject()));
client().prepareIndex().setId("1").setIndex("test").setSource(jsonBuilder().startObject().field("test", "value").startObject("loc").field("lat", 20).field("lon", 11).endObject().endObject()).setRefreshPolicy(IMMEDIATE).get();
FunctionScoreQueryBuilder baseQuery = functionScoreQuery(constantScoreQuery(termQuery("test", "value")), ScoreFunctionBuilders.weightFactorFunction(randomIntBetween(1, 10)));
GeoPoint point = new GeoPoint(20, 11);
ActionFuture<SearchResponse> response = client().search(searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(searchSource().query(functionScoreQuery(baseQuery, gaussDecayFunction("loc", point, "1000km")).boostMode(CombineFunction.REPLACE))));
SearchResponse sr = response.actionGet();
SearchHits sh = sr.getHits();
assertThat(sh.getTotalHits().value, equalTo((long) (1)));
assertThat(sh.getAt(0).getId(), equalTo("1"));
assertThat((double) sh.getAt(0).getScore(), closeTo(1.0, 1.e-5));
// this is equivalent to new GeoPoint(20, 11); just flipped so scores must be same
float[] coords = { 11, 20 };
response = client().search(searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(searchSource().query(functionScoreQuery(baseQuery, gaussDecayFunction("loc", coords, "1000km")).boostMode(CombineFunction.REPLACE))));
sr = response.actionGet();
sh = sr.getHits();
assertThat(sh.getTotalHits().value, equalTo((long) (1)));
assertThat(sh.getAt(0).getId(), equalTo("1"));
assertThat((double) sh.getAt(0).getScore(), closeTo(1.0f, 1.e-5));
}
Aggregations