Search in sources :

Example 1 with Query

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

the class CriteriaQueryMappingUnitTests method shouldMapNamesInSourceFieldsAndSourceFilters.

// #1778
@Test
@DisplayName("should map names in source fields and SourceFilters")
void shouldMapNamesInSourceFieldsAndSourceFilters() {
    Query query = Query.findAll();
    // Note: we don't care if these filters make sense here, this test is only about name mapping
    query.addFields("firstName", "lastName");
    query.addSourceFilter(new FetchSourceFilterBuilder().withIncludes("firstName").withExcludes("lastName").build());
    mappingElasticsearchConverter.updateQuery(query, Person.class);
    SoftAssertions softly = new SoftAssertions();
    softly.assertThat(query.getFields()).containsExactly("first-name", "last-name");
    SourceFilter sourceFilter = query.getSourceFilter();
    softly.assertThat(sourceFilter).isNotNull();
    softly.assertThat(sourceFilter.getIncludes()).containsExactly("first-name");
    softly.assertThat(sourceFilter.getExcludes()).containsExactly("last-name");
    softly.assertAll();
}
Also used : SourceFilter(org.springframework.data.elasticsearch.core.query.SourceFilter) Query(org.springframework.data.elasticsearch.core.query.Query) CriteriaQuery(org.springframework.data.elasticsearch.core.query.CriteriaQuery) SoftAssertions(org.assertj.core.api.SoftAssertions) FetchSourceFilterBuilder(org.springframework.data.elasticsearch.core.query.FetchSourceFilterBuilder) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 2 with Query

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

the class CriteriaQueryMappingUnitTests method shouldMapNamesInSourceStoredFields.

@Test
@DisplayName("should map names in source stored fields")
void shouldMapNamesInSourceStoredFields() {
    Query query = Query.findAll();
    query.addStoredFields("firstName", "lastName");
    mappingElasticsearchConverter.updateQuery(query, Person.class);
    SoftAssertions softly = new SoftAssertions();
    List<String> storedFields = query.getStoredFields();
    softly.assertThat(storedFields).isNotNull();
    softly.assertThat(storedFields).containsExactly("first-name", "last-name");
    softly.assertAll();
}
Also used : Query(org.springframework.data.elasticsearch.core.query.Query) CriteriaQuery(org.springframework.data.elasticsearch.core.query.CriteriaQuery) SoftAssertions(org.assertj.core.api.SoftAssertions) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 3 with Query

use of org.springframework.data.elasticsearch.core.query.Query 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 4 with Query

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

the class SimpleElasticsearchRepository method deleteAllById.

@Override
public void deleteAllById(Iterable<? extends ID> ids) {
    Assert.notNull(ids, "Cannot delete 'null' list.");
    List<String> idStrings = new ArrayList<>();
    for (ID id : ids) {
        idStrings.add(stringIdRepresentation(id));
    }
    if (idStrings.isEmpty()) {
        return;
    }
    Query query = operations.idsQuery(idStrings);
    executeAndRefresh((OperationsCallback<Void>) operations -> {
        operations.delete(query, entityClass, getIndexCoordinates());
        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) ArrayList(java.util.ArrayList)

Example 5 with Query

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

the class ReactiveSearchAfterIntegrationTests method shouldReadPagesWithSearchAfter.

// #1143
@Test
@DisplayName("should read pages with search_after")
void shouldReadPagesWithSearchAfter() {
    List<Entity> entities = IntStream.rangeClosed(1, 10).mapToObj(i -> new Entity((long) i, "message " + i)).collect(Collectors.toList());
    operations.saveAll(Mono.just(entities), Entity.class).blockLast();
    Query query = Query.findAll();
    query.setPageable(PageRequest.of(0, 3));
    query.addSort(Sort.by(Sort.Direction.ASC, "id"));
    List<Object> searchAfter = null;
    List<Entity> foundEntities = new ArrayList<>();
    int loop = 0;
    do {
        query.setSearchAfter(searchAfter);
        List<SearchHit<Entity>> searchHits = operations.search(query, Entity.class).collectList().block();
        if (searchHits.size() == 0) {
            break;
        }
        foundEntities.addAll(searchHits.stream().map(searchHit -> searchHit.getContent()).collect(Collectors.toList()));
        searchAfter = searchHits.get((int) (searchHits.size() - 1)).getSortValues();
        if (++loop > 10) {
            fail("loop not terminating");
        }
    } while (true);
    assertThat(foundEntities).containsExactlyElementsOf(entities);
}
Also used : IntStream(java.util.stream.IntStream) ReactiveElasticsearchOperations(org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations) Field(org.springframework.data.elasticsearch.annotations.Field) Autowired(org.springframework.beans.factory.annotation.Autowired) PageRequest(org.springframework.data.domain.PageRequest) Mono(reactor.core.publisher.Mono) Query(org.springframework.data.elasticsearch.core.query.Query) Collectors(java.util.stream.Collectors) ReactiveElasticsearchRestTemplateConfiguration(org.springframework.data.elasticsearch.junit.jupiter.ReactiveElasticsearchRestTemplateConfiguration) ArrayList(java.util.ArrayList) DisplayName(org.junit.jupiter.api.DisplayName) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest) List(java.util.List) FieldType(org.springframework.data.elasticsearch.annotations.FieldType) Document(org.springframework.data.elasticsearch.annotations.Document) ContextConfiguration(org.springframework.test.context.ContextConfiguration) Assertions(org.assertj.core.api.Assertions) Sort(org.springframework.data.domain.Sort) Nullable(org.springframework.lang.Nullable) Id(org.springframework.data.annotation.Id) SearchHit(org.springframework.data.elasticsearch.core.SearchHit) Query(org.springframework.data.elasticsearch.core.query.Query) SearchHit(org.springframework.data.elasticsearch.core.SearchHit) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

Query (org.springframework.data.elasticsearch.core.query.Query)44 Test (org.junit.jupiter.api.Test)22 SpringIntegrationTest (org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)17 NativeSearchQueryBuilder (org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder)16 IndexCoordinates (org.springframework.data.elasticsearch.core.mapping.IndexCoordinates)15 IndexQuery (org.springframework.data.elasticsearch.core.query.IndexQuery)15 DisplayName (org.junit.jupiter.api.DisplayName)14 ArrayList (java.util.ArrayList)13 List (java.util.List)10 CriteriaQuery (org.springframework.data.elasticsearch.core.query.CriteriaQuery)10 Collectors (java.util.stream.Collectors)9 MoreLikeThisQuery (org.springframework.data.elasticsearch.core.query.MoreLikeThisQuery)9 Nullable (org.springframework.lang.Nullable)9 UpdateQuery (org.springframework.data.elasticsearch.core.query.UpdateQuery)8 QueryBuilders (org.elasticsearch.index.query.QueryBuilders)7 HashMap (java.util.HashMap)6 Map (java.util.Map)6 MultiSearchRequest (org.elasticsearch.action.search.MultiSearchRequest)6 Log (org.apache.commons.logging.Log)5 LogFactory (org.apache.commons.logging.LogFactory)5