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

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

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:

  1. {
  2. "error" : {
  3. "root_cause" : [
  4. {
  5. "type" : "settings_exception",
  6. "reason" : "Failed to load settings from [{\"index\":[{\"sort.field\":\"created_at\",\"sort.order\":\"asc\"},{\"sort.field\":\"id\",\"sort.order\":\"asc\"}]}]"
  7. }
  8. ],
  9. "type" : "settings_exception",
  10. "reason" : "Failed to load settings from [{\"index\":[{\"sort.field\":\"created_at\",\"sort.order\":\"asc\"},{\"sort.field\":\"id\",\"sort.order\":\"asc\"}]}]",
  11. "caused_by" : {
  12. "type" : "illegal_state_exception",
  13. "reason" : "only value lists are allowed in serialized settings"
  14. }
  15. },
  16. "status" : 500
  17. }

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

英文:

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:

  1. PUT /my-index
  2. {
  3. "settings": {
  4. "index":[
  5. {
  6. "sort.field": "created_at",
  7. "sort.order": "asc"
  8. },
  9. {
  10. "sort.field": "id",
  11. "sort.order": "asc"
  12. }
  13. ]
  14. },
  15. "mappings": {
  16. "properties": {
  17. "id": {
  18. "type": "keyword",
  19. "doc_values": true
  20. },
  21. "name": {
  22. "type": "keyword"
  23. },
  24. "created_at": {
  25. "type": "date"
  26. }
  27. }
  28. }
  29. }

But, I got an error like this:

  1. {
  2. "error" : {
  3. "root_cause" : [
  4. {
  5. "type" : "settings_exception",
  6. "reason" : "Failed to load settings from [{\"index\":[{\"sort.field\":\"created_at\",\"sort.order\":\"asc\"},{\"sort.field\":\"id\",\"sort.order\":\"asc\"}]}]"
  7. }
  8. ],
  9. "type" : "settings_exception",
  10. "reason" : "Failed to load settings from [{\"index\":[{\"sort.field\":\"created_at\",\"sort.order\":\"asc\"},{\"sort.field\":\"id\",\"sort.order\":\"asc\"}]}]",
  11. "caused_by" : {
  12. "type" : "illegal_state_exception",
  13. "reason" : "only value lists are allowed in serialized settings"
  14. }
  15. },
  16. "status" : 500
  17. }

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:

  1. PUT /my-index
  2. {
  3. "settings": {
  4. "index": {
  5. "sort.field": [ "created_at", "id" ],
  6. "sort.order": [ "asc", "asc" ]
  7. }
  8. },
  9. ...

答案2

得分: 1

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

  1. {
  2. "settings": {
  3. "index": {
  4. "sort.field": [
  5. "created_at",
  6. "id"
  7. ],
  8. "sort.order": [
  9. "asc",
  10. "asc"
  11. ]
  12. }
  13. },
  14. "mappings": {
  15. "properties": {
  16. "id": {
  17. "type": "keyword",
  18. "doc_values": true
  19. },
  20. "name": {
  21. "type": "keyword"
  22. },
  23. "created_at": {
  24. "type": "date"
  25. }
  26. }
  27. }
  28. }
英文:

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

  1. {
  2. "settings": {
  3. "index": {
  4. "sort.field": [
  5. "created_at",
  6. "id"
  7. ],
  8. "sort.order": [
  9. "asc",
  10. "asc"
  11. ]
  12. }
  13. },
  14. "mappings": {
  15. "properties": {
  16. "id": {
  17. "type": "keyword",
  18. "doc_values": true
  19. },
  20. "name": {
  21. "type": "keyword"
  22. },
  23. "created_at": {
  24. "type": "date"
  25. }
  26. }
  27. }
  28. }

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:

确定