Skip to content

Fix source filter setup in multiget requests #1659

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
LemonGo97 opened this issue Jan 20, 2021 · 2 comments · Fixed by #1664
Closed

Fix source filter setup in multiget requests #1659

LemonGo97 opened this issue Jan 20, 2021 · 2 comments · Fixed by #1664
Assignees
Labels
type: bug A general bug

Comments

@LemonGo97
Copy link

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

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 20, 2021
@sothawo sothawo added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Jan 20, 2021
@sothawo sothawo changed the title When I use ElasticsearchRestTemplate, why does multiGet give up field filtering Fix source filter setup in get and multiget requests Jan 20, 2021
@sothawo
Copy link
Collaborator

sothawo commented Jan 20, 2021

Thanks for finding this. This is indeed a bug that the values that are set in the Query are not propagated into the MultiGetRequest.Item objects. The same happens if a SourceFilter is set.

@sothawo sothawo self-assigned this Jan 24, 2021
@sothawo sothawo changed the title Fix source filter setup in get and multiget requests Fix source filter setup in multiget requests Jan 24, 2021
sothawo added a commit to sothawo/spring-data-elasticsearch that referenced this issue Jan 24, 2021
sothawo added a commit that referenced this issue Jan 24, 2021
@sothawo sothawo added this to the 4.2 M3 (2021.0.0) milestone Jan 24, 2021
sothawo added a commit that referenced this issue Jan 24, 2021
Original Pull Request #1664
Closes #1659

(cherry picked from commit 1a02c1e)
sothawo added a commit that referenced this issue Jan 24, 2021
Original Pull Request #1664
Closes #1659

(cherry picked from commit 1a02c1e)
@sothawo
Copy link
Collaborator

sothawo commented Jan 24, 2021

Implemented in master branch and backported to 4.1.x and 4.0.x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants