作品> =バージョン2.0
は、残念ながらソースフィルタリングはバージョン1.xでは利用できません
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.FetchSourceFilter;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SourceFilter;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
import java.util.List;
@Repository
public class MyRepository {
private final static String FIELD_MYFIELD = "MyNestedObj.myField";
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
public List<String> findAll() {
SourceFilter sourceFilter = new FetchSourceFilter(new String[]{FIELD_MYFIELD}, null);
final PageRequest pageRequest = new PageRequest(0, Integer.MAX_VALUE, new Sort(FIELD_MYFIELD));
final NativeSearchQuery query = new NativeSearchQueryBuilder()//
.withQuery(new MatchAllQueryBuilder()).withSourceFilter(sourceFilter)//
.withIndices("myIndex").withTypes("MyType").withPageable(pageRequest)//
.build();
return this.elasticsearchTemplate.query(query, response -> {
List<String> values = new ArrayList<>();
final SearchHits hits = response.getHits();
for (final SearchHit hit : hits) {
values .add(hit.getSource().get(FIELD_MYFIELD));
}
return values;
});
}
}