Search in sources :

Example 1 with FetchSourceFilterBuilder

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

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

the class SourceFilterIntegrationTests method shouldOnlyReturnRequestedFieldsOnGMultiGet.

// #1659, #1678
@Test
@DisplayName("should only return requested fields on multiget")
void shouldOnlyReturnRequestedFieldsOnGMultiGet() {
    // multiget has no fields, need sourcefilter here
    Query query = Query.multiGetQuery(Collections.singleton("42"));
    // 
    query.addSourceFilter(new FetchSourceFilterBuilder().withIncludes("field2").build());
    List<MultiGetItem<Entity>> entities = operations.multiGet(query, Entity.class);
    assertThat(entities).hasSize(1);
    Entity entity = entities.get(0).getItem();
    assertThat(entity.getField1()).isNull();
    assertThat(entity.getField2()).isEqualTo("two");
    assertThat(entity.getField3()).isNull();
}
Also used : Query(org.springframework.data.elasticsearch.core.query.Query) FetchSourceFilterBuilder(org.springframework.data.elasticsearch.core.query.FetchSourceFilterBuilder) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

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