Skip to content
New issue

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

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

Already on GitHub? # to your account

Totalhits support for ReactiveSearchOperations #2015

Closed
aleksanderlech opened this issue Nov 25, 2021 · 2 comments · Fixed by #2017
Closed

Totalhits support for ReactiveSearchOperations #2015

aleksanderlech opened this issue Nov 25, 2021 · 2 comments · Fixed by #2017
Assignees
Labels
status: worked on a contributor is working on this issue

Comments

@aleksanderlech
Copy link

Hello,

I need to have total count while searching and would like to avoid making two searches in Elasticsearch.
For non-reactive version of SearchOperations I could use the following method:

<T> SearchHits<T> search(Query query, Class<T> clazz);

But I miss the corresponding method in ReactiveSearchOperations. It would be very convenient to have something similar in ReactiveSearchOperations like:

<T> ReactiveSearchHits<T> search(Query query, Class<T> clazz);

where the ReactiveSearchHits is a reactive version of SearchHits returning Flux instead of List.

Is this possible to deliver such an API or are there any alternative ways for doing that in the reactive world?

I am using Spring Boot 2.5.6 with Spring Data ElasticSearch 4.0.4. Thank you in advance for your help!

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 25, 2021
@sothawo sothawo added status: pending-design-work Needs design work before any code can be developed and removed status: waiting-for-triage An issue we've not yet triaged labels Nov 26, 2021
@sothawo sothawo self-assigned this Nov 28, 2021
@sothawo sothawo added status: worked on a contributor is working on this issue and removed status: pending-design-work Needs design work before any code can be developed labels Nov 28, 2021
@sothawo
Copy link
Collaborator

sothawo commented Nov 29, 2021

I thought I had left a comment on this issue last Friday, sorry if I didn't do.

We need a new name for the search method, because we cannot use the same name/arguments with a different return type.

What I have in a branch is:

public interface ReactiveSearchHits<T> {

	Mono<AggregationsContainer<?>> getAggregations();

	float getMaxScore();

	Flux<SearchHit<T>> getSearchHits();

	long getTotalHits();

	TotalHitsRelation getTotalHitsRelation();

	boolean hasAggregations();

	boolean hasSearchHits();

	@Nullable
	Suggest getSuggest();

	boolean hasSuggest();
}

and the additional methods in ReactiveSearchOperations like:

<T> Mono<ReactiveSearchHits<T>> searchForHits(Query query, Class<T> entityType)

Usage example:

public Flux<SearchHit<Person>> test() {
	
       Query query = new CriteriaQuery(new Criteria("lastName").is("Moeller")).setPageable(PageRequest.of(0, 5));

	return operations.searchForHits(query, Person.class)
		.flatMapMany(reactiveSearchHits -> {
			LOGGER.info("total number of hits: {}", reactiveSearchHits.getTotalHits());
			return reactiveSearchHits.getSearchHits();
		});
}

I used a pageable here to see the difference in the total number of hits and the count of the flux.

Would that be what you had in mind?

@aleksanderlech
Copy link
Author

Hello @sothawo,

First of all many thanks for the super fast reaction! I am impressed. And yes, that would be something that would definitely work for me.

sothawo added a commit that referenced this issue Nov 30, 2021
@sothawo sothawo added this to the 4.4 M1 (2022.0.0) milestone Nov 30, 2021
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
status: worked on a contributor is working on this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants