use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.
the class GeoPolygonIT method testSimpleUnclosedPolygon.
public void testSimpleUnclosedPolygon() throws Exception {
List<GeoPoint> points = new ArrayList<>();
points.add(new GeoPoint(40.7, -74.0));
points.add(new GeoPoint(40.7, -74.1));
points.add(new GeoPoint(40.8, -74.1));
points.add(new GeoPoint(40.8, -74.0));
SearchResponse searchResponse = // from NY
client().prepareSearch("test").setQuery(boolQuery().must(geoPolygonQuery("location", points))).execute().actionGet();
assertHitCount(searchResponse, 4);
assertThat(searchResponse.getHits().getHits().length, equalTo(4));
for (SearchHit hit : searchResponse.getHits()) {
assertThat(hit.getId(), anyOf(equalTo("1"), equalTo("3"), equalTo("4"), equalTo("5")));
}
}
use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.
the class SearchAfterBuilderTests method testBadTypes.
/**
* Explicitly tests what you can't list as a sortValue. What you can list is tested by {@link #randomSearchAfterBuilder()}.
*/
public void testBadTypes() throws IOException {
randomSearchFromBuilderWithSortValueThrows(new Object());
randomSearchFromBuilderWithSortValueThrows(new GeoPoint(0, 0));
randomSearchFromBuilderWithSortValueThrows(randomSearchAfterBuilder());
randomSearchFromBuilderWithSortValueThrows(this);
}
use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.
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.randomVersionBetween(random(), Version.V_2_0_0, Version.CURRENT);
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", "type", "d1").setSource(d1Builder), client().prepareIndex("index", "type", "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)).execute().actionGet();
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)).execute().actionGet();
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)).execute().actionGet();
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)).execute().actionGet();
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));
}
use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.
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.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.
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", "type", "d1").setSource(jsonBuilder().startObject().startObject(LOCATION_FIELD).field("lat", 1).field("lon", 1).endObject().endObject()), client().prepareIndex("index", "type", "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)).execute().actionGet();
checkCorrectSortOrderForGeoSort(searchResponse);
geoDistanceSortBuilder = new GeoDistanceSortBuilder(LOCATION_FIELD, new GeoPoint(2, 2));
searchResponse = client().prepareSearch().setQuery(matchAllQuery()).addSort(geoDistanceSortBuilder.sortMode(SortMode.MIN).order(SortOrder.ASC)).execute().actionGet();
checkCorrectSortOrderForGeoSort(searchResponse);
geoDistanceSortBuilder = new GeoDistanceSortBuilder(LOCATION_FIELD, 2, 2);
searchResponse = client().prepareSearch().setQuery(matchAllQuery()).addSort(geoDistanceSortBuilder.sortMode(SortMode.MIN).order(SortOrder.ASC)).execute().actionGet();
checkCorrectSortOrderForGeoSort(searchResponse);
searchResponse = client().prepareSearch().setSource(new SearchSourceBuilder().sort(SortBuilders.geoDistanceSort(LOCATION_FIELD, 2.0, 2.0))).execute().actionGet();
checkCorrectSortOrderForGeoSort(searchResponse);
searchResponse = client().prepareSearch().setSource(new SearchSourceBuilder().sort(SortBuilders.geoDistanceSort(LOCATION_FIELD, "s037ms06g7h0"))).execute().actionGet();
checkCorrectSortOrderForGeoSort(searchResponse);
searchResponse = client().prepareSearch().setSource(new SearchSourceBuilder().sort(SortBuilders.geoDistanceSort(LOCATION_FIELD, 2.0, 2.0))).execute().actionGet();
checkCorrectSortOrderForGeoSort(searchResponse);
searchResponse = client().prepareSearch().setSource(new SearchSourceBuilder().sort(SortBuilders.geoDistanceSort(LOCATION_FIELD, 2.0, 2.0).validation(GeoValidationMethod.COERCE))).execute().actionGet();
checkCorrectSortOrderForGeoSort(searchResponse);
}
Aggregations