Search in sources :

Example 1 with ToXContent

use of org.elasticsearch.xcontent.ToXContent in project spring-data-elasticsearch by spring-projects.

the class CompletionWithContextsIntegrationTests method shouldFindSuggestionsForGivenCriteriaQueryUsingContextCompletionEntityOfKotlin.

// DATAES-536
@Test
public void shouldFindSuggestionsForGivenCriteriaQueryUsingContextCompletionEntityOfKotlin() {
    // given
    loadContextCompletionObjectEntities();
    SuggestionBuilder completionSuggestionFuzzyBuilder = SuggestBuilders.completionSuggestion("suggest").prefix("m", Fuzziness.AUTO);
    Map<String, List<? extends ToXContent>> contextMap = new HashMap<>();
    List<CategoryQueryContext> contexts = new ArrayList<>(1);
    CategoryQueryContext.Builder builder = CategoryQueryContext.builder();
    builder.setCategory("kotlin");
    CategoryQueryContext queryContext = builder.build();
    contexts.add(queryContext);
    contextMap.put(ContextCompletionEntity.LANGUAGE_CATEGORY, contexts);
    ((CompletionSuggestionBuilder) completionSuggestionFuzzyBuilder).contexts(contextMap);
    // when
    SearchResponse suggestResponse = ((AbstractElasticsearchTemplate) operations).suggest(new SuggestBuilder().addSuggestion("test-suggest", completionSuggestionFuzzyBuilder), IndexCoordinates.of("test-index-context-completion"));
    assertThat(suggestResponse.getSuggest()).isNotNull();
    CompletionSuggestion completionSuggestion = suggestResponse.getSuggest().getSuggestion("test-suggest");
    List<CompletionSuggestion.Entry.Option> options = completionSuggestion.getEntries().get(0).getOptions();
    // then
    assertThat(options).hasSize(2);
    assertThat(options.get(0).getText().string()).isIn("Marchand", "Mohsin");
    assertThat(options.get(1).getText().string()).isIn("Marchand", "Mohsin");
}
Also used : ToXContent(org.elasticsearch.xcontent.ToXContent) AbstractElasticsearchTemplate(org.springframework.data.elasticsearch.core.AbstractElasticsearchTemplate) CompletionSuggestion(org.elasticsearch.search.suggest.completion.CompletionSuggestion) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SearchResponse(org.elasticsearch.action.search.SearchResponse) CompletionSuggestionBuilder(org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder) SuggestBuilder(org.elasticsearch.search.suggest.SuggestBuilder) ArrayList(java.util.ArrayList) List(java.util.List) SuggestionBuilder(org.elasticsearch.search.suggest.SuggestionBuilder) CompletionSuggestionBuilder(org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder) CategoryQueryContext(org.elasticsearch.search.suggest.completion.context.CategoryQueryContext) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)

Example 2 with ToXContent

use of org.elasticsearch.xcontent.ToXContent in project spring-data-elasticsearch by spring-projects.

the class CompletionWithContextsIntegrationTests method shouldFindSuggestionsForGivenCriteriaQueryUsingContextCompletionEntityOfElastic.

// DATAES-536
@Test
public void shouldFindSuggestionsForGivenCriteriaQueryUsingContextCompletionEntityOfElastic() {
    // given
    loadContextCompletionObjectEntities();
    SuggestionBuilder completionSuggestionFuzzyBuilder = SuggestBuilders.completionSuggestion("suggest").prefix("m", Fuzziness.AUTO);
    Map<String, List<? extends ToXContent>> contextMap = new HashMap<>();
    List<CategoryQueryContext> contexts = new ArrayList<>(1);
    CategoryQueryContext.Builder builder = CategoryQueryContext.builder();
    builder.setCategory("elastic");
    CategoryQueryContext queryContext = builder.build();
    contexts.add(queryContext);
    contextMap.put(ContextCompletionEntity.LANGUAGE_CATEGORY, contexts);
    ((CompletionSuggestionBuilder) completionSuggestionFuzzyBuilder).contexts(contextMap);
    // when
    SearchResponse suggestResponse = ((AbstractElasticsearchTemplate) operations).suggest(new SuggestBuilder().addSuggestion("test-suggest", completionSuggestionFuzzyBuilder), IndexCoordinates.of("test-index-context-completion"));
    assertThat(suggestResponse.getSuggest()).isNotNull();
    CompletionSuggestion completionSuggestion = suggestResponse.getSuggest().getSuggestion("test-suggest");
    List<CompletionSuggestion.Entry.Option> options = completionSuggestion.getEntries().get(0).getOptions();
    // then
    assertThat(options).hasSize(1);
    assertThat(options.get(0).getText().string()).isEqualTo("Mohsin");
}
Also used : ToXContent(org.elasticsearch.xcontent.ToXContent) AbstractElasticsearchTemplate(org.springframework.data.elasticsearch.core.AbstractElasticsearchTemplate) CompletionSuggestion(org.elasticsearch.search.suggest.completion.CompletionSuggestion) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SearchResponse(org.elasticsearch.action.search.SearchResponse) CompletionSuggestionBuilder(org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder) SuggestBuilder(org.elasticsearch.search.suggest.SuggestBuilder) ArrayList(java.util.ArrayList) List(java.util.List) SuggestionBuilder(org.elasticsearch.search.suggest.SuggestionBuilder) CompletionSuggestionBuilder(org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder) CategoryQueryContext(org.elasticsearch.search.suggest.completion.context.CategoryQueryContext) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)

Example 3 with ToXContent

use of org.elasticsearch.xcontent.ToXContent in project spring-data-elasticsearch by spring-projects.

the class CompletionWithContextsIntegrationTests method shouldFindSuggestionsForGivenCriteriaQueryUsingContextCompletionEntityOfMongo.

// DATAES-536
@Test
public void shouldFindSuggestionsForGivenCriteriaQueryUsingContextCompletionEntityOfMongo() {
    // given
    loadContextCompletionObjectEntities();
    SuggestionBuilder completionSuggestionFuzzyBuilder = SuggestBuilders.completionSuggestion("suggest").prefix("m", Fuzziness.AUTO);
    Map<String, List<? extends ToXContent>> contextMap = new HashMap<>();
    List<CategoryQueryContext> contexts = new ArrayList<>(1);
    CategoryQueryContext.Builder builder = CategoryQueryContext.builder();
    builder.setCategory("mongo");
    CategoryQueryContext queryContext = builder.build();
    contexts.add(queryContext);
    contextMap.put(ContextCompletionEntity.LANGUAGE_CATEGORY, contexts);
    ((CompletionSuggestionBuilder) completionSuggestionFuzzyBuilder).contexts(contextMap);
    // when
    SearchResponse suggestResponse = ((AbstractElasticsearchTemplate) operations).suggest(new SuggestBuilder().addSuggestion("test-suggest", completionSuggestionFuzzyBuilder), IndexCoordinates.of("test-index-context-completion"));
    assertThat(suggestResponse.getSuggest()).isNotNull();
    CompletionSuggestion completionSuggestion = suggestResponse.getSuggest().getSuggestion("test-suggest");
    List<CompletionSuggestion.Entry.Option> options = completionSuggestion.getEntries().get(0).getOptions();
    // then
    assertThat(options).hasSize(1);
    assertThat(options.get(0).getText().string()).isEqualTo("Marchand");
}
Also used : ToXContent(org.elasticsearch.xcontent.ToXContent) AbstractElasticsearchTemplate(org.springframework.data.elasticsearch.core.AbstractElasticsearchTemplate) CompletionSuggestion(org.elasticsearch.search.suggest.completion.CompletionSuggestion) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SearchResponse(org.elasticsearch.action.search.SearchResponse) CompletionSuggestionBuilder(org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder) SuggestBuilder(org.elasticsearch.search.suggest.SuggestBuilder) ArrayList(java.util.ArrayList) List(java.util.List) SuggestionBuilder(org.elasticsearch.search.suggest.SuggestionBuilder) CompletionSuggestionBuilder(org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder) CategoryQueryContext(org.elasticsearch.search.suggest.completion.context.CategoryQueryContext) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)

Aggregations

ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 List (java.util.List)3 SearchResponse (org.elasticsearch.action.search.SearchResponse)3 SuggestBuilder (org.elasticsearch.search.suggest.SuggestBuilder)3 SuggestionBuilder (org.elasticsearch.search.suggest.SuggestionBuilder)3 CompletionSuggestion (org.elasticsearch.search.suggest.completion.CompletionSuggestion)3 CompletionSuggestionBuilder (org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder)3 CategoryQueryContext (org.elasticsearch.search.suggest.completion.context.CategoryQueryContext)3 ToXContent (org.elasticsearch.xcontent.ToXContent)3 Test (org.junit.jupiter.api.Test)3 AbstractElasticsearchTemplate (org.springframework.data.elasticsearch.core.AbstractElasticsearchTemplate)3 SpringIntegrationTest (org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)3