Creating an index with multiple sort fields in Elasticsearch 7.17.9

Advertisements

Currently I’m learning and still a newbie to Elasticsearch.

I’m trying to create an index with multiple sort fields (id, created_at). My plan is to try using the search_after parameter in the search function from Python Elasticsearch Client.

Here is the query I’ve already tried:

PUT /my-index
{
  "settings": {
    "index":[
      {
        "sort.field": "created_at", 
        "sort.order": "asc"
      },
      {
        "sort.field": "id", 
        "sort.order": "asc"
      }
    ]
  },
  "mappings": {
    "properties": {
      "id": {
        "type": "keyword",
        "doc_values": true
      },
      "name": {
        "type": "keyword"
      },
      "created_at": {
        "type": "date"
      }
    }
  }
}

But, I got an error like this:

{
  "error" : {
    "root_cause" : [
      {
        "type" : "settings_exception",
        "reason" : "Failed to load settings from [{\"index\":[{\"sort.field\":\"created_at\",\"sort.order\":\"asc\"},{\"sort.field\":\"id\",\"sort.order\":\"asc\"}]}]"
      }
    ],
    "type" : "settings_exception",
    "reason" : "Failed to load settings from [{\"index\":[{\"sort.field\":\"created_at\",\"sort.order\":\"asc\"},{\"sort.field\":\"id\",\"sort.order\":\"asc\"}]}]",
    "caused_by" : {
      "type" : "illegal_state_exception",
      "reason" : "only value lists are allowed in serialized settings"
    }
  },
  "status" : 500
}

Please help me. Thanks in advance!

>Solution :

You need to do it like this:

PUT /my-index
{
  "settings": {
    "index": {
      "sort.field": [ "created_at", "id" ], 
      "sort.order": [ "asc", "asc" ]       
    }
  },
  ...

Leave a ReplyCancel reply