.startObject("location").field("lat", 40.65).field("lon", -73.95).endObject()
.endObject()).execute().actionGet();
client.admin().indices().prepareRefresh().execute().actionGet();
SearchResponse searchResponse = client.prepareSearch() // from NY
.setQuery(filteredQuery(matchAllQuery(), geoDistanceFilter("location").distance("3km").point(40.7143528, -74.0059731)))
.execute().actionGet();
assertThat(searchResponse.hits().getTotalHits(), equalTo(5l));
assertThat(searchResponse.hits().hits().length, equalTo(5));
for (SearchHit hit : searchResponse.hits()) {
assertThat(hit.id(), anyOf(equalTo("1"), equalTo("3"), equalTo("4"), equalTo("5"), equalTo("6")));
}
searchResponse = client.prepareSearch() // from NY
.setQuery(filteredQuery(matchAllQuery(), geoDistanceFilter("location").distance("2km").point(40.7143528, -74.0059731)))
.execute().actionGet();
assertThat(searchResponse.hits().getTotalHits(), equalTo(4l));
assertThat(searchResponse.hits().hits().length, equalTo(4));
for (SearchHit hit : searchResponse.hits()) {
assertThat(hit.id(), anyOf(equalTo("1"), equalTo("3"), equalTo("4"), equalTo("5")));
}
searchResponse = client.prepareSearch() // from NY
.setQuery(filteredQuery(matchAllQuery(), geoDistanceFilter("location").distance("1.242mi").point(40.7143528, -74.0059731)))
.execute().actionGet();
assertThat(searchResponse.hits().getTotalHits(), equalTo(4l));
assertThat(searchResponse.hits().hits().length, equalTo(4));
for (SearchHit hit : searchResponse.hits()) {
assertThat(hit.id(), anyOf(equalTo("1"), equalTo("3"), equalTo("4"), equalTo("5")));
}
searchResponse = client.prepareSearch() // from NY
.setQuery(filteredQuery(matchAllQuery(), geoDistanceRangeFilter("location").from("1.0km").to("2.0km").point(40.7143528, -74.0059731)))
.execute().actionGet();
assertThat(searchResponse.hits().getTotalHits(), equalTo(2l));
assertThat(searchResponse.hits().hits().length, equalTo(2));
for (SearchHit hit : searchResponse.hits()) {
assertThat(hit.id(), anyOf(equalTo("4"), equalTo("5")));
}
// SORTING
searchResponse = client.prepareSearch().setQuery(matchAllQuery())
.addSort(SortBuilders.geoDistanceSort("location").point(40.7143528, -74.0059731).order(SortOrder.ASC))
.execute().actionGet();
assertThat(searchResponse.hits().getTotalHits(), equalTo(7l));
assertThat(searchResponse.hits().hits().length, equalTo(7));
assertThat(searchResponse.hits().getAt(0).id(), equalTo("1"));
assertThat(searchResponse.hits().getAt(1).id(), equalTo("3"));
assertThat(searchResponse.hits().getAt(2).id(), equalTo("4"));
assertThat(searchResponse.hits().getAt(3).id(), equalTo("5"));
assertThat(searchResponse.hits().getAt(4).id(), equalTo("6"));
assertThat(searchResponse.hits().getAt(5).id(), equalTo("2"));
assertThat(searchResponse.hits().getAt(6).id(), equalTo("7"));
searchResponse = client.prepareSearch().setQuery(matchAllQuery())
.addSort(SortBuilders.geoDistanceSort("location").point(40.7143528, -74.0059731).order(SortOrder.DESC))
.execute().actionGet();
assertThat(searchResponse.hits().getTotalHits(), equalTo(7l));
assertThat(searchResponse.hits().hits().length, equalTo(7));
assertThat(searchResponse.hits().getAt(6).id(), equalTo("1"));
assertThat(searchResponse.hits().getAt(5).id(), equalTo("3"));
assertThat(searchResponse.hits().getAt(4).id(), equalTo("4"));
assertThat(searchResponse.hits().getAt(3).id(), equalTo("5"));
assertThat(searchResponse.hits().getAt(2).id(), equalTo("6"));
assertThat(searchResponse.hits().getAt(1).id(), equalTo("2"));
assertThat(searchResponse.hits().getAt(0).id(), equalTo("7"));
}