Search in sources :

Example 76 with SearchResponse

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

the class GeoDistanceSortBuilderIT method testCrossIndexIgnoreUnmapped.

public void testCrossIndexIgnoreUnmapped() throws Exception {
    assertAcked(prepareCreate("test1").addMapping("type", "str_field", "type=keyword", "long_field", "type=long", "double_field", "type=double").get());
    assertAcked(prepareCreate("test2").get());
    indexRandom(true, client().prepareIndex("test1", "type").setSource("str_field", "bcd", "long_field", 3, "double_field", 0.65), client().prepareIndex("test2", "type").setSource());
    SearchResponse resp = client().prepareSearch("test1", "test2").addSort(fieldSort("str_field").order(SortOrder.ASC).unmappedType("keyword")).addSort(fieldSort("str_field2").order(SortOrder.DESC).unmappedType("keyword")).get();
    assertSortValues(resp, new Object[] { "bcd", null }, new Object[] { null, null });
    resp = client().prepareSearch("test1", "test2").addSort(fieldSort("long_field").order(SortOrder.ASC).unmappedType("long")).addSort(fieldSort("long_field2").order(SortOrder.DESC).unmappedType("long")).get();
    assertSortValues(resp, new Object[] { 3L, Long.MIN_VALUE }, new Object[] { Long.MAX_VALUE, Long.MIN_VALUE });
    resp = client().prepareSearch("test1", "test2").addSort(fieldSort("double_field").order(SortOrder.ASC).unmappedType("double")).addSort(fieldSort("double_field2").order(SortOrder.DESC).unmappedType("double")).get();
    assertSortValues(resp, new Object[] { 0.65, Double.NEGATIVE_INFINITY }, new Object[] { Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY });
}
Also used : SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 77 with SearchResponse

use of org.elasticsearch.action.search.SearchResponse 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 78 with SearchResponse

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

the class SuggestSearchIT method testSizeOneShard.

// see #2729
public void testSizeOneShard() throws Exception {
    prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1, SETTING_NUMBER_OF_REPLICAS, 0).get();
    ensureGreen();
    for (int i = 0; i < 15; i++) {
        index("test", "type1", Integer.toString(i), "text", "abc" + i);
    }
    refresh();
    SearchResponse search = client().prepareSearch().setQuery(matchQuery("text", "spellchecker")).get();
    assertThat("didn't ask for suggestions but got some", search.getSuggest(), nullValue());
    TermSuggestionBuilder termSuggestion = termSuggestion("text").suggestMode(// Always, otherwise the results can vary between requests.
    SuggestMode.ALWAYS).text("abcd").size(10);
    Suggest suggest = searchSuggest("test", termSuggestion);
    assertSuggestion(suggest, 0, "test", 10, "abc0");
    termSuggestion.text("abcd").shardSize(5);
    suggest = searchSuggest("test", termSuggestion);
    assertSuggestion(suggest, 0, "test", 5, "abc0");
}
Also used : TermSuggestionBuilder(org.elasticsearch.search.suggest.term.TermSuggestionBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 79 with SearchResponse

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

Example 80 with SearchResponse

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

the class ContextCompletionSuggestSearchIT method assertSuggestions.

public void assertSuggestions(String suggestionName, SuggestionBuilder suggestBuilder, String... suggestions) {
    SearchResponse searchResponse = client().prepareSearch(INDEX).suggest(new SuggestBuilder().addSuggestion(suggestionName, suggestBuilder)).execute().actionGet();
    CompletionSuggestSearchIT.assertSuggestions(searchResponse, suggestionName, suggestions);
}
Also used : SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

SearchResponse (org.elasticsearch.action.search.SearchResponse)1457 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)976 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)239 Script (org.elasticsearch.script.Script)223 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)208 Matchers.containsString (org.hamcrest.Matchers.containsString)156 ArrayList (java.util.ArrayList)145 Bucket (org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket)133 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)131 SearchHit (org.elasticsearch.search.SearchHit)114 HashMap (java.util.HashMap)102 Bucket (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket)93 Sum (org.elasticsearch.search.aggregations.metrics.sum.Sum)78 GeoPoint (org.elasticsearch.common.geo.GeoPoint)70 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)69 SearchHits (org.elasticsearch.search.SearchHits)61 AggregationBuilders.dateHistogram (org.elasticsearch.search.aggregations.AggregationBuilders.dateHistogram)60 DateTime (org.joda.time.DateTime)59 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)58 Range (org.elasticsearch.search.aggregations.bucket.range.Range)50