go-elasticsearch icon indicating copy to clipboard operation
go-elasticsearch copied to clipboard

Using typed API from 8.4.0-alpha.1 release, facing issue in generating search query having sort

Open cmadarsh opened this issue 2 years ago • 2 comments

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

cmadarsh avatar Aug 11 '22 10:08 cmadarsh

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.

Anaethelion avatar Aug 11 '22 15:08 Anaethelion

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 avatar Aug 16 '22 09:08 cmadarsh

@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.

Anaethelion avatar Aug 18 '22 09:08 Anaethelion

Yes, thanks, its working now...

cmadarsh avatar Aug 23 '22 07:08 cmadarsh

@cmadarsh A quick ping to let you know v8.4.0 has been released!

Closing this.

Anaethelion avatar Aug 24 '22 16:08 Anaethelion