Search in sources :

Example 1 with TermSuggestionBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.suggest.term.TermSuggestionBuilder 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 2 with TermSuggestionBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.suggest.term.TermSuggestionBuilder in project elasticsearch by elastic.

the class SuggestSearchIT method testEmpty.

public void testEmpty() throws Exception {
    createIndex("test");
    ensureGreen();
    index("test", "type1", "1", "text", "bar");
    refresh();
    TermSuggestionBuilder termSuggest = termSuggestion("text").suggestMode(// Always, otherwise the results can vary between requests.
    SuggestMode.ALWAYS).text("abcd");
    Suggest suggest = searchSuggest("test", termSuggest);
    assertSuggestionSize(suggest, 0, 0, "test");
    assertThat(suggest.getSuggestion("test").getEntries().get(0).getText().string(), equalTo("abcd"));
    suggest = searchSuggest("test", termSuggest);
    assertSuggestionSize(suggest, 0, 0, "test");
    assertThat(suggest.getSuggestion("test").getEntries().get(0).getText().string(), equalTo("abcd"));
}
Also used : TermSuggestionBuilder(org.elasticsearch.search.suggest.term.TermSuggestionBuilder)

Example 3 with TermSuggestionBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.suggest.term.TermSuggestionBuilder in project elasticsearch by elastic.

the class SuggestSearchIT method testSuggestAcrossMultipleIndices.

// see #3196
public void testSuggestAcrossMultipleIndices() throws IOException {
    createIndex("test");
    ensureGreen();
    index("test", "type1", "1", "text", "abcd");
    index("test", "type1", "2", "text", "aacd");
    index("test", "type1", "3", "text", "abbd");
    index("test", "type1", "4", "text", "abcc");
    refresh();
    TermSuggestionBuilder termSuggest = termSuggestion("text").suggestMode(// Always, otherwise the results can vary between requests.
    SuggestMode.ALWAYS).text("abcd");
    logger.info("--> run suggestions with one index");
    searchSuggest("test", termSuggest);
    createIndex("test_1");
    ensureGreen();
    index("test_1", "type1", "1", "text", "ab cd");
    index("test_1", "type1", "2", "text", "aa cd");
    index("test_1", "type1", "3", "text", "ab bd");
    index("test_1", "type1", "4", "text", "ab cc");
    refresh();
    termSuggest = termSuggestion("text").suggestMode(// Always, otherwise the results can vary between requests.
    SuggestMode.ALWAYS).text("ab cd").minWordLength(1);
    logger.info("--> run suggestions with two indices");
    searchSuggest("test", termSuggest);
    XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("text").field("type", "text").field("analyzer", "keyword").endObject().endObject().endObject().endObject();
    assertAcked(prepareCreate("test_2").addMapping("type1", mapping));
    ensureGreen();
    index("test_2", "type1", "1", "text", "ab cd");
    index("test_2", "type1", "2", "text", "aa cd");
    index("test_2", "type1", "3", "text", "ab bd");
    index("test_2", "type1", "4", "text", "ab cc");
    index("test_2", "type1", "1", "text", "abcd");
    index("test_2", "type1", "2", "text", "aacd");
    index("test_2", "type1", "3", "text", "abbd");
    index("test_2", "type1", "4", "text", "abcc");
    refresh();
    termSuggest = termSuggestion("text").suggestMode(// Always, otherwise the results can vary between requests.
    SuggestMode.ALWAYS).text("ab cd").minWordLength(1);
    logger.info("--> run suggestions with three indices");
    try {
        searchSuggest("test", termSuggest);
        fail(" can not suggest across multiple indices with different analysis chains");
    } catch (SearchPhaseExecutionException ex) {
        assertThat(ex.getCause(), instanceOf(IllegalStateException.class));
        assertThat(ex.getCause().getMessage(), anyOf(endsWith("Suggest entries have different sizes actual [1] expected [2]"), endsWith("Suggest entries have different sizes actual [2] expected [1]")));
    } catch (IllegalStateException ex) {
        assertThat(ex.getMessage(), anyOf(endsWith("Suggest entries have different sizes actual [1] expected [2]"), endsWith("Suggest entries have different sizes actual [2] expected [1]")));
    }
    termSuggest = termSuggestion("text").suggestMode(// Always, otherwise the results can vary between requests.
    SuggestMode.ALWAYS).text("ABCD").minWordLength(1);
    logger.info("--> run suggestions with four indices");
    try {
        searchSuggest("test", termSuggest);
        fail(" can not suggest across multiple indices with different analysis chains");
    } catch (SearchPhaseExecutionException ex) {
        assertThat(ex.getCause(), instanceOf(IllegalStateException.class));
        assertThat(ex.getCause().getMessage(), anyOf(endsWith("Suggest entries have different text actual [ABCD] expected [abcd]"), endsWith("Suggest entries have different text actual [abcd] expected [ABCD]")));
    } catch (IllegalStateException ex) {
        assertThat(ex.getMessage(), anyOf(endsWith("Suggest entries have different text actual [ABCD] expected [abcd]"), endsWith("Suggest entries have different text actual [abcd] expected [ABCD]")));
    }
}
Also used : SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) TermSuggestionBuilder(org.elasticsearch.search.suggest.term.TermSuggestionBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 4 with TermSuggestionBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.suggest.term.TermSuggestionBuilder in project graylog2-server by Graylog2.

the class QuerySuggestionsES7 method suggest.

@Override
public SuggestionResponse suggest(SuggestionRequest req) {
    final Set<String> affectedIndices = indexLookup.indexNamesForStreamsInTimeRange(req.streams(), req.timerange());
    final TermSuggestionBuilder suggestionBuilder = SuggestBuilders.termSuggestion(req.field()).text(req.input()).size(req.size());
    final SearchSourceBuilder search = new SearchSourceBuilder().query(QueryBuilders.prefixQuery(req.field(), req.input())).size(0).aggregation(AggregationBuilders.terms("fieldvalues").field(req.field()).size(req.size())).suggest(new SuggestBuilder().addSuggestion("corrections", suggestionBuilder));
    try {
        final SearchResponse result = client.singleSearch(new SearchRequest(affectedIndices.toArray(new String[] {})).source(search), "Failed to execute aggregation");
        final ParsedStringTerms fieldValues = result.getAggregations().get("fieldvalues");
        final List<SuggestionEntry> entries = fieldValues.getBuckets().stream().map(b -> new SuggestionEntry(b.getKeyAsString(), b.getDocCount())).collect(Collectors.toList());
        if (!entries.isEmpty()) {
            return SuggestionResponse.forSuggestions(req.field(), req.input(), entries, fieldValues.getSumOfOtherDocCounts());
        } else {
            TermSuggestion suggestion = result.getSuggest().getSuggestion("corrections");
            final List<SuggestionEntry> corrections = suggestion.getEntries().stream().flatMap(e -> e.getOptions().stream()).map(o -> new SuggestionEntry(o.getText().string(), o.getFreq())).collect(Collectors.toList());
            return SuggestionResponse.forSuggestions(req.field(), req.input(), corrections, null);
        }
    } catch (org.graylog.shaded.elasticsearch7.org.elasticsearch.ElasticsearchException exception) {
        final SuggestionError err = tryResponseException(exception).orElseGet(() -> parseException(exception));
        return SuggestionResponse.forError(req.field(), req.input(), err);
    }
}
Also used : TermSuggestionBuilder(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.suggest.term.TermSuggestionBuilder) SearchResponse(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchResponse) SearchSourceBuilder(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder) ParsedStringTerms(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms) SuggestBuilder(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.suggest.SuggestBuilder) SuggestionEntry(org.graylog.plugins.views.search.engine.suggestions.SuggestionEntry) SearchRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchRequest) Set(java.util.Set) TermSuggestion(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.suggest.term.TermSuggestion) SuggestionRequest(org.graylog.plugins.views.search.engine.suggestions.SuggestionRequest) Collectors(java.util.stream.Collectors) AggregationBuilders(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.AggregationBuilders) Inject(javax.inject.Inject) List(java.util.List) QuerySuggestionsService(org.graylog.plugins.views.search.engine.QuerySuggestionsService) ResponseError(org.graylog.storage.elasticsearch7.errors.ResponseError) Optional(java.util.Optional) SuggestBuilders(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.suggest.SuggestBuilders) SuggestionResponse(org.graylog.plugins.views.search.engine.suggestions.SuggestionResponse) IndexLookup(org.graylog.plugins.views.search.elasticsearch.IndexLookup) QueryBuilders(org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.QueryBuilders) SuggestionError(org.graylog.plugins.views.search.engine.suggestions.SuggestionError) SearchRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchRequest) ParsedStringTerms(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms) SearchSourceBuilder(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchResponse) SuggestBuilder(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.suggest.SuggestBuilder) SuggestionError(org.graylog.plugins.views.search.engine.suggestions.SuggestionError) TermSuggestion(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.suggest.term.TermSuggestion) TermSuggestionBuilder(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.suggest.term.TermSuggestionBuilder) SuggestionEntry(org.graylog.plugins.views.search.engine.suggestions.SuggestionEntry)

Example 5 with TermSuggestionBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.suggest.term.TermSuggestionBuilder in project elasticsearch by elastic.

the class SuggestSearchIT method testSimple.

public void testSimple() throws Exception {
    createIndex("test");
    ensureGreen();
    index("test", "type1", "1", "text", "abcd");
    index("test", "type1", "2", "text", "aacd");
    index("test", "type1", "3", "text", "abbd");
    index("test", "type1", "4", "text", "abcc");
    refresh();
    SearchResponse search = client().prepareSearch().setQuery(matchQuery("text", "spellcecker")).get();
    assertThat("didn't ask for suggestions but got some", search.getSuggest(), nullValue());
    TermSuggestionBuilder termSuggest = termSuggestion("text").suggestMode(// Always, otherwise the results can vary between requests.
    SuggestMode.ALWAYS).text("abcd");
    Suggest suggest = searchSuggest("test", termSuggest);
    assertSuggestion(suggest, 0, "test", "aacd", "abbd", "abcc");
    assertThat(suggest.getSuggestion("test").getEntries().get(0).getText().string(), equalTo("abcd"));
    suggest = searchSuggest("test", termSuggest);
    assertSuggestion(suggest, 0, "test", "aacd", "abbd", "abcc");
    assertThat(suggest.getSuggestion("test").getEntries().get(0).getText().string(), equalTo("abcd"));
}
Also used : TermSuggestionBuilder(org.elasticsearch.search.suggest.term.TermSuggestionBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

TermSuggestionBuilder (org.elasticsearch.search.suggest.term.TermSuggestionBuilder)4 SearchResponse (org.elasticsearch.action.search.SearchResponse)2 List (java.util.List)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Inject (javax.inject.Inject)1 SearchPhaseExecutionException (org.elasticsearch.action.search.SearchPhaseExecutionException)1 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)1 IndexLookup (org.graylog.plugins.views.search.elasticsearch.IndexLookup)1 QuerySuggestionsService (org.graylog.plugins.views.search.engine.QuerySuggestionsService)1 SuggestionEntry (org.graylog.plugins.views.search.engine.suggestions.SuggestionEntry)1 SuggestionError (org.graylog.plugins.views.search.engine.suggestions.SuggestionError)1 SuggestionRequest (org.graylog.plugins.views.search.engine.suggestions.SuggestionRequest)1 SuggestionResponse (org.graylog.plugins.views.search.engine.suggestions.SuggestionResponse)1 SearchRequest (org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchRequest)1 SearchResponse (org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchResponse)1 QueryBuilders (org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.QueryBuilders)1 AggregationBuilders (org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.AggregationBuilders)1 ParsedStringTerms (org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms)1