在Elasticsearch中使用多个排序字段创建索引7.17.9

pgpifvop  于 2023-04-20  发布在  ElasticSearch
关注(0)|答案(2)|浏览(177)

目前我正在学习,仍然是Elasticsearch的新手。
我尝试创建一个包含多个排序字段(id,created_at)的索引。我的计划是尝试在Python Elasticsearch Client的search函数中使用search_after参数。
下面是我已经尝试过的查询:

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"
      }
    }
  }
}

但是,我得到了这样的错误:

{
  "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
}

请帮助我。提前感谢!

xxslljrj

xxslljrj1#

你需要这样做:

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

56lgkhnf2#

你有一个语法错误,弹性不知道组合排序顺序。请尝试以下操作,

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

相关问题