Search in sources :

Example 21 with GeoPoint

use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.

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.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(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", "type", "d1").setSource(d1Builder), client().prepareIndex("index", "type", "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)).execute().actionGet();
    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)).execute().actionGet();
    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));
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) Version(org.elasticsearch.Version) Settings(org.elasticsearch.common.settings.Settings) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 22 with GeoPoint

use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.

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.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(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", "type", "d1").setSource(d1Builder), client().prepareIndex("index", "type", "d2").setSource(d2Builder));
    List<String> qHashes = new ArrayList<>();
    List<GeoPoint> qPoints = new ArrayList<>();
    createQPoints(qHashes, qPoints);
    GeoDistanceSortBuilder geoDistanceSortBuilder = null;
    for (int i = 0; i < 4; i++) {
        int at = randomInt(3 - i);
        if (randomBoolean()) {
            if (geoDistanceSortBuilder == null) {
                geoDistanceSortBuilder = new GeoDistanceSortBuilder(LOCATION_FIELD, qHashes.get(at));
            } else {
                geoDistanceSortBuilder.geohashes(qHashes.get(at));
            }
        } else {
            if (geoDistanceSortBuilder == null) {
                geoDistanceSortBuilder = new GeoDistanceSortBuilder(LOCATION_FIELD, qPoints.get(at));
            } else {
                geoDistanceSortBuilder.points(qPoints.get(at));
            }
        }
        qHashes.remove(at);
        qPoints.remove(at);
    }
    SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).addSort(geoDistanceSortBuilder.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.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)).execute().actionGet();
    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));
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) Version(org.elasticsearch.Version) ArrayList(java.util.ArrayList) Settings(org.elasticsearch.common.settings.Settings) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) GeoPoint(org.elasticsearch.common.geo.GeoPoint) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 23 with GeoPoint

use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.

the class GeoDistanceSortBuilderIT method createQPoints.

protected void createQPoints(List<String> qHashes, List<GeoPoint> qPoints) {
    GeoPoint[] qp = { new GeoPoint(2, 1), new GeoPoint(2, 2), new GeoPoint(2, 3), new GeoPoint(2, 4) };
    qPoints.addAll(Arrays.asList(qp));
    String[] qh = { "s02equ04ven0", "s037ms06g7h0", "s065kk0dc540", "s06g7h0dyg00" };
    qHashes.addAll(Arrays.asList(qh));
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint)

Example 24 with GeoPoint

use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.

the class GeoDistanceSortBuilderTests method points.

private static GeoPoint[] points(GeoPoint[] original) {
    GeoPoint[] result = null;
    while (result == null || Arrays.deepEquals(original, result)) {
        int count = randomIntBetween(1, 10);
        result = new GeoPoint[count];
        for (int i = 0; i < count; i++) {
            result[i] = RandomGeoGenerator.randomPoint(random());
        }
    }
    return result;
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) GeoPoint(org.elasticsearch.common.geo.GeoPoint)

Example 25 with GeoPoint

use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.

the class ContextCompletionSuggestSearchIT method testGeoField.

public void testGeoField() throws Exception {
    //        Version version = VersionUtils.randomVersionBetween(random(), Version.V_2_0_0, Version.V_5_0_0_alpha5);
    //        Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build();
    XContentBuilder mapping = jsonBuilder();
    mapping.startObject();
    mapping.startObject(TYPE);
    mapping.startObject("properties");
    mapping.startObject("pin");
    mapping.field("type", "geo_point");
    mapping.endObject();
    mapping.startObject(FIELD);
    mapping.field("type", "completion");
    mapping.field("analyzer", "simple");
    mapping.startArray("contexts");
    mapping.startObject();
    mapping.field("name", "st");
    mapping.field("type", "geo");
    mapping.field("path", "pin");
    mapping.field("precision", 5);
    mapping.endObject();
    mapping.endArray();
    mapping.endObject();
    mapping.endObject();
    mapping.endObject();
    mapping.endObject();
    assertAcked(prepareCreate(INDEX).addMapping(TYPE, mapping));
    XContentBuilder source1 = jsonBuilder().startObject().latlon("pin", 52.529172, 13.407333).startObject(FIELD).array("input", "Hotel Amsterdam in Berlin").endObject().endObject();
    client().prepareIndex(INDEX, TYPE, "1").setSource(source1).execute().actionGet();
    XContentBuilder source2 = jsonBuilder().startObject().latlon("pin", 52.363389, 4.888695).startObject(FIELD).array("input", "Hotel Berlin in Amsterdam").endObject().endObject();
    client().prepareIndex(INDEX, TYPE, "2").setSource(source2).execute().actionGet();
    refresh();
    String suggestionName = randomAsciiOfLength(10);
    CompletionSuggestionBuilder context = SuggestBuilders.completionSuggestion(FIELD).text("h").size(10).contexts(Collections.singletonMap("st", Collections.singletonList(GeoQueryContext.builder().setGeoPoint(new GeoPoint(52.52, 13.4)).build())));
    SearchResponse searchResponse = client().prepareSearch(INDEX).suggest(new SuggestBuilder().addSuggestion(suggestionName, context)).get();
    assertEquals(searchResponse.getSuggest().size(), 1);
    assertEquals("Hotel Amsterdam in Berlin", searchResponse.getSuggest().getSuggestion(suggestionName).iterator().next().getOptions().iterator().next().getText().string());
}
Also used : CompletionSuggestionBuilder(org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder) GeoPoint(org.elasticsearch.common.geo.GeoPoint) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

GeoPoint (org.elasticsearch.common.geo.GeoPoint)122 SearchResponse (org.elasticsearch.action.search.SearchResponse)40 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)27 ArrayList (java.util.ArrayList)20 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)15 XContentParser (org.elasticsearch.common.xcontent.XContentParser)9 GeoBounds (org.elasticsearch.search.aggregations.metrics.geobounds.GeoBounds)9 Range (org.elasticsearch.search.aggregations.bucket.range.Range)8 GeoCentroid (org.elasticsearch.search.aggregations.metrics.geocentroid.GeoCentroid)8 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)7 Version (org.elasticsearch.Version)7 Bucket (org.elasticsearch.search.aggregations.bucket.range.Range.Bucket)7 Settings (org.elasticsearch.common.settings.Settings)6 MultiGeoPointValues (org.elasticsearch.index.fielddata.MultiGeoPointValues)6 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)5 ParsingException (org.elasticsearch.common.ParsingException)5 SearchHit (org.elasticsearch.search.SearchHit)5 Test (org.testng.annotations.Test)5 HashSet (java.util.HashSet)4 DistanceUnit (org.elasticsearch.common.unit.DistanceUnit)4