Search in sources :

Example 6 with IndexCoordinates

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();
}
Also used : ArrayList(java.util.ArrayList) IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates)

Example 7 with IndexCoordinates

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);
        });
    });
}
Also used : DefaultReactiveClusterOperations(org.springframework.data.elasticsearch.core.cluster.DefaultReactiveClusterOperations) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) ElasticsearchPersistentEntity(org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) BulkFailureException(org.springframework.data.elasticsearch.BulkFailureException) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) ReindexRequest(org.springframework.data.elasticsearch.core.reindex.ReindexRequest) VersionInfo(org.springframework.data.elasticsearch.support.VersionInfo) MappingElasticsearchConverter(org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter) Map(java.util.Map) RoutingResolver(org.springframework.data.elasticsearch.core.routing.RoutingResolver) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) DeleteByQueryRequest(org.elasticsearch.index.reindex.DeleteByQueryRequest) ElasticsearchPersistentProperty(org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty) GetRequest(org.elasticsearch.action.get.GetRequest) ReactiveEntityCallbacks(org.springframework.data.mapping.callback.ReactiveEntityCallbacks) IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates) UpdateQuery(org.springframework.data.elasticsearch.core.query.UpdateQuery) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) ReactiveClusterOperations(org.springframework.data.elasticsearch.core.cluster.ReactiveClusterOperations) Collection(java.util.Collection) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) IndexQuery(org.springframework.data.elasticsearch.core.query.IndexQuery) Collectors(java.util.stream.Collectors) List(java.util.List) Version(org.elasticsearch.Version) NonNull(org.springframework.lang.NonNull) LogFactory(org.apache.commons.logging.LogFactory) SearchDocument(org.springframework.data.elasticsearch.core.document.SearchDocument) ApplicationContextAware(org.springframework.context.ApplicationContextAware) Document(org.springframework.data.elasticsearch.core.document.Document) ReactiveBeforeConvertCallback(org.springframework.data.elasticsearch.core.event.ReactiveBeforeConvertCallback) ReactiveAfterSaveCallback(org.springframework.data.elasticsearch.core.event.ReactiveAfterSaveCallback) SeqNoPrimaryTerm(org.springframework.data.elasticsearch.core.query.SeqNoPrimaryTerm) Suggest(org.springframework.data.elasticsearch.core.suggest.response.Suggest) GetResult(org.elasticsearch.index.get.GetResult) Tuple2(reactor.util.function.Tuple2) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest) HashMap(java.util.HashMap) Query(org.springframework.data.elasticsearch.core.query.Query) SearchRequest(org.elasticsearch.action.search.SearchRequest) BulkOptions(org.springframework.data.elasticsearch.core.query.BulkOptions) EntityReader(org.springframework.data.convert.EntityReader) SearchDocumentResponse(org.springframework.data.elasticsearch.core.document.SearchDocumentResponse) WriteRequest(org.elasticsearch.action.support.WriteRequest) DocumentAdapters(org.springframework.data.elasticsearch.core.document.DocumentAdapters) ByQueryResponse(org.springframework.data.elasticsearch.core.query.ByQueryResponse) ElasticsearchConverter(org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter) ReactiveAfterLoadCallback(org.springframework.data.elasticsearch.core.event.ReactiveAfterLoadCallback) IndexResponse(org.elasticsearch.action.index.IndexResponse) Nullable(org.springframework.lang.Nullable) UpdateResponse(org.springframework.data.elasticsearch.core.query.UpdateResponse) UpdateByQueryRequest(org.elasticsearch.index.reindex.UpdateByQueryRequest) BulkByScrollResponse(org.elasticsearch.index.reindex.BulkByScrollResponse) Publisher(org.reactivestreams.Publisher) Mono(reactor.core.publisher.Mono) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) BeansException(org.springframework.beans.BeansException) SimpleElasticsearchMappingContext(org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext) ReactiveElasticsearchClient(org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient) DocWriteResponse(org.elasticsearch.action.DocWriteResponse) ApplicationContext(org.springframework.context.ApplicationContext) SuggestBuilder(org.elasticsearch.search.suggest.SuggestBuilder) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) AdaptibleEntity(org.springframework.data.elasticsearch.core.EntityOperations.AdaptibleEntity) ReactiveAfterConvertCallback(org.springframework.data.elasticsearch.core.event.ReactiveAfterConvertCallback) NoSuchIndexException(org.springframework.data.elasticsearch.NoSuchIndexException) DefaultRoutingResolver(org.springframework.data.elasticsearch.core.routing.DefaultRoutingResolver) Log(org.apache.commons.logging.Log) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) ReindexResponse(org.springframework.data.elasticsearch.core.reindex.ReindexResponse) UncategorizedElasticsearchException(org.springframework.data.elasticsearch.UncategorizedElasticsearchException) Assert(org.springframework.util.Assert) DocWriteResponse(org.elasticsearch.action.DocWriteResponse) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse)

Example 8 with IndexCoordinates

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);
    });
}
Also used : DefaultReactiveClusterOperations(org.springframework.data.elasticsearch.core.cluster.DefaultReactiveClusterOperations) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) ElasticsearchPersistentEntity(org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) BulkFailureException(org.springframework.data.elasticsearch.BulkFailureException) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) ReindexRequest(org.springframework.data.elasticsearch.core.reindex.ReindexRequest) VersionInfo(org.springframework.data.elasticsearch.support.VersionInfo) MappingElasticsearchConverter(org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter) Map(java.util.Map) RoutingResolver(org.springframework.data.elasticsearch.core.routing.RoutingResolver) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) DeleteByQueryRequest(org.elasticsearch.index.reindex.DeleteByQueryRequest) ElasticsearchPersistentProperty(org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty) GetRequest(org.elasticsearch.action.get.GetRequest) ReactiveEntityCallbacks(org.springframework.data.mapping.callback.ReactiveEntityCallbacks) IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates) UpdateQuery(org.springframework.data.elasticsearch.core.query.UpdateQuery) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) ReactiveClusterOperations(org.springframework.data.elasticsearch.core.cluster.ReactiveClusterOperations) Collection(java.util.Collection) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) IndexQuery(org.springframework.data.elasticsearch.core.query.IndexQuery) Collectors(java.util.stream.Collectors) List(java.util.List) Version(org.elasticsearch.Version) NonNull(org.springframework.lang.NonNull) LogFactory(org.apache.commons.logging.LogFactory) SearchDocument(org.springframework.data.elasticsearch.core.document.SearchDocument) ApplicationContextAware(org.springframework.context.ApplicationContextAware) Document(org.springframework.data.elasticsearch.core.document.Document) ReactiveBeforeConvertCallback(org.springframework.data.elasticsearch.core.event.ReactiveBeforeConvertCallback) ReactiveAfterSaveCallback(org.springframework.data.elasticsearch.core.event.ReactiveAfterSaveCallback) SeqNoPrimaryTerm(org.springframework.data.elasticsearch.core.query.SeqNoPrimaryTerm) Suggest(org.springframework.data.elasticsearch.core.suggest.response.Suggest) GetResult(org.elasticsearch.index.get.GetResult) Tuple2(reactor.util.function.Tuple2) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest) HashMap(java.util.HashMap) Query(org.springframework.data.elasticsearch.core.query.Query) SearchRequest(org.elasticsearch.action.search.SearchRequest) BulkOptions(org.springframework.data.elasticsearch.core.query.BulkOptions) EntityReader(org.springframework.data.convert.EntityReader) SearchDocumentResponse(org.springframework.data.elasticsearch.core.document.SearchDocumentResponse) WriteRequest(org.elasticsearch.action.support.WriteRequest) DocumentAdapters(org.springframework.data.elasticsearch.core.document.DocumentAdapters) ByQueryResponse(org.springframework.data.elasticsearch.core.query.ByQueryResponse) ElasticsearchConverter(org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter) ReactiveAfterLoadCallback(org.springframework.data.elasticsearch.core.event.ReactiveAfterLoadCallback) IndexResponse(org.elasticsearch.action.index.IndexResponse) Nullable(org.springframework.lang.Nullable) UpdateResponse(org.springframework.data.elasticsearch.core.query.UpdateResponse) UpdateByQueryRequest(org.elasticsearch.index.reindex.UpdateByQueryRequest) BulkByScrollResponse(org.elasticsearch.index.reindex.BulkByScrollResponse) Publisher(org.reactivestreams.Publisher) Mono(reactor.core.publisher.Mono) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) BeansException(org.springframework.beans.BeansException) SimpleElasticsearchMappingContext(org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext) ReactiveElasticsearchClient(org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient) DocWriteResponse(org.elasticsearch.action.DocWriteResponse) ApplicationContext(org.springframework.context.ApplicationContext) SuggestBuilder(org.elasticsearch.search.suggest.SuggestBuilder) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) AdaptibleEntity(org.springframework.data.elasticsearch.core.EntityOperations.AdaptibleEntity) ReactiveAfterConvertCallback(org.springframework.data.elasticsearch.core.event.ReactiveAfterConvertCallback) NoSuchIndexException(org.springframework.data.elasticsearch.NoSuchIndexException) DefaultRoutingResolver(org.springframework.data.elasticsearch.core.routing.DefaultRoutingResolver) Log(org.apache.commons.logging.Log) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) ReindexResponse(org.springframework.data.elasticsearch.core.reindex.ReindexResponse) UncategorizedElasticsearchException(org.springframework.data.elasticsearch.UncategorizedElasticsearchException) Assert(org.springframework.util.Assert) SearchRequest(org.elasticsearch.action.search.SearchRequest) Mono(reactor.core.publisher.Mono) SearchDocumentResponse(org.springframework.data.elasticsearch.core.document.SearchDocumentResponse)

Example 9 with IndexCoordinates

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());
}
Also used : GetResponse(org.elasticsearch.action.get.GetResponse) ClusterOperations(org.springframework.data.elasticsearch.core.cluster.ClusterOperations) MoreLikeThisQuery(org.springframework.data.elasticsearch.core.query.MoreLikeThisQuery) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) BulkFailureException(org.springframework.data.elasticsearch.BulkFailureException) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) ReindexRequest(org.springframework.data.elasticsearch.core.reindex.ReindexRequest) Map(java.util.Map) SearchResponse(org.elasticsearch.action.search.SearchResponse) RequestOptions(org.elasticsearch.client.RequestOptions) DeleteByQueryRequest(org.elasticsearch.index.reindex.DeleteByQueryRequest) TimeValue(org.elasticsearch.core.TimeValue) GetRequest(org.elasticsearch.action.get.GetRequest) MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse) IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates) UpdateQuery(org.springframework.data.elasticsearch.core.query.UpdateQuery) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) IndexQuery(org.springframework.data.elasticsearch.core.query.IndexQuery) Collectors(java.util.stream.Collectors) MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse) List(java.util.List) Stream(java.util.stream.Stream) Version(org.elasticsearch.Version) LogFactory(org.apache.commons.logging.LogFactory) FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext) ClearScrollRequest(org.elasticsearch.action.search.ClearScrollRequest) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Query(org.springframework.data.elasticsearch.core.query.Query) SearchRequest(org.elasticsearch.action.search.SearchRequest) ElasticsearchClusterOperations(org.springframework.data.elasticsearch.core.cluster.ElasticsearchClusterOperations) BulkOptions(org.springframework.data.elasticsearch.core.query.BulkOptions) ArrayList(java.util.ArrayList) SearchDocumentResponse(org.springframework.data.elasticsearch.core.document.SearchDocumentResponse) WriteRequest(org.elasticsearch.action.support.WriteRequest) DocumentAdapters(org.springframework.data.elasticsearch.core.document.DocumentAdapters) ByQueryResponse(org.springframework.data.elasticsearch.core.query.ByQueryResponse) ElasticsearchConverter(org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter) MultiSearchRequest(org.elasticsearch.action.search.MultiSearchRequest) IndexResponse(org.elasticsearch.action.index.IndexResponse) Nullable(org.springframework.lang.Nullable) MoreLikeThisQueryBuilder(org.elasticsearch.index.query.MoreLikeThisQueryBuilder) UpdateResponse(org.springframework.data.elasticsearch.core.query.UpdateResponse) UpdateByQueryRequest(org.elasticsearch.index.reindex.UpdateByQueryRequest) BulkByScrollResponse(org.elasticsearch.index.reindex.BulkByScrollResponse) Iterator(java.util.Iterator) IOException(java.io.IOException) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) DocWriteResponse(org.elasticsearch.action.DocWriteResponse) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) SuggestBuilder(org.elasticsearch.search.suggest.SuggestBuilder) Log(org.apache.commons.logging.Log) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) SearchScrollRequest(org.elasticsearch.action.search.SearchScrollRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) ReindexResponse(org.springframework.data.elasticsearch.core.reindex.ReindexResponse) Assert(org.springframework.util.Assert) MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest)

Example 10 with IndexCoordinates

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");
}
Also used : IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)

Aggregations

IndexCoordinates (org.springframework.data.elasticsearch.core.mapping.IndexCoordinates)50 Test (org.junit.jupiter.api.Test)27 SpringIntegrationTest (org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)27 IndexQuery (org.springframework.data.elasticsearch.core.query.IndexQuery)20 NativeSearchQueryBuilder (org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder)16 Query (org.springframework.data.elasticsearch.core.query.Query)16 ArrayList (java.util.ArrayList)15 Nullable (org.springframework.lang.Nullable)11 HashMap (java.util.HashMap)10 List (java.util.List)10 Collectors (java.util.stream.Collectors)10 QueryBuilders (org.elasticsearch.index.query.QueryBuilders)10 Map (java.util.Map)9 SearchRequest (org.elasticsearch.action.search.SearchRequest)9 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)9 Log (org.apache.commons.logging.Log)8 LogFactory (org.apache.commons.logging.LogFactory)8 Version (org.elasticsearch.Version)8 DocWriteResponse (org.elasticsearch.action.DocWriteResponse)8 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)8