Search in sources :

Example 6 with IndexQuery

use of org.springframework.data.elasticsearch.core.query.IndexQuery in project spring-data-elasticsearch by spring-projects.

the class NestedObjectTests method shouldIndexAndSearchMapAsNestedType.

// DATAES-73
@Test
public void shouldIndexAndSearchMapAsNestedType() {
    // given
    Book book1 = new Book();
    Book book2 = new Book();
    book1.setId(nextIdAsString());
    book1.setName("testBook1");
    book2.setId(nextIdAsString());
    book2.setName("testBook2");
    Map<Integer, Collection<String>> map1 = new HashMap<>();
    map1.put(1, Arrays.asList("test1", "test2"));
    Map<Integer, Collection<String>> map2 = new HashMap<>();
    map2.put(1, Arrays.asList("test3", "test4"));
    book1.setBuckets(map1);
    book2.setBuckets(map2);
    List<IndexQuery> indexQueries = new ArrayList<>();
    IndexQuery indexQuery1 = new IndexQuery();
    indexQuery1.setId(book1.getId());
    indexQuery1.setObject(book1);
    IndexQuery indexQuery2 = new IndexQuery();
    indexQuery2.setId(book2.getId());
    indexQuery2.setObject(book2);
    indexQueries.add(indexQuery1);
    indexQueries.add(indexQuery2);
    // when
    IndexCoordinates index = IndexCoordinates.of("test-index-book-nested-objects");
    operations.bulkIndex(indexQueries, index);
    operations.indexOps(Book.class).refresh();
    // then
    NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(nestedQuery("buckets", termQuery("buckets.1", "test3"), ScoreMode.None)).build();
    SearchHits<Book> books = operations.search(searchQuery, Book.class, index);
    assertThat(books.getSearchHits()).hasSize(1);
    assertThat(books.getSearchHit(0).getContent().getId()).isEqualTo(book2.getId());
}
Also used : IndexQuery(org.springframework.data.elasticsearch.core.query.IndexQuery) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Collection(java.util.Collection) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates) NativeSearchQuery(org.springframework.data.elasticsearch.core.query.NativeSearchQuery) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)

Example 7 with IndexQuery

use of org.springframework.data.elasticsearch.core.query.IndexQuery in project spring-data-elasticsearch by spring-projects.

the class NestedObjectTests method createPerson.

private List<IndexQuery> createPerson() {
    PersonMultipleLevelNested person1 = new PersonMultipleLevelNested();
    person1.setId("1");
    person1.setName("name");
    Car saturn = new Car();
    saturn.setName("Saturn");
    saturn.setModel("SL");
    Car subaru = new Car();
    subaru.setName("Subaru");
    subaru.setModel("Imprezza");
    Car car = new Car();
    car.setName("Saturn");
    car.setModel("Imprezza");
    Car ford = new Car();
    ford.setName("Ford");
    ford.setModel("Focus");
    GirlFriend permanent = new GirlFriend();
    permanent.setName("permanent");
    permanent.setType("permanent");
    permanent.setCars(Arrays.asList(saturn, subaru));
    GirlFriend temp = new GirlFriend();
    temp.setName("temp");
    temp.setType("temp");
    temp.setCars(Arrays.asList(car, ford));
    person1.setGirlFriends(Arrays.asList(permanent, temp));
    IndexQuery indexQuery1 = new IndexQuery();
    indexQuery1.setId(person1.getId());
    indexQuery1.setObject(person1);
    PersonMultipleLevelNested person2 = new PersonMultipleLevelNested();
    person2.setId("2");
    person2.setName("name");
    person2.setGirlFriends(Collections.singletonList(permanent));
    IndexQuery indexQuery2 = new IndexQuery();
    indexQuery2.setId(person2.getId());
    indexQuery2.setObject(person2);
    List<IndexQuery> indexQueries = new ArrayList<>();
    indexQueries.add(indexQuery1);
    indexQueries.add(indexQuery2);
    return indexQueries;
}
Also used : IndexQuery(org.springframework.data.elasticsearch.core.query.IndexQuery) ArrayList(java.util.ArrayList)

Example 8 with IndexQuery

use of org.springframework.data.elasticsearch.core.query.IndexQuery in project spring-data-elasticsearch by spring-projects.

the class ReactiveElasticsearchTemplate method getIndexQuery.

private IndexQuery getIndexQuery(Object value) {
    AdaptibleEntity<?> entity = operations.forEntity(value, converter.getConversionService(), routingResolver);
    Object id = entity.getId();
    IndexQuery query = new IndexQuery();
    if (id != null) {
        query.setId(id.toString());
    }
    query.setObject(value);
    boolean usingSeqNo = false;
    if (entity.hasSeqNoPrimaryTerm()) {
        SeqNoPrimaryTerm seqNoPrimaryTerm = entity.getSeqNoPrimaryTerm();
        if (seqNoPrimaryTerm != null) {
            query.setSeqNo(seqNoPrimaryTerm.getSequenceNumber());
            query.setPrimaryTerm(seqNoPrimaryTerm.getPrimaryTerm());
            usingSeqNo = true;
        }
    }
    // seq_no and version are incompatible in the same request
    if (!usingSeqNo && entity.isVersionedEntity()) {
        Number version = entity.getVersion();
        if (version != null) {
            query.setVersion(version.longValue());
        }
    }
    query.setRouting(entity.getRouting());
    return query;
}
Also used : IndexQuery(org.springframework.data.elasticsearch.core.query.IndexQuery) SeqNoPrimaryTerm(org.springframework.data.elasticsearch.core.query.SeqNoPrimaryTerm)

Example 9 with IndexQuery

use of org.springframework.data.elasticsearch.core.query.IndexQuery in project spring-data-elasticsearch by spring-projects.

the class SearchAsYouTypeTests method loadEntities.

private void loadEntities() {
    List<IndexQuery> indexQueries = new ArrayList<>();
    indexQueries.add(new SearchAsYouTypeEntity("1", "test 1", "test 1234").toIndex());
    indexQueries.add(new SearchAsYouTypeEntity("2", "test 2", "test 5678").toIndex());
    indexQueries.add(new SearchAsYouTypeEntity("3", "test 3", "asd 5678").toIndex());
    indexQueries.add(new SearchAsYouTypeEntity("4", "test 4", "not match").toIndex());
    IndexCoordinates index = IndexCoordinates.of("test-index-core-search-as-you-type");
    operations.bulkIndex(indexQueries, index);
    operations.indexOps(SearchAsYouTypeEntity.class).refresh();
}
Also used : IndexQuery(org.springframework.data.elasticsearch.core.query.IndexQuery) ArrayList(java.util.ArrayList) IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates)

Example 10 with IndexQuery

use of org.springframework.data.elasticsearch.core.query.IndexQuery in project spring-data-elasticsearch by spring-projects.

the class ElasticsearchOperationsCallbackIntegrationTests method shouldApplyConversionResultToIndexQueryOnSave.

// DATAES-972
@Test
@DisplayName("should apply conversion result to IndexQuery on save")
void shouldApplyConversionResultToIndexQueryOnSave() {
    SampleEntity entity = new SampleEntity("1", "test");
    operations.save(entity);
    ArgumentCaptor<IndexQuery> indexQueryCaptor = ArgumentCaptor.forClass(IndexQuery.class);
    verify(operations, times(2)).doIndex(indexQueryCaptor.capture(), any());
    final IndexQuery capturedIndexQuery = indexQueryCaptor.getValue();
    SampleEntity convertedEntity = (SampleEntity) capturedIndexQuery.getObject();
    final JoinField<String> joinField = convertedEntity.getJoinField();
    assertThat(joinField.getName()).isEqualTo("answer");
    assertThat(joinField.getParent()).isEqualTo("42");
    assertThat(capturedIndexQuery.getRouting()).isEqualTo("42");
    assertThat(capturedIndexQuery.getSeqNo()).isEqualTo(seqNoPrimaryTerm.getSequenceNumber());
    assertThat(capturedIndexQuery.getPrimaryTerm()).isEqualTo(seqNoPrimaryTerm.getPrimaryTerm());
}
Also used : IndexQuery(org.springframework.data.elasticsearch.core.query.IndexQuery) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

IndexQuery (org.springframework.data.elasticsearch.core.query.IndexQuery)31 Test (org.junit.jupiter.api.Test)12 SpringIntegrationTest (org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)9 ArrayList (java.util.ArrayList)8 IndexCoordinates (org.springframework.data.elasticsearch.core.mapping.IndexCoordinates)8 DisplayName (org.junit.jupiter.api.DisplayName)4 IndexQueryBuilder (org.springframework.data.elasticsearch.core.query.IndexQueryBuilder)4 NativeSearchQuery (org.springframework.data.elasticsearch.core.query.NativeSearchQuery)4 NativeSearchQueryBuilder (org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder)4 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)3 HashMap (java.util.HashMap)2 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 SeqNoPrimaryTerm (org.springframework.data.elasticsearch.core.query.SeqNoPrimaryTerm)2 Commodity (com.example.esdemo.dto.Commodity)1 CommonException (com.huaxing.springboot_elasticsearch.common.utils.CommonException)1 UserEntity (com.javayh.elaticsearh.docment.UserEntity)1 Field (java.lang.reflect.Field)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Collection (java.util.Collection)1