SSG
SSG

Reputation: 1505

Elasticsearch sort based on multiple fields

I have a requirement where I need to sort index data based on multiple fields.

Exa : My index data is as follows:

{ "id": 3, "displayId": 0, "createdAt": 1490207400001, "priority": "NORMAL", }

{ "id": 3, "displayId": 0, "createdAt": 1490207400002, "priority": "HIGH", }

Like this there will be multiple records with different "priority" and "createAt" field.

Step 1: First of all I need to sort the records by priority.

Step 2 : Then I want to sort the records in step 1 by the field "createdAt"

What is the way to do this in elasticsearch ?

Thanks in Advance !!

Upvotes: 2

Views: 9536

Answers (1)

Persimmonium
Persimmonium

Reputation: 15789

you do this:

GET /my_index/my_type/_search 
{
  "sort" : [
    { "priority" : {"order" : "asc"}},
    { "createAt" : "desc" }
  ],
"query" : {
    ...
}}

This assumes your priority field values are ordered ok, otherwise you have to rename them or use a function to sort.

Upvotes: 2

Related Questions