Search in sources :

Example 26 with CompletionSuggestionBuilder

use of org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder in project elasticsearch by elastic.

the class CompletionSuggestSearchIT method testEarlyTermination.

public void testEarlyTermination() throws Exception {
    final CompletionMappingBuilder mapping = new CompletionMappingBuilder();
    createIndexAndMapping(mapping);
    int numDocs = atLeast(100);
    List<IndexRequestBuilder> indexRequestBuilders = new ArrayList<>();
    for (int i = 0; i < numDocs; i++) {
        indexRequestBuilders.add(client().prepareIndex(INDEX, TYPE, "" + i).setSource(jsonBuilder().startObject().startObject(FIELD).field("input", "suggestion" + (numDocs - i)).field("weight", numDocs - i).endObject().endObject()));
    }
    indexRandom(true, indexRequestBuilders);
    int size = randomIntBetween(3, 10);
    String[] outputs = new String[size];
    for (int i = 0; i < size; i++) {
        outputs[i] = "suggestion" + (numDocs - i);
    }
    CompletionSuggestionBuilder prefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sug").size(size);
    assertSuggestions("foo", prefix, outputs);
    CompletionSuggestionBuilder regex = SuggestBuilders.completionSuggestion(FIELD).regex("su[g|s]g").size(size);
    assertSuggestions("foo", regex, outputs);
    CompletionSuggestionBuilder fuzzyPrefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg", Fuzziness.ONE).size(size);
    assertSuggestions("foo", fuzzyPrefix, outputs);
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) CompletionSuggestionBuilder(org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder) ArrayList(java.util.ArrayList) CollectionUtils.iterableAsArrayList(org.elasticsearch.common.util.CollectionUtils.iterableAsArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 27 with CompletionSuggestionBuilder

use of org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder in project elasticsearch by elastic.

the class CompletionSuggestSearchIT method testThatFuzzySuggesterIsUnicodeAware.

public void testThatFuzzySuggesterIsUnicodeAware() throws Exception {
    createIndexAndMapping(completionMappingBuilder);
    client().prepareIndex(INDEX, TYPE, "1").setSource(jsonBuilder().startObject().startObject(FIELD).startArray("input").value("ööööö").endArray().endObject().endObject()).get();
    refresh();
    // suggestion with a character, which needs unicode awareness
    org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder completionSuggestionBuilder = SuggestBuilders.completionSuggestion(FIELD).prefix("öööи", FuzzyOptions.builder().setUnicodeAware(true).build()).size(10);
    SearchResponse searchResponse = client().prepareSearch(INDEX).suggest(new SuggestBuilder().addSuggestion("foo", completionSuggestionBuilder)).execute().actionGet();
    assertSuggestions(searchResponse, false, "foo", "ööööö");
    // removing unicode awareness leads to no result
    completionSuggestionBuilder = SuggestBuilders.completionSuggestion(FIELD).prefix("öööи", FuzzyOptions.builder().setUnicodeAware(false).build()).size(10);
    searchResponse = client().prepareSearch(INDEX).suggest(new SuggestBuilder().addSuggestion("foo", completionSuggestionBuilder)).execute().actionGet();
    assertSuggestions(searchResponse, false, "foo");
    // increasing edit distance instead of unicode awareness works again, as this is only a single character
    completionSuggestionBuilder = SuggestBuilders.completionSuggestion(FIELD).prefix("öööи", FuzzyOptions.builder().setUnicodeAware(false).setFuzziness(Fuzziness.TWO).build()).size(10);
    searchResponse = client().prepareSearch(INDEX).suggest(new SuggestBuilder().addSuggestion("foo", completionSuggestionBuilder)).execute().actionGet();
    assertSuggestions(searchResponse, false, "foo", "ööööö");
}
Also used : CompletionSuggestionBuilder(org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 28 with CompletionSuggestionBuilder

use of org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder in project elasticsearch by elastic.

the class CompletionSuggestSearchIT method assertSuggestions.

public void assertSuggestions(String suggestion, String... suggestions) {
    String suggestionName = RandomStrings.randomAsciiOfLength(random(), 10);
    CompletionSuggestionBuilder suggestionBuilder = SuggestBuilders.completionSuggestion(FIELD).text(suggestion).size(10);
    assertSuggestions(suggestionName, suggestionBuilder, suggestions);
}
Also used : CompletionSuggestionBuilder(org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString)

Aggregations

CompletionSuggestionBuilder (org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder)28 ArrayList (java.util.ArrayList)23 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)23 LinkedHashMap (java.util.LinkedHashMap)16 GeoPoint (org.elasticsearch.common.geo.GeoPoint)16 CompletionMappingBuilder (org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder)16 CategoryContextMapping (org.elasticsearch.search.suggest.completion.context.CategoryContextMapping)16 ContextMapping (org.elasticsearch.search.suggest.completion.context.ContextMapping)16 GeoContextMapping (org.elasticsearch.search.suggest.completion.context.GeoContextMapping)16 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)14 CollectionUtils.iterableAsArrayList (org.elasticsearch.common.util.CollectionUtils.iterableAsArrayList)8 SearchResponse (org.elasticsearch.action.search.SearchResponse)7 CompletionSuggestion (org.elasticsearch.search.suggest.completion.CompletionSuggestion)4 Matchers.containsString (org.hamcrest.Matchers.containsString)3 HashMap (java.util.HashMap)2 List (java.util.List)2 ToXContent (org.elasticsearch.common.xcontent.ToXContent)2 IndexResponse (org.elasticsearch.action.index.IndexResponse)1 GeoQueryContext (org.elasticsearch.search.suggest.completion.context.GeoQueryContext)1