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);
}
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;
}
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);
}
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()));
}
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]}");
}
Aggregations