Search in sources :

Example 16 with IndexQuery

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

the class ElasticsearchTemplateCallbackTests method indexShouldInvokeAfterSaveCallbacks.

// DATAES-771
@Test
void indexShouldInvokeAfterSaveCallbacks() {
    template.setEntityCallbacks(EntityCallbacks.create(afterSaveCallback));
    Person entity = new Person("init", "luke");
    IndexQuery indexQuery = indexQueryForEntity(entity);
    template.index(indexQuery, index);
    verify(afterSaveCallback).onAfterSave(eq(entity), eq(index));
    Person savedPerson = (Person) indexQuery.getObject();
    assertThat(savedPerson.firstname).isEqualTo("after-save");
}
Also used : IndexQuery(org.springframework.data.elasticsearch.core.query.IndexQuery) Test(org.junit.jupiter.api.Test)

Example 17 with IndexQuery

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

the class ElasticsearchTemplateCallbackTests method bulkIndexShouldInvokeAfterSaveCallbacks.

// DATAES-771
@Test
void bulkIndexShouldInvokeAfterSaveCallbacks() {
    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), 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 18 with IndexQuery

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

the class LogEntityIntegrationTests method before.

@BeforeEach
public void before() throws ParseException {
    indexOperations = operations.indexOps(LogEntity.class);
    IndexInitializer.init(indexOperations);
    SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    IndexQuery indexQuery1 = new LogEntityBuilder("1").action("update").date(dateFormatter.parse("2013-10-18 18:01")).code(2).ip("10.10.10.1").buildIndex();
    IndexQuery indexQuery2 = new LogEntityBuilder("2").action("update").date(dateFormatter.parse("2013-10-19 18:02")).code(2).ip("10.10.10.2").buildIndex();
    IndexQuery indexQuery3 = new LogEntityBuilder("3").action("update").date(dateFormatter.parse("2013-10-19 18:03")).code(2).ip("10.10.10.3").buildIndex();
    IndexQuery indexQuery4 = new LogEntityBuilder("4").action("update").date(dateFormatter.parse("2013-10-19 18:04")).code(2).ip("10.10.10.4").buildIndex();
    operations.bulkIndex(Arrays.asList(indexQuery1, indexQuery2, indexQuery3, indexQuery4), index);
    indexOperations.refresh();
}
Also used : IndexQuery(org.springframework.data.elasticsearch.core.query.IndexQuery) SimpleDateFormat(java.text.SimpleDateFormat) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 19 with IndexQuery

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

the class CompletionIntegrationTests method loadCompletionObjectEntities.

private void loadCompletionObjectEntities() {
    List<IndexQuery> indexQueries = new ArrayList<>();
    indexQueries.add(new CompletionEntityBuilder("1").name("Rizwan Idrees").suggest(new String[] { "Rizwan Idrees" }).buildIndex());
    indexQueries.add(new CompletionEntityBuilder("2").name("Franck Marchand").suggest(new String[] { "Franck", "Marchand" }).buildIndex());
    indexQueries.add(new CompletionEntityBuilder("3").name("Mohsin Husen").suggest(new String[] { "Mohsin", "Husen" }).buildIndex());
    indexQueries.add(new CompletionEntityBuilder("4").name("Artur Konczak").suggest(new String[] { "Artur", "Konczak" }).buildIndex());
    IndexCoordinates index = IndexCoordinates.of("test-index-core-completion");
    operations.bulkIndex(indexQueries, index);
    operations.indexOps(CompletionEntity.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 20 with IndexQuery

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

the class IndexBuilder method buildIndex.

public static IndexQuery buildIndex(Object object) {
    for (Field f : object.getClass().getDeclaredFields()) {
        if (AnnotationUtils.findAnnotation(f, Id.class) != null) {
            try {
                f.setAccessible(true);
                IndexQuery indexQuery = new IndexQuery();
                indexQuery.setId((String) f.get(object));
                indexQuery.setObject(object);
                return indexQuery;
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
    throw new RuntimeException("Missing @Id field");
}
Also used : Field(java.lang.reflect.Field) IndexQuery(org.springframework.data.elasticsearch.core.query.IndexQuery) Id(org.springframework.data.annotation.Id)

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