Search in sources :

Example 1 with ParentIdQueryBuilder

use of org.elasticsearch.join.query.ParentIdQueryBuilder in project spring-data-elasticsearch by spring-projects.

the class ElasticsearchTemplateTests method shouldUpdateEntityWithJoinFields.

private void shouldUpdateEntityWithJoinFields(String qId1, String qId2, String aId1, String aId2) throws Exception {
    org.springframework.data.elasticsearch.core.document.Document document = org.springframework.data.elasticsearch.core.document.Document.create();
    document.put("myJoinField", toDocument(new JoinField<>("answer", qId2)));
    UpdateQuery updateQuery = // 
    UpdateQuery.builder(aId2).withDocument(// 
    document).withRouting(qId2).build();
    List<UpdateQuery> queries = new ArrayList<>();
    queries.add(updateQuery);
    // when
    operations.bulkUpdate(queries, IndexCoordinates.of(indexNameProvider.indexName()));
    SearchHits<SampleJoinEntity> updatedHits = operations.search(new NativeSearchQueryBuilder().withQuery(new ParentIdQueryBuilder("answer", qId2)).build(), SampleJoinEntity.class);
    List<String> hitIds = updatedHits.getSearchHits().stream().map(new Function<SearchHit<SampleJoinEntity>, String>() {

        @Override
        public String apply(SearchHit<SampleJoinEntity> sampleJoinEntitySearchHit) {
            return sampleJoinEntitySearchHit.getId();
        }
    }).collect(Collectors.toList());
    assertThat(hitIds.size()).isEqualTo(1);
    assertThat(hitIds.get(0)).isEqualTo(aId2);
    updatedHits = operations.search(new NativeSearchQueryBuilder().withQuery(new ParentIdQueryBuilder("answer", qId1)).build(), SampleJoinEntity.class);
    hitIds = updatedHits.getSearchHits().stream().map(new Function<SearchHit<SampleJoinEntity>, String>() {

        @Override
        public String apply(SearchHit<SampleJoinEntity> sampleJoinEntitySearchHit) {
            return sampleJoinEntitySearchHit.getId();
        }
    }).collect(Collectors.toList());
    assertThat(hitIds.size()).isEqualTo(1);
    assertThat(hitIds.get(0)).isEqualTo(aId1);
}
Also used : ParentIdQueryBuilder(org.elasticsearch.join.query.ParentIdQueryBuilder) ArrayList(java.util.ArrayList) JoinField(org.springframework.data.elasticsearch.core.join.JoinField) CombineFunction(org.elasticsearch.common.lucene.search.function.CombineFunction) Function(java.util.function.Function) Document(org.springframework.data.elasticsearch.core.document.Document)

Example 2 with ParentIdQueryBuilder

use of org.elasticsearch.join.query.ParentIdQueryBuilder in project spring-data-elasticsearch by spring-projects.

the class ElasticsearchTemplateTests method shouldDeleteEntityWithJoinFields.

private void shouldDeleteEntityWithJoinFields(String qId2, String aId2) throws Exception {
    Query query = new NativeSearchQueryBuilder().withQuery(new ParentIdQueryBuilder("answer", qId2)).withRoute(qId2).build();
    operations.delete(query, SampleJoinEntity.class, IndexCoordinates.of(indexNameProvider.indexName()));
    SearchHits<SampleJoinEntity> deletedHits = operations.search(new NativeSearchQueryBuilder().withQuery(new ParentIdQueryBuilder("answer", qId2)).build(), SampleJoinEntity.class);
    List<String> hitIds = deletedHits.getSearchHits().stream().map(new Function<SearchHit<SampleJoinEntity>, String>() {

        @Override
        public String apply(SearchHit<SampleJoinEntity> sampleJoinEntitySearchHit) {
            return sampleJoinEntitySearchHit.getId();
        }
    }).collect(Collectors.toList());
    assertThat(hitIds.size()).isEqualTo(0);
}
Also used : CombineFunction(org.elasticsearch.common.lucene.search.function.CombineFunction) Function(java.util.function.Function) FunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FunctionScoreQuery) ParentIdQueryBuilder(org.elasticsearch.join.query.ParentIdQueryBuilder)

Example 3 with ParentIdQueryBuilder

use of org.elasticsearch.join.query.ParentIdQueryBuilder 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)

Aggregations

Function (java.util.function.Function)3 CombineFunction (org.elasticsearch.common.lucene.search.function.CombineFunction)3 ParentIdQueryBuilder (org.elasticsearch.join.query.ParentIdQueryBuilder)3 ArrayList (java.util.ArrayList)2 FunctionScoreQuery (org.elasticsearch.common.lucene.search.function.FunctionScoreQuery)2 Double (java.lang.Double)1 Integer (java.lang.Integer)1 Long (java.lang.Long)1 Object (java.lang.Object)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 UUID (java.util.UUID)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 Assertions (org.assertj.core.api.Assertions)1