Search in sources :

Example 1 with Version

use of org.elasticsearch.Version in project elasticsearch by elastic.

the class GeoBoundingBoxIT method testSimpleBoundingBoxTest.

public void testSimpleBoundingBoxTest() throws Exception {
    Version version = VersionUtils.randomVersionBetween(random(), Version.V_2_0_0, Version.CURRENT);
    Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build();
    XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("location").field("type", "geo_point");
    xContentBuilder.endObject().endObject().endObject().endObject();
    assertAcked(prepareCreate("test").setSettings(settings).addMapping("type1", xContentBuilder));
    ensureGreen();
    client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject().field("name", "New York").startObject("location").field("lat", 40.7143528).field("lon", -74.0059731).endObject().endObject()).execute().actionGet();
    // to NY: 5.286 km
    client().prepareIndex("test", "type1", "2").setSource(jsonBuilder().startObject().field("name", "Times Square").startObject("location").field("lat", 40.759011).field("lon", -73.9844722).endObject().endObject()).execute().actionGet();
    // to NY: 0.4621 km
    client().prepareIndex("test", "type1", "3").setSource(jsonBuilder().startObject().field("name", "Tribeca").startObject("location").field("lat", 40.718266).field("lon", -74.007819).endObject().endObject()).execute().actionGet();
    // to NY: 1.055 km
    client().prepareIndex("test", "type1", "4").setSource(jsonBuilder().startObject().field("name", "Wall Street").startObject("location").field("lat", 40.7051157).field("lon", -74.0088305).endObject().endObject()).execute().actionGet();
    // to NY: 1.258 km
    client().prepareIndex("test", "type1", "5").setSource(jsonBuilder().startObject().field("name", "Soho").startObject("location").field("lat", 40.7247222).field("lon", -74).endObject().endObject()).execute().actionGet();
    // to NY: 2.029 km
    client().prepareIndex("test", "type1", "6").setSource(jsonBuilder().startObject().field("name", "Greenwich Village").startObject("location").field("lat", 40.731033).field("lon", -73.9962255).endObject().endObject()).execute().actionGet();
    // to NY: 8.572 km
    client().prepareIndex("test", "type1", "7").setSource(jsonBuilder().startObject().field("name", "Brooklyn").startObject("location").field("lat", 40.65).field("lon", -73.95).endObject().endObject()).execute().actionGet();
    client().admin().indices().prepareRefresh().execute().actionGet();
    SearchResponse searchResponse = // from NY
    client().prepareSearch().setQuery(geoBoundingBoxQuery("location").setCorners(40.73, -74.1, 40.717, -73.99)).execute().actionGet();
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L));
    assertThat(searchResponse.getHits().getHits().length, equalTo(2));
    for (SearchHit hit : searchResponse.getHits()) {
        assertThat(hit.getId(), anyOf(equalTo("1"), equalTo("3"), equalTo("5")));
    }
    searchResponse = // from NY
    client().prepareSearch().setQuery(geoBoundingBoxQuery("location").setCorners(40.73, -74.1, 40.717, -73.99).type("indexed")).execute().actionGet();
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L));
    assertThat(searchResponse.getHits().getHits().length, equalTo(2));
    for (SearchHit hit : searchResponse.getHits()) {
        assertThat(hit.getId(), anyOf(equalTo("1"), equalTo("3"), equalTo("5")));
    }
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) Version(org.elasticsearch.Version) Settings(org.elasticsearch.common.settings.Settings) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 2 with Version

use of org.elasticsearch.Version in project elasticsearch by elastic.

the class GeoBoundingBoxIT method testLimit2BoundingBox.

public void testLimit2BoundingBox() throws Exception {
    Version version = VersionUtils.randomVersionBetween(random(), Version.V_2_0_0, Version.CURRENT);
    Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build();
    XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("location").field("type", "geo_point");
    xContentBuilder.endObject().endObject().endObject().endObject();
    assertAcked(prepareCreate("test").setSettings(settings).addMapping("type1", xContentBuilder));
    ensureGreen();
    client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject().field("userid", 880).field("title", "Place in Stockholm").startObject("location").field("lat", 59.328355000000002).field("lon", 18.036842).endObject().endObject()).setRefreshPolicy(IMMEDIATE).get();
    client().prepareIndex("test", "type1", "2").setSource(jsonBuilder().startObject().field("userid", 534).field("title", "Place in Montreal").startObject("location").field("lat", 45.509526999999999).field("lon", -73.570986000000005).endObject().endObject()).setRefreshPolicy(IMMEDIATE).get();
    SearchResponse searchResponse = client().prepareSearch().setQuery(boolQuery().must(termQuery("userid", 880)).filter(geoBoundingBoxQuery("location").setCorners(74.579421999999994, 143.5, -66.668903999999998, 113.96875))).execute().actionGet();
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
    searchResponse = client().prepareSearch().setQuery(boolQuery().must(termQuery("userid", 880)).filter(geoBoundingBoxQuery("location").setCorners(74.579421999999994, 143.5, -66.668903999999998, 113.96875).type("indexed"))).execute().actionGet();
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
    searchResponse = client().prepareSearch().setQuery(boolQuery().must(termQuery("userid", 534)).filter(geoBoundingBoxQuery("location").setCorners(74.579421999999994, 143.5, -66.668903999999998, 113.96875))).execute().actionGet();
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
    searchResponse = client().prepareSearch().setQuery(boolQuery().must(termQuery("userid", 534)).filter(geoBoundingBoxQuery("location").setCorners(74.579421999999994, 143.5, -66.668903999999998, 113.96875).type("indexed"))).execute().actionGet();
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
}
Also used : Version(org.elasticsearch.Version) Settings(org.elasticsearch.common.settings.Settings) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 3 with Version

use of org.elasticsearch.Version in project elasticsearch by elastic.

the class GeoBoundingBoxIT method testCompleteLonRange.

public void testCompleteLonRange() throws Exception {
    Version version = VersionUtils.randomVersionBetween(random(), Version.V_2_0_0, Version.CURRENT);
    Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build();
    XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("location").field("type", "geo_point");
    xContentBuilder.endObject().endObject().endObject().endObject();
    assertAcked(prepareCreate("test").setSettings(settings).addMapping("type1", xContentBuilder));
    ensureGreen();
    client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject().field("userid", 880).field("title", "Place in Stockholm").startObject("location").field("lat", 59.328355000000002).field("lon", 18.036842).endObject().endObject()).setRefreshPolicy(IMMEDIATE).get();
    client().prepareIndex("test", "type1", "2").setSource(jsonBuilder().startObject().field("userid", 534).field("title", "Place in Montreal").startObject("location").field("lat", 45.509526999999999).field("lon", -73.570986000000005).endObject().endObject()).setRefreshPolicy(IMMEDIATE).get();
    SearchResponse searchResponse = client().prepareSearch().setQuery(geoBoundingBoxQuery("location").setValidationMethod(GeoValidationMethod.COERCE).setCorners(50, -180, -50, 180)).execute().actionGet();
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
    searchResponse = client().prepareSearch().setQuery(geoBoundingBoxQuery("location").setValidationMethod(GeoValidationMethod.COERCE).setCorners(50, -180, -50, 180).type("indexed")).execute().actionGet();
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
    searchResponse = client().prepareSearch().setQuery(geoBoundingBoxQuery("location").setValidationMethod(GeoValidationMethod.COERCE).setCorners(90, -180, -90, 180)).execute().actionGet();
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L));
    searchResponse = client().prepareSearch().setQuery(geoBoundingBoxQuery("location").setValidationMethod(GeoValidationMethod.COERCE).setCorners(90, -180, -90, 180).type("indexed")).execute().actionGet();
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L));
    searchResponse = client().prepareSearch().setQuery(geoBoundingBoxQuery("location").setValidationMethod(GeoValidationMethod.COERCE).setCorners(50, 0, -50, 360)).execute().actionGet();
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
    searchResponse = client().prepareSearch().setQuery(geoBoundingBoxQuery("location").setValidationMethod(GeoValidationMethod.COERCE).setCorners(50, 0, -50, 360).type("indexed")).execute().actionGet();
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
    searchResponse = client().prepareSearch().setQuery(geoBoundingBoxQuery("location").setValidationMethod(GeoValidationMethod.COERCE).setCorners(90, 0, -90, 360)).execute().actionGet();
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L));
    searchResponse = client().prepareSearch().setQuery(geoBoundingBoxQuery("location").setValidationMethod(GeoValidationMethod.COERCE).setCorners(90, 0, -90, 360).type("indexed")).execute().actionGet();
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L));
}
Also used : Version(org.elasticsearch.Version) Settings(org.elasticsearch.common.settings.Settings) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 4 with Version

use of org.elasticsearch.Version in project elasticsearch by elastic.

the class GeoDistanceIT method testGeoDistanceFilter.

/**
     * Issue 3073
     */
public void testGeoDistanceFilter() throws IOException {
    Version version = VersionUtils.randomVersionBetween(random(), Version.V_2_0_0, Version.CURRENT);
    Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build();
    double lat = 40.720611;
    double lon = -73.998776;
    XContentBuilder mapping = JsonXContent.contentBuilder().startObject().startObject("location").startObject("properties").startObject("pin").field("type", "geo_point");
    mapping.endObject().endObject().endObject().endObject();
    XContentBuilder source = JsonXContent.contentBuilder().startObject().field("pin", GeoHashUtils.stringEncode(lon, lat)).endObject();
    assertAcked(prepareCreate("locations").setSettings(settings).addMapping("location", mapping));
    client().prepareIndex("locations", "location", "1").setCreate(true).setSource(source).execute().actionGet();
    refresh();
    client().prepareGet("locations", "location", "1").execute().actionGet();
    SearchResponse result = client().prepareSearch("locations").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(QueryBuilders.geoDistanceQuery("pin").geoDistance(GeoDistance.ARC).point(lat, lon).distance("1m")).execute().actionGet();
    assertHitCount(result, 1);
}
Also used : Version(org.elasticsearch.Version) Settings(org.elasticsearch.common.settings.Settings) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 5 with Version

use of org.elasticsearch.Version in project elasticsearch by elastic.

the class GeoDistanceIT method testDistanceSortingNestedFields.

public void testDistanceSortingNestedFields() throws Exception {
    Version version = VersionUtils.randomVersionBetween(random(), Version.V_2_0_0, Version.CURRENT);
    Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build();
    XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().startObject().startObject("company").startObject("properties").startObject("name").field("type", "text").endObject().startObject("branches").field("type", "nested").startObject("properties").startObject("name").field("type", "text").endObject().startObject("location").field("type", "geo_point");
    xContentBuilder.endObject().endObject().endObject().endObject().endObject().endObject();
    assertAcked(prepareCreate("companies").setSettings(settings).addMapping("company", xContentBuilder));
    ensureGreen();
    indexRandom(true, client().prepareIndex("companies", "company", "1").setSource(jsonBuilder().startObject().field("name", "company 1").startArray("branches").startObject().field("name", "New York").startObject("location").field("lat", 40.7143528).field("lon", -74.0059731).endObject().endObject().endArray().endObject()), client().prepareIndex("companies", "company", "2").setSource(jsonBuilder().startObject().field("name", "company 2").startArray("branches").startObject().field("name", "Times Square").startObject("location").field("lat", 40.759011).field("lon", -73.9844722).endObject().endObject().startObject().field("name", "Tribeca").startObject("location").field("lat", 40.718266).field("lon", -74.007819).endObject().endObject().endArray().endObject()), client().prepareIndex("companies", "company", "3").setSource(jsonBuilder().startObject().field("name", "company 3").startArray("branches").startObject().field("name", "Wall Street").startObject("location").field("lat", 40.7051157).field("lon", -74.0088305).endObject().endObject().startObject().field("name", "Soho").startObject("location").field("lat", 40.7247222).field("lon", -74).endObject().endObject().endArray().endObject()), client().prepareIndex("companies", "company", "4").setSource(jsonBuilder().startObject().field("name", "company 4").startArray("branches").startObject().field("name", "Greenwich Village").startObject("location").field("lat", 40.731033).field("lon", -73.9962255).endObject().endObject().startObject().field("name", "Brooklyn").startObject("location").field("lat", 40.65).field("lon", -73.95).endObject().endObject().endArray().endObject()));
    // Order: Asc
    SearchResponse searchResponse = client().prepareSearch("companies").setQuery(matchAllQuery()).addSort(SortBuilders.geoDistanceSort("branches.location", 40.7143528, -74.0059731).order(SortOrder.ASC).setNestedPath("branches")).execute().actionGet();
    assertHitCount(searchResponse, 4);
    assertOrderedSearchHits(searchResponse, "1", "2", "3", "4");
    assertThat(((Number) searchResponse.getHits().getAt(0).getSortValues()[0]).doubleValue(), closeTo(0d, 10d));
    assertThat(((Number) searchResponse.getHits().getAt(1).getSortValues()[0]).doubleValue(), closeTo(462.1d, 10d));
    assertThat(((Number) searchResponse.getHits().getAt(2).getSortValues()[0]).doubleValue(), closeTo(1055.0d, 10d));
    assertThat(((Number) searchResponse.getHits().getAt(3).getSortValues()[0]).doubleValue(), closeTo(2029.0d, 10d));
    // Order: Asc, Mode: max
    searchResponse = client().prepareSearch("companies").setQuery(matchAllQuery()).addSort(SortBuilders.geoDistanceSort("branches.location", 40.7143528, -74.0059731).order(SortOrder.ASC).sortMode(SortMode.MAX).setNestedPath("branches")).execute().actionGet();
    assertHitCount(searchResponse, 4);
    assertOrderedSearchHits(searchResponse, "1", "3", "2", "4");
    assertThat(((Number) searchResponse.getHits().getAt(0).getSortValues()[0]).doubleValue(), closeTo(0d, 10d));
    assertThat(((Number) searchResponse.getHits().getAt(1).getSortValues()[0]).doubleValue(), closeTo(1258.0d, 10d));
    assertThat(((Number) searchResponse.getHits().getAt(2).getSortValues()[0]).doubleValue(), closeTo(5286.0d, 10d));
    assertThat(((Number) searchResponse.getHits().getAt(3).getSortValues()[0]).doubleValue(), closeTo(8572.0d, 10d));
    // Order: Desc
    searchResponse = client().prepareSearch("companies").setQuery(matchAllQuery()).addSort(SortBuilders.geoDistanceSort("branches.location", 40.7143528, -74.0059731).order(SortOrder.DESC).setNestedPath("branches")).execute().actionGet();
    assertHitCount(searchResponse, 4);
    assertOrderedSearchHits(searchResponse, "4", "2", "3", "1");
    assertThat(((Number) searchResponse.getHits().getAt(0).getSortValues()[0]).doubleValue(), closeTo(8572.0d, 10d));
    assertThat(((Number) searchResponse.getHits().getAt(1).getSortValues()[0]).doubleValue(), closeTo(5286.0d, 10d));
    assertThat(((Number) searchResponse.getHits().getAt(2).getSortValues()[0]).doubleValue(), closeTo(1258.0d, 10d));
    assertThat(((Number) searchResponse.getHits().getAt(3).getSortValues()[0]).doubleValue(), closeTo(0d, 10d));
    // Order: Desc, Mode: min
    searchResponse = client().prepareSearch("companies").setQuery(matchAllQuery()).addSort(SortBuilders.geoDistanceSort("branches.location", 40.7143528, -74.0059731).order(SortOrder.DESC).sortMode(SortMode.MIN).setNestedPath("branches")).execute().actionGet();
    assertHitCount(searchResponse, 4);
    assertOrderedSearchHits(searchResponse, "4", "3", "2", "1");
    assertThat(((Number) searchResponse.getHits().getAt(0).getSortValues()[0]).doubleValue(), closeTo(2029.0d, 10d));
    assertThat(((Number) searchResponse.getHits().getAt(1).getSortValues()[0]).doubleValue(), closeTo(1055.0d, 10d));
    assertThat(((Number) searchResponse.getHits().getAt(2).getSortValues()[0]).doubleValue(), closeTo(462.1d, 10d));
    assertThat(((Number) searchResponse.getHits().getAt(3).getSortValues()[0]).doubleValue(), closeTo(0d, 10d));
    searchResponse = client().prepareSearch("companies").setQuery(matchAllQuery()).addSort(SortBuilders.geoDistanceSort("branches.location", 40.7143528, -74.0059731).sortMode(SortMode.AVG).order(SortOrder.ASC).setNestedPath("branches")).execute().actionGet();
    assertHitCount(searchResponse, 4);
    assertOrderedSearchHits(searchResponse, "1", "3", "2", "4");
    assertThat(((Number) searchResponse.getHits().getAt(0).getSortValues()[0]).doubleValue(), closeTo(0d, 10d));
    assertThat(((Number) searchResponse.getHits().getAt(1).getSortValues()[0]).doubleValue(), closeTo(1157.0d, 10d));
    assertThat(((Number) searchResponse.getHits().getAt(2).getSortValues()[0]).doubleValue(), closeTo(2874.0d, 10d));
    assertThat(((Number) searchResponse.getHits().getAt(3).getSortValues()[0]).doubleValue(), closeTo(5301.0d, 10d));
    searchResponse = client().prepareSearch("companies").setQuery(matchAllQuery()).addSort(SortBuilders.geoDistanceSort("branches.location", 40.7143528, -74.0059731).setNestedPath("branches").sortMode(SortMode.AVG).order(SortOrder.DESC).setNestedPath("branches")).execute().actionGet();
    assertHitCount(searchResponse, 4);
    assertOrderedSearchHits(searchResponse, "4", "2", "3", "1");
    assertThat(((Number) searchResponse.getHits().getAt(0).getSortValues()[0]).doubleValue(), closeTo(5301.0d, 10d));
    assertThat(((Number) searchResponse.getHits().getAt(1).getSortValues()[0]).doubleValue(), closeTo(2874.0d, 10d));
    assertThat(((Number) searchResponse.getHits().getAt(2).getSortValues()[0]).doubleValue(), closeTo(1157.0d, 10d));
    assertThat(((Number) searchResponse.getHits().getAt(3).getSortValues()[0]).doubleValue(), closeTo(0d, 10d));
    searchResponse = client().prepareSearch("companies").setQuery(matchAllQuery()).addSort(SortBuilders.geoDistanceSort("branches.location", 40.7143528, -74.0059731).setNestedFilter(termQuery("branches.name", "brooklyn")).sortMode(SortMode.AVG).order(SortOrder.ASC).setNestedPath("branches")).execute().actionGet();
    assertHitCount(searchResponse, 4);
    assertFirstHit(searchResponse, hasId("4"));
    assertSearchHits(searchResponse, "1", "2", "3", "4");
    assertThat(((Number) searchResponse.getHits().getAt(0).getSortValues()[0]).doubleValue(), closeTo(8572.0d, 10d));
    assertThat(((Number) searchResponse.getHits().getAt(1).getSortValues()[0]).doubleValue(), equalTo(Double.POSITIVE_INFINITY));
    assertThat(((Number) searchResponse.getHits().getAt(2).getSortValues()[0]).doubleValue(), equalTo(Double.POSITIVE_INFINITY));
    assertThat(((Number) searchResponse.getHits().getAt(3).getSortValues()[0]).doubleValue(), equalTo(Double.POSITIVE_INFINITY));
    try {
        client().prepareSearch("companies").setQuery(matchAllQuery()).addSort(SortBuilders.geoDistanceSort("branches.location", 40.7143528, -74.0059731).sortMode(SortMode.SUM).setNestedPath("branches"));
        fail("Sum should not be allowed as sort mode");
    } catch (IllegalArgumentException e) {
    //expected
    }
}
Also used : Version(org.elasticsearch.Version) Settings(org.elasticsearch.common.settings.Settings) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

Version (org.elasticsearch.Version)124 Settings (org.elasticsearch.common.settings.Settings)51 StreamInput (org.elasticsearch.common.io.stream.StreamInput)21 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)20 HashMap (java.util.HashMap)17 ArrayList (java.util.ArrayList)16 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)16 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)14 IOException (java.io.IOException)13 SearchResponse (org.elasticsearch.action.search.SearchResponse)12 List (java.util.List)11 Map (java.util.Map)11 ClusterState (org.elasticsearch.cluster.ClusterState)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 ClusterName (org.elasticsearch.cluster.ClusterName)9 TimeValue (org.elasticsearch.common.unit.TimeValue)9 VersionUtils.randomVersion (org.elasticsearch.test.VersionUtils.randomVersion)9 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)8 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)8 GeoPoint (org.elasticsearch.common.geo.GeoPoint)8