Search in sources :

Example 76 with SearchResponse

use of org.graylog.shaded.elasticsearch7.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 77 with SearchResponse

use of org.graylog.shaded.elasticsearch7.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 78 with SearchResponse

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

Example 79 with SearchResponse

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

the class SearchStatsIT method testOpenContexts.

public void testOpenContexts() {
    String index = "test1";
    createIndex(index);
    ensureGreen(index);
    // create shards * docs number of docs and attempt to distribute them equally
    // this distribution will not be perfect; each shard will have an integer multiple of docs (possibly zero)
    // we do this so we have a lot of pages to scroll through
    final int docs = scaledRandomIntBetween(20, 50);
    for (int s = 0; s < numAssignedShards(index); s++) {
        for (int i = 0; i < docs; i++) {
            client().prepareIndex(index, "type", Integer.toString(s * docs + i)).setSource("field", "value").setRouting(Integer.toString(s)).execute().actionGet();
        }
    }
    client().admin().indices().prepareRefresh(index).execute().actionGet();
    IndicesStatsResponse indicesStats = client().admin().indices().prepareStats(index).execute().actionGet();
    assertThat(indicesStats.getTotal().getSearch().getOpenContexts(), equalTo(0L));
    int size = scaledRandomIntBetween(1, docs);
    SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size).setScroll(TimeValue.timeValueMinutes(2)).execute().actionGet();
    assertSearchResponse(searchResponse);
    // refresh the stats now that scroll contexts are opened
    indicesStats = client().admin().indices().prepareStats(index).execute().actionGet();
    assertThat(indicesStats.getTotal().getSearch().getOpenContexts(), equalTo((long) numAssignedShards(index)));
    assertThat(indicesStats.getTotal().getSearch().getTotal().getScrollCurrent(), equalTo((long) numAssignedShards(index)));
    int hits = 0;
    while (true) {
        if (searchResponse.getHits().getHits().length == 0) {
            break;
        }
        hits += searchResponse.getHits().getHits().length;
        searchResponse = client().prepareSearchScroll(searchResponse.getScrollId()).setScroll(TimeValue.timeValueMinutes(2)).execute().actionGet();
    }
    long expected = 0;
    // the number of queries executed is equal to at least the sum of number of pages in shard over all shards
    IndicesStatsResponse r = client().admin().indices().prepareStats(index).execute().actionGet();
    for (int s = 0; s < numAssignedShards(index); s++) {
        expected += (long) Math.ceil(r.getShards()[s].getStats().getDocs().getCount() / size);
    }
    indicesStats = client().admin().indices().prepareStats().execute().actionGet();
    Stats stats = indicesStats.getTotal().getSearch().getTotal();
    assertEquals(hits, docs * numAssignedShards(index));
    assertThat(stats.getQueryCount(), greaterThanOrEqualTo(expected));
    clearScroll(searchResponse.getScrollId());
    indicesStats = client().admin().indices().prepareStats().execute().actionGet();
    stats = indicesStats.getTotal().getSearch().getTotal();
    assertThat(indicesStats.getTotal().getSearch().getOpenContexts(), equalTo(0L));
    assertThat(stats.getScrollCount(), equalTo((long) numAssignedShards(index)));
    assertThat(stats.getScrollTimeInMillis(), greaterThan(0L));
}
Also used : IndicesStatsResponse(org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse) NodeStats(org.elasticsearch.action.admin.cluster.node.stats.NodeStats) Stats(org.elasticsearch.index.search.stats.SearchStats.Stats) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 80 with SearchResponse

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

the class CompletionSuggestSearchIT method assertSuggestionsNotInOrder.

public void assertSuggestionsNotInOrder(String suggestString, String... suggestions) {
    String suggestionName = RandomStrings.randomAsciiOfLength(random(), 10);
    SearchResponse searchResponse = client().prepareSearch(INDEX).suggest(new SuggestBuilder().addSuggestion(suggestionName, SuggestBuilders.completionSuggestion(FIELD).text(suggestString).size(10))).execute().actionGet();
    assertSuggestions(searchResponse, false, suggestionName, suggestions);
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) 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