spring-projects / spring-data-elasticsearch

Provide support to increase developer productivity in Java when using Elasticsearch. Uses familiar Spring concepts such as a template classes for core API usage and lightweight repository style data access.
https://spring.io/projects/spring-data-elasticsearch/
Apache License 2.0
2.9k stars 1.33k forks source link

Cannot use `search_after` with field collapse #2935

Closed jorge-marques closed 2 months ago

jorge-marques commented 2 months ago

The elasticsearch documentation indicates that it is possible to use search_after with field collapse, as long the results are sorted exclusively by the collapsing field.

Is this possible with spring-data-elasticsearch? A cursory look at the code suggests it's not possible as _shard_doc is always added to the sort criteria as a tiebreaker

sothawo commented 2 months ago

I assume you are talking about calls to ReactiveSearchOperations.search(Query,...) as this is the only place where in the implementation _searchafter is used?

So you are setting a FieldCollapse on a NativeQuery and want to add a Sort for that same field and that fails then?

The solution would probably be to not add the _sharddoc when there is a FieldCollapse set with a field name being the same as the one in the only Sort passed in.

jorge-marques commented 2 months ago

That's exactly right, elasticsearch returns the following error: Cannot use [collapse] in conjunction with [search_after] unless the search is sorted on the same field. Multiple sort fields are not allowed

I believe your proposed solution would work well.

sothawo commented 2 months ago

Implemented on main branch and backported to current 5.3.x.