Search in sources :

Example 26 with IndexCoordinates

use of org.springframework.data.elasticsearch.core.mapping.IndexCoordinates in project spring-data-elasticsearch by spring-projects.

the class ElasticsearchRestTemplate method multiSearch.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public List<SearchHits<?>> multiSearch(List<? extends Query> queries, List<Class<?>> classes) {
    Assert.notNull(queries, "queries must not be null");
    Assert.notNull(classes, "classes must not be null");
    Assert.isTrue(queries.size() == classes.size(), "queries and classes must have the same size");
    MultiSearchRequest request = new MultiSearchRequest();
    Iterator<Class<?>> it = classes.iterator();
    for (Query query : queries) {
        Class<?> clazz = it.next();
        request.add(requestFactory.searchRequest(query, clazz, getIndexCoordinatesFor(clazz)));
    }
    MultiSearchResponse.Item[] items = getMultiSearchResult(request);
    List<SearchHits<?>> res = new ArrayList<>(queries.size());
    Iterator<Class<?>> it1 = classes.iterator();
    for (int i = 0; i < queries.size(); i++) {
        Class entityClass = it1.next();
        IndexCoordinates index = getIndexCoordinatesFor(entityClass);
        ReadDocumentCallback<?> documentCallback = new ReadDocumentCallback<>(elasticsearchConverter, entityClass, index);
        SearchDocumentResponseCallback<SearchHits<?>> callback = new ReadSearchDocumentResponseCallback<>(entityClass, index);
        SearchResponse response = items[i].getResponse();
        res.add(callback.doWith(SearchDocumentResponse.from(response, getEntityCreator(documentCallback))));
    }
    return res;
}
Also used : MoreLikeThisQuery(org.springframework.data.elasticsearch.core.query.MoreLikeThisQuery) UpdateQuery(org.springframework.data.elasticsearch.core.query.UpdateQuery) IndexQuery(org.springframework.data.elasticsearch.core.query.IndexQuery) Query(org.springframework.data.elasticsearch.core.query.Query) ArrayList(java.util.ArrayList) SearchResponse(org.elasticsearch.action.search.SearchResponse) MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse) MultiSearchRequest(org.elasticsearch.action.search.MultiSearchRequest) IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates)

Example 27 with IndexCoordinates

use of org.springframework.data.elasticsearch.core.mapping.IndexCoordinates in project spring-data-elasticsearch by spring-projects.

the class ElasticsearchRestTemplate method doBulkOperation.

public List<IndexedObjectInformation> doBulkOperation(List<?> queries, BulkOptions bulkOptions, IndexCoordinates index) {
    BulkRequest bulkRequest = prepareWriteRequest(requestFactory.bulkRequest(queries, bulkOptions, index));
    List<IndexedObjectInformation> indexedObjectInformationList = checkForBulkOperationFailure(execute(client -> client.bulk(bulkRequest, RequestOptions.DEFAULT)));
    updateIndexedObjectsWithQueries(queries, indexedObjectInformationList);
    return indexedObjectInformationList;
}
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) BulkRequest(org.elasticsearch.action.bulk.BulkRequest)

Example 28 with IndexCoordinates

use of org.springframework.data.elasticsearch.core.mapping.IndexCoordinates in project spring-data-elasticsearch by spring-projects.

the class ElasticsearchTemplateTests method shouldSaveEntityWithJoinFields.

// #1218
private void shouldSaveEntityWithJoinFields(String qId1, String qId2, String aId1, String aId2) throws Exception {
    SampleJoinEntity sampleQuestionEntity1 = new SampleJoinEntity();
    sampleQuestionEntity1.setUuid(qId1);
    sampleQuestionEntity1.setText("This is a question");
    JoinField<String> myQJoinField1 = new JoinField<>("question");
    sampleQuestionEntity1.setMyJoinField(myQJoinField1);
    SampleJoinEntity sampleQuestionEntity2 = new SampleJoinEntity();
    sampleQuestionEntity2.setUuid(qId2);
    sampleQuestionEntity2.setText("This is another question");
    JoinField<String> myQJoinField2 = new JoinField<>("question");
    sampleQuestionEntity2.setMyJoinField(myQJoinField2);
    SampleJoinEntity sampleAnswerEntity1 = new SampleJoinEntity();
    sampleAnswerEntity1.setUuid(aId1);
    sampleAnswerEntity1.setText("This is an answer");
    JoinField<String> myAJoinField1 = new JoinField<>("answer");
    myAJoinField1.setParent(qId1);
    sampleAnswerEntity1.setMyJoinField(myAJoinField1);
    SampleJoinEntity sampleAnswerEntity2 = new SampleJoinEntity();
    sampleAnswerEntity2.setUuid(aId2);
    sampleAnswerEntity2.setText("This is another answer");
    JoinField<String> myAJoinField2 = new JoinField<>("answer");
    myAJoinField2.setParent(qId1);
    sampleAnswerEntity2.setMyJoinField(myAJoinField2);
    IndexCoordinates index = IndexCoordinates.of(indexNameProvider.indexName());
    operations.save(Arrays.asList(sampleQuestionEntity1, sampleQuestionEntity2, sampleAnswerEntity1, sampleAnswerEntity2), index);
    SearchHits<SampleJoinEntity> hits = operations.search(new NativeSearchQueryBuilder().withQuery(new ParentIdQueryBuilder("answer", qId1)).build(), SampleJoinEntity.class);
    List<String> hitIds = hits.getSearchHits().stream().map(sampleJoinEntitySearchHit -> sampleJoinEntitySearchHit.getId()).collect(Collectors.toList());
    assertThat(hitIds.size()).isEqualTo(2);
    assertThat(hitIds.containsAll(Arrays.asList(aId1, aId2))).isTrue();
    hits.forEach(searchHit -> {
        assertThat(searchHit.getRouting()).isEqualTo(qId1);
    });
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) GaussDecayFunctionBuilder(org.elasticsearch.index.query.functionscore.GaussDecayFunctionBuilder) SortBuilders(org.elasticsearch.search.sort.SortBuilders) AliasAction(org.springframework.data.elasticsearch.core.index.AliasAction) Version(org.springframework.data.annotation.Version) Autowired(org.springframework.beans.factory.annotation.Autowired) ScoreMode(org.springframework.data.elasticsearch.core.query.RescorerQuery.ScoreMode) Order(org.junit.jupiter.api.Order) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) Integer(java.lang.Integer) OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) ReindexRequest(org.springframework.data.elasticsearch.core.reindex.ReindexRequest) Long(java.lang.Long) Map(java.util.Map) Document(org.springframework.data.elasticsearch.annotations.Document) Assertions(org.assertj.core.api.Assertions) Pageable(org.springframework.data.domain.Pageable) Sort(org.springframework.data.domain.Sort) IndexBuilder(org.springframework.data.elasticsearch.utils.IndexBuilder) StreamUtils(org.springframework.data.util.StreamUtils) Integer(org.springframework.data.elasticsearch.annotations.FieldType.Integer) JoinTypeRelation(org.springframework.data.elasticsearch.annotations.JoinTypeRelation) org.springframework.data.elasticsearch.core.query(org.springframework.data.elasticsearch.core.query) IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates) Collection(java.util.Collection) PageRequest(org.springframework.data.domain.PageRequest) UUID(java.util.UUID) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) Explanation(org.springframework.data.elasticsearch.core.document.Explanation) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest) FieldType(org.springframework.data.elasticsearch.annotations.FieldType) List(java.util.List) CombineFunction(org.elasticsearch.common.lucene.search.function.CombineFunction) Lists(org.assertj.core.util.Lists) GeoPoint(org.springframework.data.elasticsearch.core.geo.GeoPoint) IdGenerator(org.springframework.data.elasticsearch.utils.IdGenerator) VersionType(org.springframework.data.elasticsearch.annotations.Document.VersionType) SortOrder(org.elasticsearch.search.sort.SortOrder) IndexNameProvider(org.springframework.data.elasticsearch.utils.IndexNameProvider) Id(org.springframework.data.annotation.Id) Document(org.springframework.data.elasticsearch.core.document.Document) FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext) IntStream(java.util.stream.IntStream) SoftAssertions(org.assertj.core.api.SoftAssertions) AliasActions(org.springframework.data.elasticsearch.core.index.AliasActions) Double(java.lang.Double) Field(org.springframework.data.elasticsearch.annotations.Field) ScriptType(org.elasticsearch.script.ScriptType) HashMap(java.util.HashMap) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) ScriptedField(org.springframework.data.elasticsearch.annotations.ScriptedField) SearchRequest(org.elasticsearch.action.search.SearchRequest) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder) JoinTypeRelations(org.springframework.data.elasticsearch.annotations.JoinTypeRelations) MultiField(org.springframework.data.elasticsearch.annotations.MultiField) FunctionScoreQueryBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder) Nullable(org.springframework.lang.Nullable) ParentIdQueryBuilder(org.elasticsearch.join.query.ParentIdQueryBuilder) FunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FunctionScoreQuery) CollapseBuilder(org.elasticsearch.search.collapse.CollapseBuilder) Script(org.elasticsearch.script.Script) Setting(org.springframework.data.elasticsearch.annotations.Setting) Iterator(java.util.Iterator) AliasActionParameters(org.springframework.data.elasticsearch.core.index.AliasActionParameters) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) DisplayName(org.junit.jupiter.api.DisplayName) JoinField(org.springframework.data.elasticsearch.core.join.JoinField) FilterFunctionBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder.FilterFunctionBuilder) Object(java.lang.Object) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) InnerField(org.springframework.data.elasticsearch.annotations.InnerField) Collections(java.util.Collections) ReindexResponse(org.springframework.data.elasticsearch.core.reindex.ReindexResponse) ParentIdQueryBuilder(org.elasticsearch.join.query.ParentIdQueryBuilder) JoinField(org.springframework.data.elasticsearch.core.join.JoinField) IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates)

Example 29 with IndexCoordinates

use of org.springframework.data.elasticsearch.core.mapping.IndexCoordinates in project spring-data-elasticsearch by spring-projects.

the class ElasticsearchTemplateTests method shouldRunRescoreQueryInSearchQuery.

// #1686
@Test
void shouldRunRescoreQueryInSearchQuery() {
    IndexCoordinates index = IndexCoordinates.of("test-index-rescore-entity-template");
    // matches main query better
    SampleEntity entity = // 
    SampleEntity.builder().id(// 
    "1").message(// 
    "some message").rate(java.lang.Integer.MAX_VALUE).version(// 
    System.currentTimeMillis()).build();
    // high score from rescore query
    SampleEntity entity2 = // 
    SampleEntity.builder().id(// 
    "2").message(// 
    "nothing").rate(1).version(// 
    System.currentTimeMillis()).build();
    List<IndexQuery> indexQueries = getIndexQueries(Arrays.asList(entity, entity2));
    operations.bulkIndex(indexQueries, index);
    NativeSearchQuery query = // 
    new NativeSearchQueryBuilder().withQuery(// 
    boolQuery().filter(existsQuery("rate")).should(termQuery("message", "message"))).withRescorerQuery(new RescorerQuery(new NativeSearchQueryBuilder().withQuery(QueryBuilders.functionScoreQuery(new FunctionScoreQueryBuilder.FilterFunctionBuilder[] { new FilterFunctionBuilder(new GaussDecayFunctionBuilder("rate", 0, 10, null, 0.5).setWeight(1f)), new FilterFunctionBuilder(new GaussDecayFunctionBuilder("rate", 0, 10, null, 0.5).setWeight(100f)) }).scoreMode(FunctionScoreQuery.ScoreMode.SUM).maxBoost(80f).boostMode(CombineFunction.REPLACE)).build()).withScoreMode(ScoreMode.Max).withWindowSize(100)).build();
    SearchHits<SampleEntity> searchHits = operations.search(query, SampleEntity.class, index);
    assertThat(searchHits).isNotNull();
    assertThat(searchHits.getSearchHits()).hasSize(2);
    SearchHit<SampleEntity> searchHit = searchHits.getSearchHit(0);
    assertThat(searchHit.getContent().getMessage()).isEqualTo("nothing");
    // score capped to 80
    assertThat(searchHit.getScore()).isEqualTo(80f);
}
Also used : GaussDecayFunctionBuilder(org.elasticsearch.index.query.functionscore.GaussDecayFunctionBuilder) FunctionScoreQueryBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder) FilterFunctionBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder.FilterFunctionBuilder) IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)

Example 30 with IndexCoordinates

use of org.springframework.data.elasticsearch.core.mapping.IndexCoordinates in project spring-data-elasticsearch by spring-projects.

the class ElasticsearchTemplateTests method shouldComposeObjectsReturnedFromHeterogeneousIndexes.

@Test
public /*
	 * This is basically a demonstration to show composing entities out of heterogeneous indexes.
	 */
void shouldComposeObjectsReturnedFromHeterogeneousIndexes() {
    IndexCoordinates index1 = IndexCoordinates.of(INDEX_1_NAME);
    IndexCoordinates index2 = IndexCoordinates.of(INDEX_2_NAME);
    operations.indexOps(index1).delete();
    operations.indexOps(index2).delete();
    HetroEntity1 entity1 = new HetroEntity1(nextIdAsString(), "aFirstName");
    HetroEntity2 entity2 = new HetroEntity2(nextIdAsString(), "aLastName");
    IndexQuery indexQuery1 = new IndexQueryBuilder().withId(entity1.getId()).withObject(entity1).build();
    IndexQuery indexQuery2 = new IndexQueryBuilder().withId(entity2.getId()).withObject(entity2).build();
    operations.index(indexQuery1, index1);
    operations.index(indexQuery2, index2);
    // when
    NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).build();
    SearchHits<ResultAggregator> page = operations.search(searchQuery, ResultAggregator.class, IndexCoordinates.of(INDEX_1_NAME, INDEX_2_NAME));
    assertThat(page.getTotalHits()).isEqualTo(2);
}
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