use of org.springframework.data.elasticsearch.core.mapping.IndexCoordinates in project spring-data-elasticsearch by spring-projects.
the class SuggestReactiveTemplateIntegrationTests method loadCompletionObjectEntities.
// region helper functions
private Mono<CompletionEntity> loadCompletionObjectEntities() {
CompletionEntity rizwan_idrees = new CompletionEntityBuilder("1").name("Rizwan Idrees").suggest(new String[] { "Rizwan Idrees" }).build();
CompletionEntity franck_marchand = new CompletionEntityBuilder("2").name("Franck Marchand").suggest(new String[] { "Franck", "Marchand" }).build();
CompletionEntity mohsin_husen = new CompletionEntityBuilder("3").name("Mohsin Husen").suggest(new String[] { "Mohsin", "Husen" }).build();
CompletionEntity artur_konczak = new CompletionEntityBuilder("4").name("Artur Konczak").suggest(new String[] { "Artur", "Konczak" }).build();
List<CompletionEntity> entities = new ArrayList<>(Arrays.asList(rizwan_idrees, franck_marchand, mohsin_husen, artur_konczak));
IndexCoordinates index = IndexCoordinates.of(indexNameProvider.indexName());
return operations.saveAll(entities, index).last();
}
use of org.springframework.data.elasticsearch.core.mapping.IndexCoordinates in project spring-data-elasticsearch by spring-projects.
the class ReactiveElasticsearchTemplate method saveAll.
@Override
public <T> Flux<T> saveAll(Mono<? extends Collection<? extends T>> entitiesPublisher, IndexCoordinates index) {
Assert.notNull(entitiesPublisher, "Entities must not be null!");
return //
entitiesPublisher.flatMapMany(entities -> //
Flux.fromIterable(entities).concatMap(//
entity -> maybeCallBeforeConvert(entity, index))).collectList().map(//
Entities::new).flatMapMany(entities -> {
if (entities.isEmpty()) {
return Flux.empty();
}
return //
doBulkOperation(entities.indexQueries(), BulkOptions.defaultOptions(), index).index().flatMap(indexAndResponse -> {
T savedEntity = entities.entityAt(indexAndResponse.getT1());
BulkItemResponse bulkItemResponse = indexAndResponse.getT2();
DocWriteResponse response = bulkItemResponse.getResponse();
updateIndexedObject(savedEntity, IndexedObjectInformation.of(response.getId(), response.getSeqNo(), response.getPrimaryTerm(), response.getVersion()));
return maybeCallAfterSave(savedEntity, index);
});
});
}
use of org.springframework.data.elasticsearch.core.mapping.IndexCoordinates 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.mapping.IndexCoordinates in project spring-data-elasticsearch by spring-projects.
the class ElasticsearchRestTemplate method multiGet.
@Override
public <T> List<MultiGetItem<T>> multiGet(Query query, Class<T> clazz, IndexCoordinates index) {
Assert.notNull(index, "index must not be null");
MultiGetRequest request = requestFactory.multiGetRequest(query, clazz, index);
MultiGetResponse result = execute(client -> client.mget(request, RequestOptions.DEFAULT));
DocumentCallback<T> callback = new ReadDocumentCallback<>(elasticsearchConverter, clazz, index);
return //
DocumentAdapters.from(result).stream().map(multiGetItem -> //
MultiGetItem.of(multiGetItem.isFailed() ? null : callback.doWith(multiGetItem.getItem()), //
multiGetItem.getFailure())).collect(Collectors.toList());
}
use of org.springframework.data.elasticsearch.core.mapping.IndexCoordinates in project spring-data-elasticsearch by spring-projects.
the class ElasticsearchTemplateTests method shouldIndexGteEntityWithVersionType.
// DATAES-523
@Test
public void shouldIndexGteEntityWithVersionType() {
// given
String documentId = nextIdAsString();
GTEVersionEntity entity = new GTEVersionEntity();
entity.setId(documentId);
entity.setName("FooBar");
entity.setVersion(System.currentTimeMillis());
IndexQueryBuilder indexQueryBuilder = new IndexQueryBuilder().withId(documentId).withVersion(entity.getVersion()).withObject(entity);
IndexCoordinates index = IndexCoordinates.of(indexNameProvider.indexName());
operations.index(indexQueryBuilder.build(), index);
NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).build();
// when
SearchHits<GTEVersionEntity> entities = operations.search(searchQuery, GTEVersionEntity.class, index);
// then
assertThat(entities).isNotNull();
assertThat(entities.getTotalHits()).isGreaterThanOrEqualTo(1);
// reindex with same version
operations.index(indexQueryBuilder.build(), index);
// reindex with version one below
assertThatThrownBy(() -> operations.index(indexQueryBuilder.withVersion(entity.getVersion() - 1).build(), index)).hasMessageContaining("version").hasMessageContaining("conflict");
}
Aggregations