use of org.springframework.data.elasticsearch.core.document.SearchDocument in project spring-data-elasticsearch by spring-projects.
the class DocumentAdaptersUnitTests method searchResponseShouldReturnContainsKey.
// DATAES-628
@Test
public void searchResponseShouldReturnContainsKey() {
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.containsKey("string")).isTrue();
assertThat(document.containsKey("not-set")).isFalse();
}
use of org.springframework.data.elasticsearch.core.document.SearchDocument in project spring-data-elasticsearch by spring-projects.
the class DocumentAdaptersUnitTests method shouldAdaptReturnedMatchedQueries.
// DATAES-979
@Test
@DisplayName("should adapt returned matched queries")
void shouldAdaptReturnedMatchedQueries() {
SearchHit searchHit = new SearchHit(42);
searchHit.matchedQueries(new String[] { "query1", "query2" });
SearchDocument searchDocument = DocumentAdapters.from(searchHit);
List<String> matchedQueries = searchDocument.getMatchedQueries();
assertThat(matchedQueries).isNotNull();
assertThat(matchedQueries).hasSize(2);
assertThat(matchedQueries).isEqualTo(Arrays.asList("query1", "query2"));
}
use of org.springframework.data.elasticsearch.core.document.SearchDocument in project spring-data-elasticsearch by spring-projects.
the class DocumentAdaptersUnitTests method shouldAdaptSearchResponseSource.
// DATAES-628, DATAES-848
@Test
public void shouldAdaptSearchResponseSource() {
BytesArray source = new BytesArray("{\"field\":\"value\"}");
SearchShardTarget shard = new SearchShardTarget("node", new ShardId("index", "uuid", 42), null);
SearchHit searchHit = new SearchHit(123, "my-id", new Text("type"), null, null);
searchHit.shard(shard);
searchHit.sourceRef(source).score(42);
searchHit.version(22);
searchHit.setSeqNo(1);
searchHit.setPrimaryTerm(2);
SearchDocument document = DocumentAdapters.from(searchHit);
assertThat(document.getIndex()).isEqualTo("index");
assertThat(document.hasId()).isTrue();
assertThat(document.getId()).isEqualTo("my-id");
assertThat(document.hasVersion()).isTrue();
assertThat(document.getVersion()).isEqualTo(22);
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 ReactiveElasticsearchTemplate method doFindForResponse.
private <T> Mono<SearchDocumentResponse> doFindForResponse(Query query, Class<?> clazz, IndexCoordinates index) {
return Mono.defer(() -> {
SearchRequest request = requestFactory.searchRequest(query, clazz, index);
request = prepareSearchRequest(request, false);
SearchDocumentCallback<?> documentCallback = new ReadSearchDocumentCallback<>(clazz, index);
// noinspection unchecked
SearchDocumentResponse.EntityCreator<T> entityCreator = searchDocument -> ((Mono<T>) documentCallback.toEntity(searchDocument)).toFuture();
return doFindForResponse(request, entityCreator);
});
}
use of org.springframework.data.elasticsearch.core.document.SearchDocument in project spring-data-elasticsearch by spring-projects.
the class SearchHitMapping method mapInnerDocuments.
/**
* try to convert the SearchDocument instances to instances of the inner property class.
*
* @param searchHits {@link SearchHits} containing {@link Document} instances
* @param type the class of the containing class
* @return a new {@link SearchHits} instance containing the mapped objects or the original inout if any error occurs
*/
private SearchHits<?> mapInnerDocuments(SearchHits<SearchDocument> searchHits, Class<T> type) {
if (searchHits.isEmpty()) {
return searchHits;
}
try {
NestedMetaData nestedMetaData = searchHits.getSearchHit(0).getContent().getNestedMetaData();
ElasticsearchPersistentEntityWithNestedMetaData persistentEntityWithNestedMetaData = getPersistentEntity(mappingContext.getPersistentEntity(type), nestedMetaData);
if (persistentEntityWithNestedMetaData.entity != null) {
List<SearchHit<Object>> convertedSearchHits = new ArrayList<>();
Class<?> targetType = persistentEntityWithNestedMetaData.entity.getType();
// convert the list of SearchHit<SearchDocument> to list of SearchHit<Object>
searchHits.getSearchHits().forEach(searchHit -> {
SearchDocument searchDocument = searchHit.getContent();
Object targetObject = converter.read(targetType, searchDocument);
convertedSearchHits.add(new //
SearchHit<Object>(//
searchDocument.getIndex(), //
searchDocument.getId(), //
searchDocument.getRouting(), //
searchDocument.getScore(), //
searchDocument.getSortValues(), //
searchDocument.getHighlightFields(), //
searchHit.getInnerHits(), //
persistentEntityWithNestedMetaData.nestedMetaData, //
searchHit.getExplanation(), //
searchHit.getMatchedQueries(), targetObject));
});
String scrollId = null;
if (searchHits instanceof SearchHitsImpl) {
scrollId = ((SearchHitsImpl<?>) searchHits).getScrollId();
}
return new //
SearchHitsImpl<>(//
searchHits.getTotalHits(), //
searchHits.getTotalHitsRelation(), //
searchHits.getMaxScore(), //
scrollId, //
convertedSearchHits, //
searchHits.getAggregations(), searchHits.getSuggest());
}
} catch (Exception e) {
throw new UncategorizedElasticsearchException("Unable to convert inner hits.", e);
}
return searchHits;
}
Aggregations