Example usage for org.springframework.data.elasticsearch.core.query FetchSourceFilterBuilder withIncludes

List of usage examples for org.springframework.data.elasticsearch.core.query FetchSourceFilterBuilder withIncludes

Introduction

In this page you can find the example usage for org.springframework.data.elasticsearch.core.query FetchSourceFilterBuilder withIncludes.

Prototype

public FetchSourceFilterBuilder withIncludes(String... includes) 

Source Link

Usage

From source file:com.github.vanroy.springdata.jest.JestElasticsearchTemplateTests.java

@Test
public void shouldReturnFieldsBasedOnSourceFilter() {
    // given/* w  ww .  j  a v  a 2 s . com*/
    String documentId = randomNumeric(5);
    String message = "some test message";
    SampleEntity sampleEntity = SampleEntity.builder().id(documentId).message(message)
            .version(System.currentTimeMillis()).build();

    IndexQuery indexQuery = getIndexQuery(sampleEntity);

    elasticsearchTemplate.index(indexQuery);
    elasticsearchTemplate.refresh(SampleEntity.class);

    FetchSourceFilterBuilder sourceFilter = new FetchSourceFilterBuilder();
    sourceFilter.withIncludes("message");

    SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).withIndices(INDEX_NAME)
            .withTypes(TYPE_NAME).withSourceFilter(sourceFilter.build()).build();
    // when
    Page<SampleEntity> page = elasticsearchTemplate.queryForPage(searchQuery, SampleEntity.class);
    // then
    assertThat(page, is(notNullValue()));
    assertThat(page.getTotalElements(), is(equalTo(1L)));
    assertThat(page.getContent().get(0).getMessage(), is(message));
}