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