Search in sources :

Example 1 with IndexCoordinates

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");
}
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 2 with IndexCoordinates

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

Example 3 with IndexCoordinates

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());
}
Also used : IndexQuery(org.springframework.data.elasticsearch.core.query.IndexQuery) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Collection(java.util.Collection) 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 4 with IndexCoordinates

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;
    });
}
Also used : AbstractElasticsearchTemplate(org.springframework.data.elasticsearch.core.AbstractElasticsearchTemplate) MoreLikeThisQuery(org.springframework.data.elasticsearch.core.query.MoreLikeThisQuery) Query(org.springframework.data.elasticsearch.core.query.Query) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) ElasticsearchPersistentEntity(org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity) ArrayList(java.util.ArrayList) IndexOperations(org.springframework.data.elasticsearch.core.IndexOperations) Pageable(org.springframework.data.domain.Pageable) Sort(org.springframework.data.domain.Sort) Nullable(org.springframework.lang.Nullable) ElasticsearchRepository(org.springframework.data.elasticsearch.repository.ElasticsearchRepository) StreamUtils(org.springframework.data.util.StreamUtils) SearchHit(org.springframework.data.elasticsearch.core.SearchHit) RefreshPolicy(org.springframework.data.elasticsearch.core.RefreshPolicy) IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates) SearchHits(org.springframework.data.elasticsearch.core.SearchHits) PageRequest(org.springframework.data.domain.PageRequest) SearchPage(org.springframework.data.elasticsearch.core.SearchPage) Page(org.springframework.data.domain.Page) Collectors(java.util.stream.Collectors) SearchHitSupport(org.springframework.data.elasticsearch.core.SearchHitSupport) List(java.util.List) CollectionUtils(org.springframework.util.CollectionUtils) Streamable(org.springframework.data.util.Streamable) ElasticsearchOperations(org.springframework.data.elasticsearch.core.ElasticsearchOperations) Optional(java.util.Optional) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) NativeSearchQuery(org.springframework.data.elasticsearch.core.query.NativeSearchQuery) Collections(java.util.Collections) PageImpl(org.springframework.data.domain.PageImpl) MultiGetItem(org.springframework.data.elasticsearch.core.MultiGetItem) Assert(org.springframework.util.Assert) MoreLikeThisQuery(org.springframework.data.elasticsearch.core.query.MoreLikeThisQuery) Query(org.springframework.data.elasticsearch.core.query.Query) NativeSearchQuery(org.springframework.data.elasticsearch.core.query.NativeSearchQuery) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates)

Example 5 with IndexCoordinates

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;
}
Also used : ParametersParameterAccessor(org.springframework.data.repository.query.ParametersParameterAccessor) CriteriaQuery(org.springframework.data.elasticsearch.core.query.CriteriaQuery) SearchHitsImpl(org.springframework.data.elasticsearch.core.SearchHitsImpl) SearchHits(org.springframework.data.elasticsearch.core.SearchHits) IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates)

Aggregations

IndexCoordinates (org.springframework.data.elasticsearch.core.mapping.IndexCoordinates)50 Test (org.junit.jupiter.api.Test)27 SpringIntegrationTest (org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)27 IndexQuery (org.springframework.data.elasticsearch.core.query.IndexQuery)20 NativeSearchQueryBuilder (org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder)16 Query (org.springframework.data.elasticsearch.core.query.Query)16 ArrayList (java.util.ArrayList)15 Nullable (org.springframework.lang.Nullable)11 HashMap (java.util.HashMap)10 List (java.util.List)10 Collectors (java.util.stream.Collectors)10 QueryBuilders (org.elasticsearch.index.query.QueryBuilders)10 Map (java.util.Map)9 SearchRequest (org.elasticsearch.action.search.SearchRequest)9 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)9 Log (org.apache.commons.logging.Log)8 LogFactory (org.apache.commons.logging.LogFactory)8 Version (org.elasticsearch.Version)8 DocWriteResponse (org.elasticsearch.action.DocWriteResponse)8 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)8