question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[discuss] sort format in search API

See original GitHub issue

version elasticsearch-js@canary.35 problem: search method support sort parameter that can be configured either in body or in query. The problem is the sort passed in body and query has different syntax. sort: sortField:sortOrder syntax is valid in the query but cannot be used when declared in body.sort. Unfortunately, it’s hard to realize this syntax is invalid because the Elasticsearch error response will be a cryptic all shard failed message.

There are several options to fix the problem

  • make the client smart enough to detect sort: sortField:sortOrder syntax and put sort parameter in query. Not sure it’s a good option in the long term as it requires the elasticsearch-js client to cover all the possible API deviations in the future.
  • reduce API surface to support sort: SearchSortContainer only. It doesn’t allow sort: string shorthand usage anymore but removes ambiguity in the accepted params.

cc @pgayvallet

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
tomtasticocommented, Feb 27, 2022

I see currently in v8.0.0, the type for sort in SearchRequest is string | string[].

https://github.com/elastic/elasticsearch-js/blob/main/src/api/types.ts#L969

So I can’t really use the format shown in the docs https://www.elastic.co/guide/en/elasticsearch/reference/current/sort-search-results.html i.e.

"sort" : [
    { "post_date" : {"order" : "asc", "format": "strict_date_optional_time_nanos"}},
    "user",
    { "name" : "desc" },
    { "age" : "desc" },
    "_score"
  ],

Gives a type error. I have to cast it to any to be able to use it.

0reactions
And2scommented, May 11, 2022

I see currently in v8.0.0, the type for sort in SearchRequest is string | string[].

https://github.com/elastic/elasticsearch-js/blob/main/src/api/types.ts#L969

So I can’t really use the format shown in the docs https://www.elastic.co/guide/en/elasticsearch/reference/current/sort-search-results.html i.e.

"sort" : [
    { "post_date" : {"order" : "asc", "format": "strict_date_optional_time_nanos"}},
    "user",
    { "name" : "desc" },
    { "age" : "desc" },
    "_score"
  ],

Gives a type error. I have to cast it to any to be able to use it.

Same problem here. It’s typedef should be sort: Sort | string | string[];.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Search API sort | Elastic App Search Documentation [8.5]
A special sorting field name is available, _score , to order by relevance. If a sort parameter is not given, results will be...
Read more >
REST API Design: Filtering, Sorting, and Pagination - Moesif
Advanced REST API design guidelines for API filtering, sorting, and pagination. ... This is the simplest form of paging. Limit/Offset became popular with ......
Read more >
REST API: Sorting, Filtering, and Pagination - Tania Rascia
Sorting allows you to order the results by any field, in ascending or descending order.
Read more >
Filtering and sorting search results - Google Developers
Sorted results from your engine are presented based on the value those pages have in their PageMaps for that DataObject and Attribute. Pages...
Read more >
Sorting on multiple fields using Search api Sort - Drupal
I want sorting on multiple fields like my custom field in DESC order and Price in ASC order based on some condition.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found