Skip to content
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

Nested sort seems to be missing #2376

Closed
sweiss435 opened this issue Nov 23, 2022 · 6 comments
Closed

Nested sort seems to be missing #2376

sweiss435 opened this issue Nov 23, 2022 · 6 comments
Labels
status: waiting-for-feedback We need additional information before we can continue

Comments

@sweiss435
Copy link

before we used to be able to do

GeoDistanceSortBuilder geoDistanceSortBuilder = SortBuilders .geoDistanceSort(locationFieldName, poi.getFirst(), poi.getSecond()) .unit(DistanceUnit.MILES) .order(SortOrder.ASC); queryBuilder.withSort(geoDistanceSortBuilder);
now all I can do is
queryBuilder.withSort(Sort.by(Sort.Order.asc(locationField)));
just pass in a field, but need to do a nested query to generate a geo_distance field from which to sort on

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 23, 2022
@sothawo
Copy link
Collaborator

sothawo commented Nov 24, 2022

For implementing nested sort there is a ticket #1783

Which version of Spring Data Elasticsearch are you referring to?

GeoDistanceSortBuilder was coming from Elasticsearch 7 libraries, what was the type of the queryBuilder you use in your code? Spring Data Elasticsearch has the org.springframework.data.elasticsearch.core.query.GeoDistanceOrder since version 4.0.

@sothawo sothawo added the status: waiting-for-feedback We need additional information before we can continue label Nov 24, 2022
@sweiss435
Copy link
Author

sweiss435 commented Nov 25, 2022

Yes, this api was on elastic 7. I've got it running on elastic 8.4 now with spring data 4.4.5. After upgrading to elastic 8.x, we switched over to org.springframework.data.elasticsearch.client.elc.NativeQueryBuilder. Just about everything works, but this nexted sort. That's a blocker since this search needs to sort by distance.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Nov 25, 2022
@sweiss435
Copy link
Author

I can't imagine we are the only ones that need to do a lat/lon nested query to sort by distance. #1783 looks pretty old and stale, how are we suppose to use elastic 8.x if it lack this?

@sothawo
Copy link
Collaborator

sothawo commented Nov 25, 2022

When you use NativeQueryBuilder you can do:

  1. Use the Elasticsearch classes:
var sortOptions = new SortOptions.Builder()
	.geoDistance(gd -> gd
		.field("locationFieldName")
		.location(loc -> loc
			.latlon(latlon -> latlon
				.lat(12.34)
				.lon(23.45)
			)
		)
		.unit(DistanceUnit.Miles)
	)
	.build();

var query = NativeQuery.builder().withSort(sortOptions).build();

or just:

var query = NativeQuery.builder()
	.withSort(sob -> sob
		.geoDistance(gd -> gd
			.field("locationFieldName")
			.location(loc -> loc
				.latlon(latlon -> latlon
					.lat(12.34)
					.lon(23.45)
				)
			)
			.unit(DistanceUnit.Miles)
		)
	)
	.build();
  1. Use the Spring Data Elasticsearch class for geo distance order:
var order = new GeoDistanceOrder("locationFieldname", new GeoPoint(12.34, 23.45)).withUnit("miles");
var query = NativeQuery.builder().withSort(Sort.by(order)).build();

@sothawo sothawo added status: waiting-for-feedback We need additional information before we can continue and removed status: feedback-provided Feedback has been provided labels Nov 25, 2022
@sweiss435
Copy link
Author

Thanks! That seems to get me a lot closer.
It does still fail due to "query_shard_exception","reason":"it is mandatory to set the [nested] context on the nested sort field: [associatedLocations.location].

From what I can see in the elastic documentation, there's suppose to be a 'path' in the nested query. At least I think that is the issue, any idea how I set that?

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Nov 25, 2022
@sothawo
Copy link
Collaborator

sothawo commented Nov 25, 2022

The nested sort only seems available in the co.elastic.clients.elasticsearch._types.FieldSort and it's builder, the API documentation has this for field sorts as well (https://www.elastic.co/guide/en/elasticsearch/reference/8.5/sort-search-results.html#nested-sorting).

The co.elastic.clients.elasticsearch._types.GeoDistanceSort does not have this, but I cannot find it in the API docs either (https://www.elastic.co/guide/en/elasticsearch/reference/8.5/sort-search-results.html#geo-sorting).

But that's an Elasticsearch topic, not Spring Data Elasticsearch then. You might want to open an issue in Elastic's repository for the client at https://github.com/elastic/elasticsearch-java/issues

@sothawo sothawo added status: waiting-for-feedback We need additional information before we can continue and removed status: waiting-for-triage An issue we've not yet triaged status: feedback-provided Feedback has been provided labels Nov 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-feedback We need additional information before we can continue
Projects
None yet
Development

No branches or pull requests

3 participants