Search in sources :

Example 1 with IndexQuery

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

the class AbstractElasticsearchTemplate method maybeCallbackAfterSaveWithQuery.

protected void maybeCallbackAfterSaveWithQuery(Object query, IndexCoordinates index) {
    if (query instanceof IndexQuery) {
        IndexQuery indexQuery = (IndexQuery) query;
        Object queryObject = indexQuery.getObject();
        if (queryObject != null) {
            queryObject = maybeCallbackAfterSave(queryObject, index);
            indexQuery.setObject(queryObject);
        }
    }
}
Also used : IndexQuery(org.springframework.data.elasticsearch.core.query.IndexQuery)

Example 2 with IndexQuery

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

the class AbstractElasticsearchTemplate method updateIndexedObjectsWithQueries.

// endregion
protected void updateIndexedObjectsWithQueries(List<?> queries, List<IndexedObjectInformation> indexedObjectInformationList) {
    for (int i = 0; i < queries.size(); i++) {
        Object query = queries.get(i);
        if (query instanceof IndexQuery) {
            IndexQuery indexQuery = (IndexQuery) query;
            Object queryObject = indexQuery.getObject();
            if (queryObject != null) {
                indexQuery.setObject(updateIndexedObject(queryObject, indexedObjectInformationList.get(i)));
            }
        }
    }
}
Also used : IndexQuery(org.springframework.data.elasticsearch.core.query.IndexQuery)

Example 3 with IndexQuery

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

the class ElasticsearchTemplateCallbackTests method bulkIndexWithOptionsShouldInvokeAfterSaveCallbacks.

// DATAES-771
@Test
void bulkIndexWithOptionsShouldInvokeAfterSaveCallbacks() {
    template.setEntityCallbacks(EntityCallbacks.create(afterSaveCallback));
    Person entity1 = new Person("init1", "luke1");
    Person entity2 = new Person("init2", "luke2");
    IndexQuery query1 = indexQueryForEntity(entity1);
    IndexQuery query2 = indexQueryForEntity(entity2);
    template.bulkIndex(Arrays.asList(query1, query2), BulkOptions.defaultOptions(), index);
    verify(afterSaveCallback, times(2)).onAfterSave(any(), eq(index));
    Person savedPerson1 = (Person) query1.getObject();
    Person savedPerson2 = (Person) query2.getObject();
    assertThat(savedPerson1.firstname).isEqualTo("after-save");
    assertThat(savedPerson2.firstname).isEqualTo("after-save");
}
Also used : IndexQuery(org.springframework.data.elasticsearch.core.query.IndexQuery) Test(org.junit.jupiter.api.Test)

Example 4 with IndexQuery

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

the class NestedObjectTests method shouldSearchUsingNestedQueryOnMultipleLevelNestedObject.

@Test
public void shouldSearchUsingNestedQueryOnMultipleLevelNestedObject() {
    // given
    List<IndexQuery> indexQueries = createPerson();
    // when
    IndexCoordinates index = IndexCoordinates.of("test-index-person-multiple-level-nested");
    operations.bulkIndex(indexQueries, index);
    operations.indexOps(PersonMultipleLevelNested.class).refresh();
    // then
    BoolQueryBuilder builder = boolQuery();
    builder.must(nestedQuery("girlFriends", termQuery("girlFriends.type", "temp"), ScoreMode.None)).must(nestedQuery("girlFriends.cars", termQuery("girlFriends.cars.name", "Ford".toLowerCase()), ScoreMode.None));
    NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build();
    SearchHits<PersonMultipleLevelNested> personIndexed = operations.search(searchQuery, PersonMultipleLevelNested.class, index);
    assertThat(personIndexed).isNotNull();
    assertThat(personIndexed.getTotalHits()).isEqualTo(1);
    assertThat(personIndexed.getSearchHit(0).getContent().getId()).isEqualTo("1");
}
Also used : IndexQuery(org.springframework.data.elasticsearch.core.query.IndexQuery) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) 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 5 with IndexQuery

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

the class NestedObjectTests method shouldSearchBooksForPersonInitialLevelNestedType.

@Test
public void shouldSearchBooksForPersonInitialLevelNestedType() {
    // given
    List<Car> cars = new ArrayList<>();
    Car saturn = new Car();
    saturn.setName("Saturn");
    saturn.setModel("SL");
    Car subaru = new Car();
    subaru.setName("Subaru");
    subaru.setModel("Imprezza");
    Car ford = new Car();
    ford.setName("Ford");
    ford.setModel("Focus");
    cars.add(saturn);
    cars.add(subaru);
    cars.add(ford);
    Book java = new Book();
    java.setId("1");
    java.setName("java");
    Author javaAuthor = new Author();
    javaAuthor.setId("1");
    javaAuthor.setName("javaAuthor");
    java.setAuthor(javaAuthor);
    Book spring = new Book();
    spring.setId("2");
    spring.setName("spring");
    Author springAuthor = new Author();
    springAuthor.setId("2");
    springAuthor.setName("springAuthor");
    spring.setAuthor(springAuthor);
    Person foo = new Person();
    foo.setName("Foo");
    foo.setId("1");
    foo.setCar(cars);
    foo.setBooks(Arrays.asList(java, spring));
    Car car = new Car();
    car.setName("Saturn");
    car.setModel("Imprezza");
    Person bar = new Person();
    bar.setId("2");
    bar.setName("Bar");
    bar.setCar(Collections.singletonList(car));
    List<IndexQuery> indexQueries = new ArrayList<>();
    IndexQuery indexQuery1 = new IndexQuery();
    indexQuery1.setId(foo.getId());
    indexQuery1.setObject(foo);
    IndexQuery indexQuery2 = new IndexQuery();
    indexQuery2.setId(bar.getId());
    indexQuery2.setObject(bar);
    indexQueries.add(indexQuery1);
    indexQueries.add(indexQuery2);
    IndexCoordinates index = IndexCoordinates.of("test-index-person");
    operations.bulkIndex(indexQueries, index);
    operations.indexOps(Person.class).refresh();
    // when
    QueryBuilder builder = nestedQuery("books", boolQuery().must(termQuery("books.name", "java")), ScoreMode.None);
    NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build();
    SearchHits<Person> persons = operations.search(searchQuery, Person.class, index);
    // then
    assertThat(persons).hasSize(1);
}
Also used : IndexQuery(org.springframework.data.elasticsearch.core.query.IndexQuery) ArrayList(java.util.ArrayList) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) NativeSearchQuery(org.springframework.data.elasticsearch.core.query.NativeSearchQuery) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)

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