use of org.springframework.data.elasticsearch.core.mapping.IndexCoordinates 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.mapping.IndexCoordinates 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);
}
use of org.springframework.data.elasticsearch.core.mapping.IndexCoordinates 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.mapping.IndexCoordinates in project spring-data-elasticsearch by spring-projects.
the class SimpleElasticsearchRepository method deleteAll.
@Override
public void deleteAll() {
IndexCoordinates indexCoordinates = getIndexCoordinates();
Query query = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).build();
executeAndRefresh((OperationsCallback<Void>) operations -> {
operations.delete(query, entityClass, indexCoordinates);
return null;
});
}
use of org.springframework.data.elasticsearch.core.mapping.IndexCoordinates in project spring-data-elasticsearch by spring-projects.
the class ElasticsearchPartQuery method execute.
@Override
public Object execute(Object[] parameters) {
Class<?> clazz = queryMethod.getResultProcessor().getReturnedType().getDomainType();
ParametersParameterAccessor accessor = new ParametersParameterAccessor(queryMethod.getParameters(), parameters);
CriteriaQuery query = createQuery(accessor);
Assert.notNull(query, "unsupported query");
elasticsearchConverter.updateQuery(query, clazz);
if (queryMethod.hasAnnotatedHighlight()) {
query.setHighlightQuery(queryMethod.getAnnotatedHighlightQuery());
}
IndexCoordinates index = elasticsearchOperations.getIndexCoordinatesFor(clazz);
Object result = null;
if (tree.isLimiting()) {
// noinspection ConstantConditions
query.setMaxResults(tree.getMaxResults());
}
if (tree.isDelete()) {
result = countOrGetDocumentsForDelete(query, accessor);
elasticsearchOperations.delete(query, clazz, index);
elasticsearchOperations.indexOps(index).refresh();
} else if (queryMethod.isPageQuery()) {
query.setPageable(accessor.getPageable());
SearchHits<?> searchHits = elasticsearchOperations.search(query, clazz, index);
if (queryMethod.isSearchPageMethod()) {
result = SearchHitSupport.searchPageFor(searchHits, query.getPageable());
} else {
result = SearchHitSupport.unwrapSearchHits(SearchHitSupport.searchPageFor(searchHits, query.getPageable()));
}
} else if (queryMethod.isStreamQuery()) {
if (accessor.getPageable().isUnpaged()) {
query.setPageable(PageRequest.of(0, DEFAULT_STREAM_BATCH_SIZE));
} else {
query.setPageable(accessor.getPageable());
}
result = StreamUtils.createStreamFromIterator(elasticsearchOperations.searchForStream(query, clazz, index));
} else if (queryMethod.isCollectionQuery()) {
if (accessor.getPageable().isUnpaged()) {
int itemCount = (int) elasticsearchOperations.count(query, clazz, index);
if (itemCount == 0) {
result = new SearchHitsImpl<>(0, TotalHitsRelation.EQUAL_TO, Float.NaN, null, Collections.emptyList(), null, null);
} else {
query.setPageable(PageRequest.of(0, Math.max(1, itemCount)));
}
} else {
query.setPageable(accessor.getPageable());
}
if (result == null) {
result = elasticsearchOperations.search(query, clazz, index);
}
} else if (tree.isCountProjection()) {
result = elasticsearchOperations.count(query, clazz, index);
} else {
result = elasticsearchOperations.searchOne(query, clazz, index);
}
return (queryMethod.isNotSearchHitMethod() && queryMethod.isNotSearchPageMethod()) ? SearchHitSupport.unwrapSearchHits(result) : result;
}
Aggregations