Search in sources :

Example 1 with Explanation

use of org.springframework.data.elasticsearch.core.document.Explanation in project spring-data-elasticsearch by spring-projects.

the class ElasticsearchTemplateTests method shouldNotReturnExplanationWhenNotRequested.

// #725
@Test
@DisplayName("should not return explanation when not requested")
void shouldNotReturnExplanationWhenNotRequested() {
    SampleEntity entity = SampleEntity.builder().id("42").message("a message with text").build();
    operations.save(entity);
    Criteria criteria = new Criteria("message").contains("with");
    CriteriaQuery query = new CriteriaQuery(criteria);
    SearchHits<SampleEntity> searchHits = operations.search(query, SampleEntity.class);
    assertThat(searchHits.getTotalHits()).isEqualTo(1L);
    Explanation explanation = searchHits.getSearchHit(0).getExplanation();
    assertThat(explanation).isNull();
}
Also used : Explanation(org.springframework.data.elasticsearch.core.document.Explanation) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest) DisplayName(org.junit.jupiter.api.DisplayName)

Example 2 with Explanation

use of org.springframework.data.elasticsearch.core.document.Explanation in project spring-data-elasticsearch by spring-projects.

the class ElasticsearchTemplateTests method shouldReturnExplanationWhenRequested.

// #725
@Test
@DisplayName("should return explanation when requested")
void shouldReturnExplanationWhenRequested() {
    SampleEntity entity = SampleEntity.builder().id("42").message("a message with text").build();
    operations.save(entity);
    Criteria criteria = new Criteria("message").contains("with");
    CriteriaQuery query = new CriteriaQuery(criteria);
    query.setExplain(true);
    SearchHits<SampleEntity> searchHits = operations.search(query, SampleEntity.class);
    assertThat(searchHits.getTotalHits()).isEqualTo(1L);
    Explanation explanation = searchHits.getSearchHit(0).getExplanation();
    assertThat(explanation).isNotNull();
}
Also used : Explanation(org.springframework.data.elasticsearch.core.document.Explanation) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest) DisplayName(org.junit.jupiter.api.DisplayName)

Example 3 with Explanation

use of org.springframework.data.elasticsearch.core.document.Explanation in project spring-data-elasticsearch by spring-projects.

the class DocumentAdaptersUnitTests method shouldAdaptReturnedExplanations.

// #725
@Test
@DisplayName("should adapt returned explanations")
void shouldAdaptReturnedExplanations() {
    SearchHit searchHit = new SearchHit(42);
    searchHit.explanation(// 
    org.apache.lucene.search.Explanation.match(// 
    3.14, // 
    "explanation 3.14", Collections.singletonList(// 
    org.apache.lucene.search.Explanation.noMatch(// 
    "explanation noMatch", Collections.emptyList()))));
    SearchDocument searchDocument = DocumentAdapters.from(searchHit);
    Explanation explanation = searchDocument.getExplanation();
    assertThat(explanation).isNotNull();
    assertThat(explanation.isMatch()).isTrue();
    assertThat(explanation.getValue()).isEqualTo(3.14);
    assertThat(explanation.getDescription()).isEqualTo("explanation 3.14");
    List<Explanation> details = explanation.getDetails();
    assertThat(details).containsExactly(new Explanation(false, 0.0, "explanation noMatch", Collections.emptyList()));
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) Explanation(org.springframework.data.elasticsearch.core.document.Explanation) SearchDocument(org.springframework.data.elasticsearch.core.document.SearchDocument) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

DisplayName (org.junit.jupiter.api.DisplayName)3 Test (org.junit.jupiter.api.Test)3 Explanation (org.springframework.data.elasticsearch.core.document.Explanation)3 SpringIntegrationTest (org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)2 SearchHit (org.elasticsearch.search.SearchHit)1 SearchDocument (org.springframework.data.elasticsearch.core.document.SearchDocument)1