Search in sources :

Example 1 with SourceFilter

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

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

the class MappingElasticsearchConverter method updateFieldsAndSourceFilter.

private void updateFieldsAndSourceFilter(Query query, Class<?> domainClass) {
    ElasticsearchPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(domainClass);
    if (persistentEntity != null) {
        List<String> fields = query.getFields();
        if (!fields.isEmpty()) {
            query.setFields(updateFieldNames(fields, persistentEntity));
        }
        List<String> storedFields = query.getStoredFields();
        if (!CollectionUtils.isEmpty(storedFields)) {
            query.setStoredFields(updateFieldNames(storedFields, persistentEntity));
        }
        SourceFilter sourceFilter = query.getSourceFilter();
        if (sourceFilter != null) {
            String[] includes = null;
            String[] excludes = null;
            if (sourceFilter.getIncludes() != null) {
                includes = updateFieldNames(Arrays.asList(sourceFilter.getIncludes()), persistentEntity).toArray(new String[] {});
            }
            if (sourceFilter.getExcludes() != null) {
                excludes = updateFieldNames(Arrays.asList(sourceFilter.getExcludes()), persistentEntity).toArray(new String[] {});
            }
            query.addSourceFilter(new FetchSourceFilter(includes, excludes));
        }
    }
}
Also used : FetchSourceFilter(org.springframework.data.elasticsearch.core.query.FetchSourceFilter) SourceFilter(org.springframework.data.elasticsearch.core.query.SourceFilter) FetchSourceFilter(org.springframework.data.elasticsearch.core.query.FetchSourceFilter)

Example 3 with SourceFilter

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

the class SourceFilterIntegrationTests method shouldOnlyReturnIncludedFieldsFromSourceFilterOnSearch.

// #1659
@Test
@DisplayName("should only return included fields from SourceFilter on search")
void shouldOnlyReturnIncludedFieldsFromSourceFilterOnSearch() {
    Query query = Query.findAll();
    query.addSourceFilter(new SourceFilter() {

        @Override
        public String[] getIncludes() {
            return new String[] { "field2" };
        }

        @Override
        public String[] getExcludes() {
            return new String[] {};
        }
    });
    SearchHits<Entity> entities = operations.search(query, Entity.class);
    assertThat(entities).hasSize(1);
    Entity entity = entities.getSearchHit(0).getContent();
    assertThat(entity.getField1()).isNull();
    assertThat(entity.getField2()).isNotNull();
    assertThat(entity.getField3()).isNull();
}
Also used : SourceFilter(org.springframework.data.elasticsearch.core.query.SourceFilter) Query(org.springframework.data.elasticsearch.core.query.Query) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest) DisplayName(org.junit.jupiter.api.DisplayName)

Example 4 with SourceFilter

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

the class SourceFilterIntegrationTests method shouldNotReturnExcludedFieldsFromSourceFilterOnMultiGet.

// #1659, #1678
@Test
@DisplayName("should not return excluded fields from SourceFilter on multiget")
void shouldNotReturnExcludedFieldsFromSourceFilterOnMultiGet() {
    Query query = Query.multiGetQuery(Collections.singleton("42"));
    query.addSourceFilter(new SourceFilter() {

        @Override
        public String[] getIncludes() {
            return new String[] {};
        }

        @Override
        public String[] getExcludes() {
            return new String[] { "field2" };
        }
    });
    List<MultiGetItem<Entity>> entities = operations.multiGet(query, Entity.class);
    assertThat(entities).hasSize(1);
    Entity entity = entities.get(0).getItem();
    assertThat(entity.getField1()).isNotNull();
    assertThat(entity.getField2()).isNull();
    assertThat(entity.getField3()).isNotNull();
}
Also used : SourceFilter(org.springframework.data.elasticsearch.core.query.SourceFilter) Query(org.springframework.data.elasticsearch.core.query.Query) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest) DisplayName(org.junit.jupiter.api.DisplayName)

Example 5 with SourceFilter

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

the class SourceFilterIntegrationTests method shouldNotReturnExcludedFieldsFromSourceFilterOnSearch.

// #1659
@Test
@DisplayName("should not return excluded fields from SourceFilter on search")
void shouldNotReturnExcludedFieldsFromSourceFilterOnSearch() {
    Query query = Query.findAll();
    query.addSourceFilter(new SourceFilter() {

        @Override
        public String[] getIncludes() {
            return new String[] {};
        }

        @Override
        public String[] getExcludes() {
            return new String[] { "field2" };
        }
    });
    SearchHits<Entity> entities = operations.search(query, Entity.class);
    assertThat(entities).hasSize(1);
    Entity entity = entities.getSearchHit(0).getContent();
    assertThat(entity.getField1()).isNotNull();
    assertThat(entity.getField2()).isNull();
    assertThat(entity.getField3()).isNotNull();
}
Also used : SourceFilter(org.springframework.data.elasticsearch.core.query.SourceFilter) Query(org.springframework.data.elasticsearch.core.query.Query) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

SourceFilter (org.springframework.data.elasticsearch.core.query.SourceFilter)6 DisplayName (org.junit.jupiter.api.DisplayName)5 Test (org.junit.jupiter.api.Test)5 Query (org.springframework.data.elasticsearch.core.query.Query)5 SpringIntegrationTest (org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)4 SoftAssertions (org.assertj.core.api.SoftAssertions)1 CriteriaQuery (org.springframework.data.elasticsearch.core.query.CriteriaQuery)1 FetchSourceFilter (org.springframework.data.elasticsearch.core.query.FetchSourceFilter)1 FetchSourceFilterBuilder (org.springframework.data.elasticsearch.core.query.FetchSourceFilterBuilder)1