Search in sources :

Example 81 with GeoPoint

use of org.opensearch.common.geo.GeoPoint in project OpenSearch by opensearch-project.

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.randomIndexCompatibleVersion(random());
    Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, version).build();
    assertAcked(prepareCreate("index").setSettings(settings).setMapping(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").setId("d1").setSource(d1Builder), client().prepareIndex("index").setId("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)).get();
    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)).get();
    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)).get();
    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)).get();
    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));
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) Version(org.opensearch.Version) Settings(org.opensearch.common.settings.Settings) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 82 with GeoPoint

use of org.opensearch.common.geo.GeoPoint in project OpenSearch by opensearch-project.

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();
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) ArrayList(java.util.ArrayList) GeoPoint(org.opensearch.common.geo.GeoPoint)

Example 83 with GeoPoint

use of org.opensearch.common.geo.GeoPoint in project OpenSearch by opensearch-project.

the class SimpleSortIT method testSortMinValueScript.

public void testSortMinValueScript() throws IOException {
    String mapping = Strings.toString(jsonBuilder().startObject().startObject("properties").startObject("lvalue").field("type", "long").endObject().startObject("dvalue").field("type", "double").endObject().startObject("svalue").field("type", "keyword").endObject().startObject("gvalue").field("type", "geo_point").endObject().endObject().endObject());
    assertAcked(prepareCreate("test").setMapping(mapping));
    ensureGreen();
    for (int i = 0; i < 10; i++) {
        client().prepareIndex("test").setId("" + i).setSource(jsonBuilder().startObject().field("ord", i).array("svalue", new String[] { "" + i, "" + (i + 1), "" + (i + 2) }).array("lvalue", new long[] { i, i + 1, i + 2 }).array("dvalue", new double[] { i, i + 1, i + 2 }).startObject("gvalue").field("lat", (double) i + 1).field("lon", (double) i).endObject().endObject()).get();
    }
    for (int i = 10; i < 20; i++) {
        // add some docs that don't have values in those fields
        client().prepareIndex("test").setId("" + i).setSource(jsonBuilder().startObject().field("ord", i).endObject()).get();
    }
    client().admin().indices().prepareRefresh("test").get();
    // test the long values
    SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).addScriptField("min", new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "get min long", Collections.emptyMap())).addSort(SortBuilders.fieldSort("ord").order(SortOrder.ASC).unmappedType("long")).setSize(10).get();
    assertNoFailures(searchResponse);
    assertHitCount(searchResponse, 20L);
    for (int i = 0; i < 10; i++) {
        SearchHit searchHit = searchResponse.getHits().getAt(i);
        assertThat("res: " + i + " id: " + searchHit.getId(), searchHit.field("min").getValue(), equalTo((long) i));
    }
    // test the double values
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).addScriptField("min", new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "get min double", Collections.emptyMap())).addSort(SortBuilders.fieldSort("ord").order(SortOrder.ASC).unmappedType("long")).setSize(10).get();
    assertNoFailures(searchResponse);
    assertHitCount(searchResponse, 20L);
    for (int i = 0; i < 10; i++) {
        SearchHit searchHit = searchResponse.getHits().getAt(i);
        assertThat("res: " + i + " id: " + searchHit.getId(), searchHit.field("min").getValue(), equalTo((double) i));
    }
    // test the string values
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).addScriptField("min", new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "get min string", Collections.emptyMap())).addSort(SortBuilders.fieldSort("ord").order(SortOrder.ASC).unmappedType("long")).setSize(10).get();
    assertNoFailures(searchResponse);
    assertHitCount(searchResponse, 20L);
    for (int i = 0; i < 10; i++) {
        SearchHit searchHit = searchResponse.getHits().getAt(i);
        assertThat("res: " + i + " id: " + searchHit.getId(), searchHit.field("min").getValue(), equalTo(i));
    }
    // test the geopoint values
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).addScriptField("min", new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "get min geopoint lon", Collections.emptyMap())).addSort(SortBuilders.fieldSort("ord").order(SortOrder.ASC).unmappedType("long")).setSize(10).get();
    assertNoFailures(searchResponse);
    assertHitCount(searchResponse, 20L);
    for (int i = 0; i < 10; i++) {
        SearchHit searchHit = searchResponse.getHits().getAt(i);
        assertThat("res: " + i + " id: " + searchHit.getId(), searchHit.field("min").getValue(), closeTo(i, GeoUtils.TOLERANCE));
    }
}
Also used : Script(org.opensearch.script.Script) SearchHit(org.opensearch.search.SearchHit) Matchers.containsString(org.hamcrest.Matchers.containsString) GeoPoint(org.opensearch.common.geo.GeoPoint) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 84 with GeoPoint

use of org.opensearch.common.geo.GeoPoint in project OpenSearch by opensearch-project.

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))).get();
    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")));
    }
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) SearchHit(org.opensearch.search.SearchHit) ArrayList(java.util.ArrayList) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 85 with GeoPoint

use of org.opensearch.common.geo.GeoPoint in project OpenSearch by opensearch-project.

the class GeoPolygonIT method testFieldAlias.

public void testFieldAlias() {
    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("alias", points))).get();
    assertHitCount(searchResponse, 4);
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) ArrayList(java.util.ArrayList) SearchResponse(org.opensearch.action.search.SearchResponse)

Aggregations

GeoPoint (org.opensearch.common.geo.GeoPoint)151 SearchResponse (org.opensearch.action.search.SearchResponse)42 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)27 ArrayList (java.util.ArrayList)23 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)19 Matchers.containsString (org.hamcrest.Matchers.containsString)11 XContentParser (org.opensearch.common.xcontent.XContentParser)10 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)10 LatLonPoint (org.apache.lucene.document.LatLonPoint)8 Version (org.opensearch.Version)8 IOException (java.io.IOException)7 LatLonDocValuesField (org.apache.lucene.document.LatLonDocValuesField)7 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)7 OpenSearchParseException (org.opensearch.OpenSearchParseException)7 Range (org.opensearch.search.aggregations.bucket.range.Range)7 Bucket (org.opensearch.search.aggregations.bucket.range.Range.Bucket)6 Map (java.util.Map)5 Document (org.apache.lucene.document.Document)5 Directory (org.apache.lucene.store.Directory)5 RandomIndexWriter (org.apache.lucene.tests.index.RandomIndexWriter)5