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");
}
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"));
}
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]")));
}
}
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);
}
}
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"));
}
Aggregations