use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.
the class GeoCentroidIT method testSingleValuedField.
public void testSingleValuedField() throws Exception {
SearchResponse response = client().prepareSearch(IDX_NAME).setQuery(matchAllQuery()).addAggregation(geoCentroid(aggName).field(SINGLE_VALUED_FIELD_NAME)).execute().actionGet();
assertSearchResponse(response);
GeoCentroid geoCentroid = response.getAggregations().get(aggName);
assertThat(geoCentroid, notNullValue());
assertThat(geoCentroid.getName(), equalTo(aggName));
GeoPoint centroid = geoCentroid.centroid();
assertThat(centroid.lat(), closeTo(singleCentroid.lat(), GEOHASH_TOLERANCE));
assertThat(centroid.lon(), closeTo(singleCentroid.lon(), GEOHASH_TOLERANCE));
}
use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.
the class GeoBoundsAggregatorTests method testRandom.
public void testRandom() throws Exception {
double top = Double.NEGATIVE_INFINITY;
double bottom = Double.POSITIVE_INFINITY;
double posLeft = Double.POSITIVE_INFINITY;
double posRight = Double.NEGATIVE_INFINITY;
double negLeft = Double.POSITIVE_INFINITY;
double negRight = Double.NEGATIVE_INFINITY;
int numDocs = randomIntBetween(50, 100);
try (Directory dir = newDirectory();
RandomIndexWriter w = new RandomIndexWriter(random(), dir)) {
for (int i = 0; i < numDocs; i++) {
Document doc = new Document();
int numValues = randomIntBetween(1, 5);
for (int j = 0; j < numValues; j++) {
GeoPoint point = RandomGeoGenerator.randomPoint(random());
if (point.getLat() > top) {
top = point.getLat();
}
if (point.getLat() < bottom) {
bottom = point.getLat();
}
if (point.getLon() >= 0 && point.getLon() < posLeft) {
posLeft = point.getLon();
}
if (point.getLon() >= 0 && point.getLon() > posRight) {
posRight = point.getLon();
}
if (point.getLon() < 0 && point.getLon() < negLeft) {
negLeft = point.getLon();
}
if (point.getLon() < 0 && point.getLon() > negRight) {
negRight = point.getLon();
}
doc.add(new LatLonDocValuesField("field", point.getLat(), point.getLon()));
}
w.addDocument(doc);
}
GeoBoundsAggregationBuilder aggBuilder = new GeoBoundsAggregationBuilder("my_agg").field("field").wrapLongitude(false);
MappedFieldType fieldType = new GeoPointFieldMapper.GeoPointFieldType();
fieldType.setHasDocValues(true);
fieldType.setName("field");
try (IndexReader reader = w.getReader()) {
IndexSearcher searcher = new IndexSearcher(reader);
InternalGeoBounds bounds = search(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType);
assertThat(bounds.top, closeTo(top, GEOHASH_TOLERANCE));
assertThat(bounds.bottom, closeTo(bottom, GEOHASH_TOLERANCE));
assertThat(bounds.posLeft, closeTo(posLeft, GEOHASH_TOLERANCE));
assertThat(bounds.posRight, closeTo(posRight, GEOHASH_TOLERANCE));
assertThat(bounds.negRight, closeTo(negRight, GEOHASH_TOLERANCE));
assertThat(bounds.negLeft, closeTo(negLeft, GEOHASH_TOLERANCE));
}
}
}
use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.
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().setType("type1").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(), 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(), equalTo((long) (1)));
assertThat(sh.getAt(0).getId(), equalTo("1"));
assertThat((double) sh.getAt(0).getScore(), closeTo(1.0f, 1.e-5));
}
use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.
the class HighlighterSearchIT method testGeoFieldHighlightingWhenQueryGetsRewritten.
public void testGeoFieldHighlightingWhenQueryGetsRewritten() throws IOException {
// same as above but in this example the query gets rewritten during highlighting
// see https://github.com/elastic/elasticsearch/issues/17537#issuecomment-244939633
XContentBuilder mappings = jsonBuilder();
mappings.startObject();
mappings.startObject("jobs").startObject("properties").startObject("loc").field("type", "geo_point").endObject().startObject("jd").field("type", "text").endObject().endObject().endObject();
mappings.endObject();
assertAcked(prepareCreate("test").addMapping("jobs", mappings));
ensureYellow();
client().prepareIndex("test", "jobs", "1").setSource(jsonBuilder().startObject().field("jd", "some आवश्यकता है- आर्य समाज अनाथालय, 68 सिविल लाइन्स, बरेली को एक पुरूष" + " रस text").field("loc", "12.934059,77.610741").endObject()).get();
refresh();
QueryBuilder query = QueryBuilders.functionScoreQuery(QueryBuilders.boolQuery().filter(QueryBuilders.geoBoundingBoxQuery("loc").setCorners(new GeoPoint(48.934059, 41.610741), new GeoPoint(-23.065941, 113.610741))));
SearchResponse search = client().prepareSearch().setSource(new SearchSourceBuilder().query(query).highlighter(new HighlightBuilder().highlighterType("plain").field("jd"))).get();
assertNoFailures(search);
assertThat(search.getHits().getTotalHits(), equalTo(1L));
}
use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.
the class GeoPolygonIT method testSimplePolygon.
public void testSimplePolygon() 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));
points.add(new GeoPoint(40.7, -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")));
}
}
Aggregations