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);
}
}
}
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)));
}
}
}
}
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");
}
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");
}
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);
}
Aggregations