Description
I created NativeSearchQuery
using NativeSearchQueryBuilder
, but when I used elasticsearchRestTemplate.multiGet
method , I found that the multiGet
method discarded the fields
attribute.
In ElasticsearchRestTemplate
, I see that RequestFactory
is used to create MultiGetRequest
objects.
@Override
public <T> List<T> multiGet(Query query, Class<T> clazz, IndexCoordinates index) {
Assert.notNull(index, "index must not be null");
Assert.notEmpty(query.getIds(), "No Id define for Query");
MultiGetRequest request = requestFactory.multiGetRequest(query, clazz, index);
MultiGetResponse result = execute(client -> client.mget(request, RequestOptions.DEFAULT));
DocumentCallback<T> callback = new ReadDocumentCallback<>(elasticsearchConverter, clazz, index);
return DocumentAdapters.from(result).stream().map(callback::doWith).collect(Collectors.toList());
}
In RequestFactory, I saw the use of the multiGetRequestBuilder method to create and initialize the MultiGetRequest object.
public MultiGetRequest multiGetRequest(Query query, Class<?> clazz, IndexCoordinates index) {
MultiGetRequest multiGetRequest = new MultiGetRequest();
getMultiRequestItems(query, clazz, index).forEach(multiGetRequest::add);
return multiGetRequest;
}
But after the getMultiRequestItems method is executed, the sourceFilter property set in the Query object is not set in the MultiGetRequest object, and then multiGetRequest returns the MultiGetRequest object.
private List<MultiGetRequest.Item> getMultiRequestItems(Query searchQuery, Class<?> clazz, IndexCoordinates index) {
elasticsearchConverter.updateQuery(searchQuery, clazz);
List<MultiGetRequest.Item> items = new ArrayList<>();
if (!isEmpty(searchQuery.getFields())) {
searchQuery.addSourceFilter(new FetchSourceFilter(toArray(searchQuery.getFields()), null));
}
if (!isEmpty(searchQuery.getIds())) {
String indexName = index.getIndexName();
for (String id : searchQuery.getIds()) {
MultiGetRequest.Item item = new MultiGetRequest.Item(indexName, id);
if (searchQuery.getRoute() != null) {
item = item.routing(searchQuery.getRoute());
}
items.add(item);
}
}
return items;
}
The following is my code. In this code, the withFields method is invalid. After execution, the result I get is all fields.
private <E> List<E> searchOtherDocFromElasticSearchByIds(Class<E> clazz, Collection<String> ids, String... searchFields) {
List<E> result = new ArrayList<>();
List<String> stringIds = StreamUtils.createStreamFromIterator(ids.iterator()).map(id -> Objects.toString(id, null))
.collect(Collectors.toList());
if (stringIds.isEmpty()){
return result;
}
NativeSearchQuery query = new NativeSearchQueryBuilder().withIds(stringIds).withFields(searchFields).build();
return elasticsearchRestTemplate.multiGet(query, clazz);
}
Please help me to solve this problem, thanks