Search in sources :

Example 1 with CompletionSuggestionBuilder

use of org.opensearch.search.suggest.completion.CompletionSuggestionBuilder in project OpenSearch by opensearch-project.

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).setId("" + 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.opensearch.action.index.IndexRequestBuilder) CompletionSuggestionBuilder(org.opensearch.search.suggest.completion.CompletionSuggestionBuilder) CollectionUtils.iterableAsArrayList(org.opensearch.common.util.CollectionUtils.iterableAsArrayList) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 2 with CompletionSuggestionBuilder

use of org.opensearch.search.suggest.completion.CompletionSuggestionBuilder in project OpenSearch by opensearch-project.

the class CompletionSuggestSearchIT method testSuggestDocumentNoSource.

public void testSuggestDocumentNoSource() throws Exception {
    final CompletionMappingBuilder mapping = new CompletionMappingBuilder();
    createIndexAndMapping(mapping);
    int numDocs = randomIntBetween(10, 100);
    List<IndexRequestBuilder> indexRequestBuilders = new ArrayList<>();
    for (int i = 1; i <= numDocs; i++) {
        indexRequestBuilders.add(client().prepareIndex(INDEX).setId("" + i).setSource(jsonBuilder().startObject().startObject(FIELD).field("input", "suggestion" + i).field("weight", i).endObject().endObject()));
    }
    indexRandom(true, indexRequestBuilders);
    CompletionSuggestionBuilder prefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg").size(numDocs);
    SearchResponse searchResponse = client().prepareSearch(INDEX).suggest(new SuggestBuilder().addSuggestion("foo", prefix)).setFetchSource(false).get();
    CompletionSuggestion completionSuggestion = searchResponse.getSuggest().getSuggestion("foo");
    CompletionSuggestion.Entry options = completionSuggestion.getEntries().get(0);
    assertThat(options.getOptions().size(), equalTo(numDocs));
    int id = numDocs;
    for (CompletionSuggestion.Entry.Option option : options) {
        assertThat(option.getText().toString(), equalTo("suggestion" + id));
        assertThat(option.getHit(), hasId("" + id));
        assertThat(option.getHit(), hasScore((id)));
        assertNull(option.getHit().getSourceAsMap());
        id--;
    }
}
Also used : IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) CompletionSuggestionBuilder(org.opensearch.search.suggest.completion.CompletionSuggestionBuilder) CompletionSuggestion(org.opensearch.search.suggest.completion.CompletionSuggestion) CollectionUtils.iterableAsArrayList(org.opensearch.common.util.CollectionUtils.iterableAsArrayList) ArrayList(java.util.ArrayList) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 3 with CompletionSuggestionBuilder

use of org.opensearch.search.suggest.completion.CompletionSuggestionBuilder in project OpenSearch by opensearch-project.

the class CompletionSuggestSearchIT method testSuggestOnlyExplain.

public void testSuggestOnlyExplain() throws Exception {
    final CompletionMappingBuilder mapping = new CompletionMappingBuilder();
    createIndexAndMapping(mapping);
    int numDocs = 10;
    List<IndexRequestBuilder> indexRequestBuilders = new ArrayList<>();
    for (int i = 1; i <= numDocs; i++) {
        indexRequestBuilders.add(client().prepareIndex(INDEX).setId("" + i).setSource(jsonBuilder().startObject().startObject(FIELD).field("input", "suggestion" + i).field("weight", i).endObject().endObject()));
    }
    indexRandom(true, indexRequestBuilders);
    CompletionSuggestionBuilder prefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg");
    SearchResponse searchResponse = client().prepareSearch(INDEX).setExplain(true).suggest(new SuggestBuilder().addSuggestion("foo", prefix)).get();
    assertSuggestions(searchResponse, "foo", "suggestion10", "suggestion9", "suggestion8", "suggestion7", "suggestion6");
}
Also used : IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) CompletionSuggestionBuilder(org.opensearch.search.suggest.completion.CompletionSuggestionBuilder) CollectionUtils.iterableAsArrayList(org.opensearch.common.util.CollectionUtils.iterableAsArrayList) ArrayList(java.util.ArrayList) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 4 with CompletionSuggestionBuilder

use of org.opensearch.search.suggest.completion.CompletionSuggestionBuilder in project OpenSearch by opensearch-project.

the class CompletionSuggestSearchIT method testTextAndGlobalText.

/**
 * test that suggestion works if prefix is either provided via {@link CompletionSuggestionBuilder#text(String)} or
 * {@link SuggestBuilder#setGlobalText(String)}
 */
public void testTextAndGlobalText() throws Exception {
    final CompletionMappingBuilder mapping = new CompletionMappingBuilder();
    createIndexAndMapping(mapping);
    int numDocs = 10;
    List<IndexRequestBuilder> indexRequestBuilders = new ArrayList<>();
    for (int i = 1; i <= numDocs; i++) {
        indexRequestBuilders.add(client().prepareIndex(INDEX).setId("" + i).setSource(jsonBuilder().startObject().startObject(FIELD).field("input", "suggestion" + i).field("weight", i).endObject().endObject()));
    }
    indexRandom(true, indexRequestBuilders);
    CompletionSuggestionBuilder noText = SuggestBuilders.completionSuggestion(FIELD);
    SearchResponse searchResponse = client().prepareSearch(INDEX).suggest(new SuggestBuilder().addSuggestion("foo", noText).setGlobalText("sugg")).get();
    assertSuggestions(searchResponse, "foo", "suggestion10", "suggestion9", "suggestion8", "suggestion7", "suggestion6");
    CompletionSuggestionBuilder withText = SuggestBuilders.completionSuggestion(FIELD).text("sugg");
    searchResponse = client().prepareSearch(INDEX).suggest(new SuggestBuilder().addSuggestion("foo", withText)).get();
    assertSuggestions(searchResponse, "foo", "suggestion10", "suggestion9", "suggestion8", "suggestion7", "suggestion6");
    // test that suggestion text takes precedence over global text
    searchResponse = client().prepareSearch(INDEX).suggest(new SuggestBuilder().addSuggestion("foo", withText).setGlobalText("bogus")).get();
    assertSuggestions(searchResponse, "foo", "suggestion10", "suggestion9", "suggestion8", "suggestion7", "suggestion6");
}
Also used : IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) CompletionSuggestionBuilder(org.opensearch.search.suggest.completion.CompletionSuggestionBuilder) CollectionUtils.iterableAsArrayList(org.opensearch.common.util.CollectionUtils.iterableAsArrayList) ArrayList(java.util.ArrayList) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 5 with CompletionSuggestionBuilder

use of org.opensearch.search.suggest.completion.CompletionSuggestionBuilder in project OpenSearch by opensearch-project.

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.opensearch.search.suggest.completion.CompletionSuggestionBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString)

Aggregations

CompletionSuggestionBuilder (org.opensearch.search.suggest.completion.CompletionSuggestionBuilder)33 ArrayList (java.util.ArrayList)26 IndexRequestBuilder (org.opensearch.action.index.IndexRequestBuilder)26 LinkedHashMap (java.util.LinkedHashMap)15 CategoryContextMapping (org.opensearch.search.suggest.completion.context.CategoryContextMapping)15 ContextMapping (org.opensearch.search.suggest.completion.context.ContextMapping)15 GeoContextMapping (org.opensearch.search.suggest.completion.context.GeoContextMapping)15 GeoPoint (org.opensearch.common.geo.GeoPoint)14 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)14 CompletionMappingBuilder (org.opensearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder)14 CollectionUtils.iterableAsArrayList (org.opensearch.common.util.CollectionUtils.iterableAsArrayList)13 SearchResponse (org.opensearch.action.search.SearchResponse)11 CompletionSuggestion (org.opensearch.search.suggest.completion.CompletionSuggestion)6 Matchers.containsString (org.hamcrest.Matchers.containsString)5 List (java.util.List)3 RandomStrings (com.carrotsearch.randomizedtesting.generators.RandomStrings)2 IOException (java.io.IOException)2 Arrays (java.util.Arrays)2 HashMap (java.util.HashMap)2 Locale (java.util.Locale)2