在Elasticsearch 7.17.9中创建具有多个排序字段的索引。

huangapple go评论56阅读模式
英文:

Creating an index with multiple sort fields in Elasticsearch 7.17.9

问题

I'm currently learning and still a newbie to Elasticsearch. I'm trying to create an index with multiple sort fields (id, created_at) using the search_after parameter in the Python Elasticsearch Client. Here's the query I've already tried, but I encountered an error:

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

请帮我解决这个问题。谢谢!

英文:

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!

答案1

得分: 1

你需要这样做:

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

英文:

You need to do it like this:

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

答案2

得分: 1

你有一个语法错误,elastic 不知道组合排序顺序。尝试以下内容,

{
    "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"
            }
        }
    }
}
英文:

You have a syntax error, elastic don't know the combined sort order. Try following,

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

huangapple
  • 本文由 发表于 2023年4月13日 21:44:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76006184.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定