Search in sources :

Example 6 with SearchDocument

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

the class SearchHitMapping method mapHitsFromResponse.

private SearchHitsImpl<T> mapHitsFromResponse(SearchDocumentResponse searchDocumentResponse, List<T> contents) {
    Assert.notNull(searchDocumentResponse, "searchDocumentResponse is null");
    Assert.notNull(contents, "contents is null");
    Assert.isTrue(searchDocumentResponse.getSearchDocuments().size() == contents.size(), "Count of documents must match the count of entities");
    long totalHits = searchDocumentResponse.getTotalHits();
    float maxScore = searchDocumentResponse.getMaxScore();
    String scrollId = searchDocumentResponse.getScrollId();
    List<SearchHit<T>> searchHits = new ArrayList<>();
    List<SearchDocument> searchDocuments = searchDocumentResponse.getSearchDocuments();
    for (int i = 0; i < searchDocuments.size(); i++) {
        SearchDocument document = searchDocuments.get(i);
        T content = contents.get(i);
        SearchHit<T> hit = mapHit(document, content);
        searchHits.add(hit);
    }
    AggregationsContainer<?> aggregations = searchDocumentResponse.getAggregations();
    TotalHitsRelation totalHitsRelation = TotalHitsRelation.valueOf(searchDocumentResponse.getTotalHitsRelation());
    Suggest suggest = searchDocumentResponse.getSuggest();
    mapHitsInCompletionSuggestion(suggest);
    return new SearchHitsImpl<>(totalHits, totalHitsRelation, maxScore, scrollId, searchHits, aggregations, suggest);
}
Also used : ArrayList(java.util.ArrayList) Suggest(org.springframework.data.elasticsearch.core.suggest.response.Suggest) SearchDocument(org.springframework.data.elasticsearch.core.document.SearchDocument)

Example 7 with SearchDocument

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

the class SearchHitMapping method mapInnerHits.

private Map<String, SearchHits<?>> mapInnerHits(SearchDocument searchDocument) {
    Map<String, SearchHits<?>> innerHits = new LinkedHashMap<>();
    Map<String, SearchDocumentResponse> documentInnerHits = searchDocument.getInnerHits();
    if (documentInnerHits != null && documentInnerHits.size() > 0) {
        SearchHitMapping<SearchDocument> searchDocumentSearchHitMapping = SearchHitMapping.mappingFor(SearchDocument.class, converter);
        for (Map.Entry<String, SearchDocumentResponse> entry : documentInnerHits.entrySet()) {
            SearchDocumentResponse searchDocumentResponse = entry.getValue();
            SearchHits<SearchDocument> searchHits = searchDocumentSearchHitMapping.mapHitsFromResponse(searchDocumentResponse, searchDocumentResponse.getSearchDocuments());
            // map Documents to real objects
            SearchHits<?> mappedSearchHits = mapInnerDocuments(searchHits, type);
            innerHits.put(entry.getKey(), mappedSearchHits);
        }
    }
    return innerHits;
}
Also used : SearchDocumentResponse(org.springframework.data.elasticsearch.core.document.SearchDocumentResponse) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) SearchDocument(org.springframework.data.elasticsearch.core.document.SearchDocument)

Example 8 with SearchDocument

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

the class DocumentAdaptersUnitTests method shouldAdaptSearchResponse.

// DATAES-628, DATAES-848
@Test
public void shouldAdaptSearchResponse() {
    Map<String, DocumentField> fields = Collections.singletonMap("field", new DocumentField("field", Collections.singletonList("value")));
    SearchShardTarget shard = new SearchShardTarget("node", new ShardId("index", "uuid", 42), null);
    SearchHit searchHit = new SearchHit(123, "my-id", new Text("type"), null, fields);
    searchHit.shard(shard);
    searchHit.setSeqNo(1);
    searchHit.setPrimaryTerm(2);
    searchHit.score(42);
    SearchDocument document = DocumentAdapters.from(searchHit);
    assertThat(document.getIndex()).isEqualTo("index");
    assertThat(document.hasId()).isTrue();
    assertThat(document.getId()).isEqualTo("my-id");
    assertThat(document.hasVersion()).isFalse();
    assertThat(document.getScore()).isBetween(42f, 42f);
    assertThat(document.get("field")).isEqualTo("value");
    assertThat(document.hasSeqNo()).isTrue();
    assertThat(document.getSeqNo()).isEqualTo(1);
    assertThat(document.hasPrimaryTerm()).isTrue();
    assertThat(document.getPrimaryTerm()).isEqualTo(2);
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) DocumentField(org.elasticsearch.common.document.DocumentField) SearchHit(org.elasticsearch.search.SearchHit) SearchShardTarget(org.elasticsearch.search.SearchShardTarget) Text(org.elasticsearch.common.text.Text) SearchDocument(org.springframework.data.elasticsearch.core.document.SearchDocument) Test(org.junit.jupiter.api.Test)

Example 9 with SearchDocument

use of org.springframework.data.elasticsearch.core.document.SearchDocument 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)

Example 10 with SearchDocument

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

the class DocumentAdaptersUnitTests method shouldRenderToJson.

// DATAES-628
@Test
public void shouldRenderToJson() {
    Map<String, DocumentField> fields = new LinkedHashMap<>();
    fields.put("string", new DocumentField("string", Collections.singletonList("value")));
    fields.put("bool", new DocumentField("bool", Arrays.asList(true, true, false)));
    SearchHit searchHit = new SearchHit(123, "my-id", new Text("type"), fields, null);
    SearchDocument document = DocumentAdapters.from(searchHit);
    assertThat(document.toJson()).isEqualTo("{\"string\":\"value\",\"bool\":[true,true,false]}");
}
Also used : DocumentField(org.elasticsearch.common.document.DocumentField) SearchHit(org.elasticsearch.search.SearchHit) Text(org.elasticsearch.common.text.Text) LinkedHashMap(java.util.LinkedHashMap) SearchDocument(org.springframework.data.elasticsearch.core.document.SearchDocument) Test(org.junit.jupiter.api.Test)

Aggregations

SearchDocument (org.springframework.data.elasticsearch.core.document.SearchDocument)11 SearchHit (org.elasticsearch.search.SearchHit)7 Test (org.junit.jupiter.api.Test)7 Text (org.elasticsearch.common.text.Text)5 LinkedHashMap (java.util.LinkedHashMap)4 DocumentField (org.elasticsearch.common.document.DocumentField)4 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ShardId (org.elasticsearch.index.shard.ShardId)2 SearchShardTarget (org.elasticsearch.search.SearchShardTarget)2 DisplayName (org.junit.jupiter.api.DisplayName)2 UncategorizedElasticsearchException (org.springframework.data.elasticsearch.UncategorizedElasticsearchException)2 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Log (org.apache.commons.logging.Log)1 LogFactory (org.apache.commons.logging.LogFactory)1 Version (org.elasticsearch.Version)1 DocWriteResponse (org.elasticsearch.action.DocWriteResponse)1