Search in sources :

Example 1 with TermSuggestionBuilder

use of org.opensearch.search.suggest.term.TermSuggestionBuilder in project OpenSearch by opensearch-project.

the class SuggestSearchIT method testEmpty.

public void testEmpty() throws Exception {
    assertAcked(prepareCreate("test").addMapping("type1", "text", "type=text"));
    ensureGreen();
    index("test", "type1", "1", "text", "bar");
    refresh();
    TermSuggestionBuilder termSuggest = // Always, otherwise the results can vary
    termSuggestion("text").suggestMode(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.opensearch.search.suggest.term.TermSuggestionBuilder)

Example 2 with TermSuggestionBuilder

use of org.opensearch.search.suggest.term.TermSuggestionBuilder in project OpenSearch by opensearch-project.

the class SuggestSearchIT method testSuggestAcrossMultipleIndices.

// see #3196
public void testSuggestAcrossMultipleIndices() throws IOException {
    assertAcked(prepareCreate("test").addMapping("type1", "text", "type=text"));
    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 = // Always, otherwise the results can vary
    termSuggestion("text").suggestMode(SuggestMode.ALWAYS).text("abcd");
    logger.info("--> run suggestions with one index");
    searchSuggest("test", termSuggest);
    assertAcked(prepareCreate("test_1").addMapping("type1", "text", "type=text"));
    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 = // Always, otherwise the results can vary between requests.
    termSuggestion("text").suggestMode(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 = // Always, otherwise the results can vary between requests.
    termSuggestion("text").suggestMode(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 = // Always, otherwise the results can vary between requests.
    termSuggestion("text").suggestMode(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.opensearch.action.search.SearchPhaseExecutionException) TermSuggestionBuilder(org.opensearch.search.suggest.term.TermSuggestionBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 3 with TermSuggestionBuilder

use of org.opensearch.search.suggest.term.TermSuggestionBuilder in project OpenSearch by opensearch-project.

the class SuggestSearchIT method testSimple.

public void testSimple() throws Exception {
    assertAcked(prepareCreate("test").addMapping("type1", "text", "type=text"));
    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 = // Always, otherwise the results can vary
    termSuggestion("text").suggestMode(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.opensearch.search.suggest.term.TermSuggestionBuilder) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 4 with TermSuggestionBuilder

use of org.opensearch.search.suggest.term.TermSuggestionBuilder in project OpenSearch by opensearch-project.

the class CCSDuelIT method testTermSuggester.

public void testTermSuggester() throws Exception {
    assumeMultiClusterSetup();
    SearchRequest searchRequest = initSearchRequest();
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    searchRequest.source(sourceBuilder);
    SuggestBuilder suggestBuilder = new SuggestBuilder();
    suggestBuilder.setGlobalText("jva hml");
    suggestBuilder.addSuggestion("tags", new TermSuggestionBuilder("tags").suggestMode(TermSuggestionBuilder.SuggestMode.POPULAR));
    sourceBuilder.suggest(suggestBuilder);
    duelSearch(searchRequest, response -> {
        assertMultiClusterSearchResponse(response);
        assertEquals(1, response.getSuggest().size());
        TermSuggestion tags = response.getSuggest().getSuggestion("tags");
        assertThat(tags.getEntries().size(), greaterThan(0));
    });
}
Also used : SearchRequest(org.opensearch.action.search.SearchRequest) SuggestBuilder(org.opensearch.search.suggest.SuggestBuilder) TermSuggestion(org.opensearch.search.suggest.term.TermSuggestion) TermSuggestionBuilder(org.opensearch.search.suggest.term.TermSuggestionBuilder) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder)

Example 5 with TermSuggestionBuilder

use of org.opensearch.search.suggest.term.TermSuggestionBuilder in project OpenSearch by opensearch-project.

the class SuggestSearchIT method testSizeOneShard.

// see #2729
public void testSizeOneShard() throws Exception {
    prepareCreate("test").setSettings(Settings.builder().put(SETTING_NUMBER_OF_SHARDS, 1).put(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 = // Always, otherwise the results can
    termSuggestion("text").suggestMode(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.opensearch.search.suggest.term.TermSuggestionBuilder) SearchResponse(org.opensearch.action.search.SearchResponse)

Aggregations

TermSuggestionBuilder (org.opensearch.search.suggest.term.TermSuggestionBuilder)7 SearchResponse (org.opensearch.action.search.SearchResponse)2 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)2 ArrayList (java.util.ArrayList)1 CreateIndexRequestBuilder (org.opensearch.action.admin.indices.create.CreateIndexRequestBuilder)1 IndexRequestBuilder (org.opensearch.action.index.IndexRequestBuilder)1 SearchPhaseExecutionException (org.opensearch.action.search.SearchPhaseExecutionException)1 SearchRequest (org.opensearch.action.search.SearchRequest)1 SearchSourceBuilder (org.opensearch.search.builder.SearchSourceBuilder)1 SuggestBuilder (org.opensearch.search.suggest.SuggestBuilder)1 TermSuggestion (org.opensearch.search.suggest.term.TermSuggestion)1