Skip to content

[Feature Request] New lexicographical sort mode for multi-valued keyword fields #18244

Open
@igordemiranda

Description

@igordemiranda

Is your feature request related to a problem? Please describe

Consider the following 4 documents with a keyword field "names":

  • _doc/0: { names: [A, B] }
  • _doc/1: { names: [A, E] }
  • _doc/2: { names: [A, D] }
  • _doc/4: { names: [A, C] }

The current supported behavior would have these 4 docs be considered as ties when sorted ascendingly by names.

I would like to be able to sort these so that the lists are sorted lexicographically as a whole; i.e. when the first element is a tie, it compares the second element, and so on, like so:

  • _doc/0: { names: [A, B] }
  • _doc/4: { names: [A, C] }
  • _doc/2: { names: [A, D] }
  • _doc/1: { names: [A, E] }

More examples:

  • [ [A], [B], [A, B] ] would sort as [ [A], [A, B], [B] ]
  • [ [A], [A, B, C], [A, B, B] ] would sort as [ [A], [A, B, B], [A, B, C] ]

Describe the solution you'd like

This could be solved by OpenSearch by introducing a new sort mode (e.g lex, as a placeholder name for now) for multi-value fields.

POST /_search
{
   "query" : {
      "match_all" : {}
   },
   "sort" : [
      {"names" : {"order" : "asc", "mode" : "lex"}}
   ]
}

Related component

Search

Describe alternatives you've considered

This is how I ended up solving this for my use-case in the absence of built-in support in OpenSearch.

I created a new field names_sortKey of type keyword, and in my application I joined the list elements with a delimiter character that sorts before all printable characters (e.g. \u001F) and then I perform the sort on this field.

Example:

{
  "names": ["A", "B", "C"],
  "names_sortKey": "A\u001fB\u001fC"
}
POST /_search
{
   "query" : {
      "match_all" : {}
   },
   "sort" : [
      {"names_sortKey" : {"order" : "asc"}}
   ]
}

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    SearchSearch query, autocomplete ...etcenhancementEnhancement or improvement to existing feature or requestuntriaged

    Type

    No type

    Projects

    Status

    🆕 New

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions