go-elasticsearch
go-elasticsearch copied to clipboard
Using typed API from 8.4.0-alpha.1 release, facing issue in generating search query having sort
Hello,
As part of below search query, I was trying to add 'sort' attribute using typed API.
Query
POST myIdx/_search { "query": { "bool": { "must": [ { "match_phrase": { "FieldName": { "query": "value" } } } ] } }, "sort": [ { "@timestamp": { "format": "strict_date_optional_time_nanos", "order": "asc", "numeric_type": "date_nanos" } } ] }
Here is the code snippet for adding 'sort'.
qry := search.NewRequestBuilder()
var so sortorder.SortOrder
so.UnmarshalText([]byte("asc"))
sortCombBuild := types.NewSortCombinationsBuilder().Field("@timestamp").SortOptions(types.NewSortOptionsBuilder().
SortOptions(map[types.Field]*types.FieldSortBuilder{
"@timestamp": types.NewFieldSortBuilder().
Format("strict_date_optional_time_nanos").Order(so)}))
sortComb := sortCombBuild.Build()
var sortCombArray []types.SortCombinations
sortCombArray = append(sortCombArray, sortComb)
qry.Sort(types.NewSortBuilder().Sort(sortCombArray))
But this is generating 'sort' query as below which adds a field 'SortOptions' and when query is run, it is not able to recognise the field "@timestamp" which is present in my document and I get an error as below.
"sort": [ { "SortOptions": { "@timestamp": { "format": "strict_date_optional_time_nanos", "order": "asc" } } } ]
Error:
{ "error" : { "root_cause" : [ { "type" : "x_content_parse_exception", "reason" : "[18:9] [field_sort] unknown field [@timestamp]" } ], "type" : "x_content_parse_exception", "reason" : "[18:9] [field_sort] unknown field [@timestamp]" }, "status" : 400 }
With typed API, as 'sort' is a slice of SortCombinations, I am not able to create correct sort from the corresponding typed API code.
Could you please advice the code implementation for sort using typed API, or let me know if there is an issue with the library or the way I have implemented?
Thanks and Regards, Adarsh M
Hi @cmadarsh
Thank you for reporting this! This is indeed a bug and should come with a proper serialization override to prevent such thing from happening.
I'll have a look.
Could you please let me know if I can expect the fix in a months time, along with production release of 8.4.0-alpha.1?
@cmadarsh I've just pushed a new prerelease with a fix related to the serialization which impact sort. Please test it and let me know if that works for you!
As for the production release, I cannot make any promise since we release the whole stack at once. It should however happen soon.
Yes, thanks, its working now...
@cmadarsh A quick ping to let you know v8.4.0 has been released!
Closing this.