Search in sources :

Example 66 with SearchResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.

the class FieldSortIT method testSortDuelBetweenSingleShardAndMultiShardIndex.

public void testSortDuelBetweenSingleShardAndMultiShardIndex() throws Exception {
    String sortField = "sortField";
    assertAcked(prepareCreate("test1").setSettings(IndexMetaData.SETTING_NUMBER_OF_SHARDS, between(2, maximumNumberOfShards())).addMapping("type", sortField, "type=long").get());
    assertAcked(prepareCreate("test2").setSettings(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).addMapping("type", sortField, "type=long").get());
    for (String index : new String[] { "test1", "test2" }) {
        List<IndexRequestBuilder> docs = new ArrayList<>();
        for (int i = 0; i < 256; i++) {
            docs.add(client().prepareIndex(index, "type", Integer.toString(i)).setSource(sortField, i));
        }
        indexRandom(true, docs);
    }
    ensureSearchable("test1", "test2");
    SortOrder order = randomBoolean() ? SortOrder.ASC : SortOrder.DESC;
    int from = between(0, 256);
    int size = between(0, 256);
    SearchResponse multiShardResponse = client().prepareSearch("test1").setFrom(from).setSize(size).addSort(sortField, order).get();
    assertNoFailures(multiShardResponse);
    SearchResponse singleShardResponse = client().prepareSearch("test2").setFrom(from).setSize(size).addSort(sortField, order).get();
    assertNoFailures(singleShardResponse);
    assertThat(multiShardResponse.getHits().getTotalHits(), equalTo(singleShardResponse.getHits().getTotalHits()));
    assertThat(multiShardResponse.getHits().getHits().length, equalTo(singleShardResponse.getHits().getHits().length));
    for (int i = 0; i < multiShardResponse.getHits().getHits().length; i++) {
        assertThat(multiShardResponse.getHits().getAt(i).getSortValues()[0], equalTo(singleShardResponse.getHits().getAt(i).getSortValues()[0]));
        assertThat(multiShardResponse.getHits().getAt(i).getId(), equalTo(singleShardResponse.getHits().getAt(i).getId()));
    }
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 67 with SearchResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchResponse 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 68 with SearchResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchResponse 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)

Example 69 with SearchResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.

the class GeoDistanceIT method testDistanceSortingWithMissingGeoPoint.

// Regression bug:
// https://github.com/elastic/elasticsearch/issues/2851
public void testDistanceSortingWithMissingGeoPoint() 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("locations").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().array("names", "Times Square", "Tribeca").startArray("locations").startObject().field("lat", 40.759011).field("lon", -73.9844722).endObject().startObject().field("lat", 40.718266).field("lon", -74.007819).endObject().endArray().endObject()).execute().actionGet();
    client().prepareIndex("test", "type1", "2").setSource(jsonBuilder().startObject().array("names", "Wall Street", "Soho").endObject()).execute().actionGet();
    refresh();
    // Order: Asc
    SearchResponse searchResponse = client().prepareSearch("test").setQuery(matchAllQuery()).addSort(SortBuilders.geoDistanceSort("locations", 40.7143528, -74.0059731).order(SortOrder.ASC)).execute().actionGet();
    assertHitCount(searchResponse, 2);
    assertOrderedSearchHits(searchResponse, "1", "2");
    assertThat(((Number) searchResponse.getHits().getAt(0).getSortValues()[0]).doubleValue(), closeTo(462.1d, 10d));
    assertThat(((Number) searchResponse.getHits().getAt(1).getSortValues()[0]).doubleValue(), equalTo(Double.POSITIVE_INFINITY));
    // Order: Desc
    searchResponse = client().prepareSearch("test").setQuery(matchAllQuery()).addSort(SortBuilders.geoDistanceSort("locations", 40.7143528, -74.0059731).order(SortOrder.DESC)).execute().actionGet();
    // Doc with missing geo point is first, is consistent with 0.20.x
    assertHitCount(searchResponse, 2);
    assertOrderedSearchHits(searchResponse, "2", "1");
    assertThat(((Number) searchResponse.getHits().getAt(0).getSortValues()[0]).doubleValue(), equalTo(Double.POSITIVE_INFINITY));
    assertThat(((Number) searchResponse.getHits().getAt(1).getSortValues()[0]).doubleValue(), closeTo(5286d, 10d));
}
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 70 with SearchResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.

the class GeoDistanceIT method testDistanceSortingMVFields.

public void testDistanceSortingMVFields() 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("locations").field("type", "geo_point");
    xContentBuilder.field("ignore_malformed", true).endObject().endObject().endObject().endObject();
    assertAcked(prepareCreate("test").setSettings(settings).addMapping("type1", xContentBuilder));
    ensureGreen();
    client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject().field("names", "New York").startObject("locations").field("lat", 40.7143528).field("lon", -74.0059731).endObject().endObject()).execute().actionGet();
    client().prepareIndex("test", "type1", "2").setSource(jsonBuilder().startObject().field("names", "New York 2").startObject("locations").field("lat", 400.7143528).field("lon", 285.9990269).endObject().endObject()).execute().actionGet();
    client().prepareIndex("test", "type1", "3").setSource(jsonBuilder().startObject().array("names", "Times Square", "Tribeca").startArray("locations").startObject().field("lat", 40.759011).field("lon", -73.9844722).endObject().startObject().field("lat", 40.718266).field("lon", -74.007819).endObject().endArray().endObject()).execute().actionGet();
    client().prepareIndex("test", "type1", "4").setSource(jsonBuilder().startObject().array("names", "Wall Street", "Soho").startArray("locations").startObject().field("lat", 40.7051157).field("lon", -74.0088305).endObject().startObject().field("lat", 40.7247222).field("lon", -74).endObject().endArray().endObject()).execute().actionGet();
    client().prepareIndex("test", "type1", "5").setSource(jsonBuilder().startObject().array("names", "Greenwich Village", "Brooklyn").startArray("locations").startObject().field("lat", 40.731033).field("lon", -73.9962255).endObject().startObject().field("lat", 40.65).field("lon", -73.95).endObject().endArray().endObject()).execute().actionGet();
    client().admin().indices().prepareRefresh().execute().actionGet();
    // Order: Asc
    SearchResponse searchResponse = client().prepareSearch("test").setQuery(matchAllQuery()).addSort(SortBuilders.geoDistanceSort("locations", 40.7143528, -74.0059731).order(SortOrder.ASC)).execute().actionGet();
    assertHitCount(searchResponse, 5);
    assertOrderedSearchHits(searchResponse, "1", "2", "3", "4", "5");
    assertThat(((Number) searchResponse.getHits().getAt(0).getSortValues()[0]).doubleValue(), closeTo(0d, 10d));
    assertThat(((Number) searchResponse.getHits().getAt(1).getSortValues()[0]).doubleValue(), closeTo(421.2d, 10d));
    assertThat(((Number) searchResponse.getHits().getAt(2).getSortValues()[0]).doubleValue(), closeTo(462.1d, 10d));
    assertThat(((Number) searchResponse.getHits().getAt(3).getSortValues()[0]).doubleValue(), closeTo(1055.0d, 10d));
    assertThat(((Number) searchResponse.getHits().getAt(4).getSortValues()[0]).doubleValue(), closeTo(2029.0d, 10d));
    // Order: Asc, Mode: max
    searchResponse = client().prepareSearch("test").setQuery(matchAllQuery()).addSort(SortBuilders.geoDistanceSort("locations", 40.7143528, -74.0059731).order(SortOrder.ASC).sortMode(SortMode.MAX)).execute().actionGet();
    assertHitCount(searchResponse, 5);
    assertOrderedSearchHits(searchResponse, "1", "2", "4", "3", "5");
    assertThat(((Number) searchResponse.getHits().getAt(0).getSortValues()[0]).doubleValue(), closeTo(0d, 10d));
    assertThat(((Number) searchResponse.getHits().getAt(1).getSortValues()[0]).doubleValue(), closeTo(421.2d, 10d));
    assertThat(((Number) searchResponse.getHits().getAt(2).getSortValues()[0]).doubleValue(), closeTo(1258.0d, 10d));
    assertThat(((Number) searchResponse.getHits().getAt(3).getSortValues()[0]).doubleValue(), closeTo(5286.0d, 10d));
    assertThat(((Number) searchResponse.getHits().getAt(4).getSortValues()[0]).doubleValue(), closeTo(8572.0d, 10d));
    // Order: Desc
    searchResponse = client().prepareSearch("test").setQuery(matchAllQuery()).addSort(SortBuilders.geoDistanceSort("locations", 40.7143528, -74.0059731).order(SortOrder.DESC)).execute().actionGet();
    assertHitCount(searchResponse, 5);
    assertOrderedSearchHits(searchResponse, "5", "3", "4", "2", "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(421.2d, 10d));
    assertThat(((Number) searchResponse.getHits().getAt(4).getSortValues()[0]).doubleValue(), closeTo(0d, 10d));
    // Order: Desc, Mode: min
    searchResponse = client().prepareSearch("test").setQuery(matchAllQuery()).addSort(SortBuilders.geoDistanceSort("locations", 40.7143528, -74.0059731).order(SortOrder.DESC).sortMode(SortMode.MIN)).execute().actionGet();
    assertHitCount(searchResponse, 5);
    assertOrderedSearchHits(searchResponse, "5", "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(421.2d, 10d));
    assertThat(((Number) searchResponse.getHits().getAt(4).getSortValues()[0]).doubleValue(), closeTo(0d, 10d));
    searchResponse = client().prepareSearch("test").setQuery(matchAllQuery()).addSort(SortBuilders.geoDistanceSort("locations", 40.7143528, -74.0059731).sortMode(SortMode.AVG).order(SortOrder.ASC)).execute().actionGet();
    assertHitCount(searchResponse, 5);
    assertOrderedSearchHits(searchResponse, "1", "2", "4", "3", "5");
    assertThat(((Number) searchResponse.getHits().getAt(0).getSortValues()[0]).doubleValue(), closeTo(0d, 10d));
    assertThat(((Number) searchResponse.getHits().getAt(1).getSortValues()[0]).doubleValue(), closeTo(421.2d, 10d));
    assertThat(((Number) searchResponse.getHits().getAt(2).getSortValues()[0]).doubleValue(), closeTo(1157d, 10d));
    assertThat(((Number) searchResponse.getHits().getAt(3).getSortValues()[0]).doubleValue(), closeTo(2874d, 10d));
    assertThat(((Number) searchResponse.getHits().getAt(4).getSortValues()[0]).doubleValue(), closeTo(5301d, 10d));
    searchResponse = client().prepareSearch("test").setQuery(matchAllQuery()).addSort(SortBuilders.geoDistanceSort("locations", 40.7143528, -74.0059731).sortMode(SortMode.AVG).order(SortOrder.DESC)).execute().actionGet();
    assertHitCount(searchResponse, 5);
    assertOrderedSearchHits(searchResponse, "5", "3", "4", "2", "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(421.2d, 10d));
    assertThat(((Number) searchResponse.getHits().getAt(4).getSortValues()[0]).doubleValue(), closeTo(0d, 10d));
    try {
        client().prepareSearch("test").setQuery(matchAllQuery()).addSort(SortBuilders.geoDistanceSort("locations", 40.7143528, -74.0059731).sortMode(SortMode.SUM));
        fail("sum should not be supported for sorting by geo distance");
    } 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

SearchResponse (org.elasticsearch.action.search.SearchResponse)1722 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)976 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)275 SearchHit (org.elasticsearch.search.SearchHit)227 Script (org.elasticsearch.script.Script)225 ArrayList (java.util.ArrayList)217 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)210 Matchers.containsString (org.hamcrest.Matchers.containsString)156 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)143 HashMap (java.util.HashMap)139 Bucket (org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket)133 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)132 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)97 Bucket (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket)95 SearchHits (org.elasticsearch.search.SearchHits)92 Sum (org.elasticsearch.search.aggregations.metrics.sum.Sum)84 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)82 Test (org.junit.Test)75 Map (java.util.Map)72 SearchRequest (org.elasticsearch.action.search.SearchRequest)70