如何在elasticsearch中按数字数组对项目进行排序?

wbrvyc0a  于 2023-05-28  发布在  ElasticSearch
关注(0)|答案(2)|浏览(291)

我正在使用Elasticsearch 7.11版本,我想通过数字数组对项目进行排序:lucky_numbers.数字列表
A有很多像下面这样的元素,数组有一个动态的长度,例如:

....
 _source {
     "game.name": "Twisted Game",
     "lucky_numbers": [
     {
      "number_list": "9"
     },
     {
      "number_list": "11"
     },
     {
      "number_list": "102"
     }
     ]
 },
 ....
  _source {
     "game.name": "Number Roulette",
     "lucky_numbers": [
     {
      "number_list": "10"
     },
     {
      "number_list": "11"
     },
     {
      "number_list": "99"
     },
     {
      "number_list": "102"
     }
     ]
 },
 ....
  _source {
     "game.name": "Numeric Odyssey",
     "lucky_numbers": [
     {
      "number_list": "9"
     },
     {
      "number_list": "12"
     },
     {
      "number_list": "100"
     }
     ]
 }

如果我使用一个排序脚本,并将这些排序后的数字连接成一个字符串,我得到的顺序是:
“10,11,99,102”“9,11,102”,“9,12,100”
但我要正确的数字顺序:“9,11,102”,“9,12,100”“10,11,99,102”
我该怎么做?我知道那种人有。option mode:min用于数组,但我想考虑数组中的所有数字,而不仅仅是最小值。也许我可以写我自己的比较器?

rm5edbpk

rm5edbpk1#

在您的脚本中,当将数字连接在一起时,您需要在左侧填充零,以便所有数字都有三个位置。这样你就能一直按正确的顺序排列它们。

009,011,102
009,012,100
010,011,099,102
ccgok5k5

ccgok5k52#

Elasticsearch script sorting可以提供帮助。这里有一个例子。

POST test_lucky/_bulk
{ "index": {}}
{ "game.name": "Twisted Game", "lucky_numbers": [{ "number_list": "9" }, { "number_list": "11" }, { "number_list": "102" }] }
{ "index": {}}
{ "game.name": "Number Roulette", "lucky_numbers": [{ "number_list": "10" }, { "number_list": "11" }, { "number_list": "99" }, { "number_list": "102" }] }
{ "index": {}}
{ "game.name": "Numeric Odyssey", "lucky_numbers": [{ "number_list": "9" }, { "number_list": "12" }, { "number_list": "100" }] }

GET test_lucky/_search
{
  "query": {
    "match_all": {}
  },
      "sort" : {
        "_script" : {
            "script" : "String s = ''; for(int i = 0; i < params._source.lucky_numbers.length; ++i) {s += params._source.lucky_numbers[i] + ','}  s",
            "type" : "string",
            "order" : "asc"
        }
    }
}

结果:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": null,
    "hits": [
      {
        "_index": "test_lucky",
        "_id": "Kv1xQogBVSbrfAXEoHUk",
        "_score": null,
        "_source": {
          "game.name": "Number Roulette",
          "lucky_numbers": [
            {
              "number_list": "10"
            },
            {
              "number_list": "11"
            },
            {
              "number_list": "99"
            },
            {
              "number_list": "102"
            }
          ]
        },
        "sort": [
          "{number_list=10},{number_list=11},{number_list=99},{number_list=102},"
        ]
      },
      {
        "_index": "test_lucky",
        "_id": "Kf1xQogBVSbrfAXEoHUk",
        "_score": null,
        "_source": {
          "game.name": "Twisted Game",
          "lucky_numbers": [
            {
              "number_list": "9"
            },
            {
              "number_list": "11"
            },
            {
              "number_list": "102"
            }
          ]
        },
        "sort": [
          "{number_list=9},{number_list=11},{number_list=102},"
        ]
      },
      {
        "_index": "test_lucky",
        "_id": "K_1xQogBVSbrfAXEoHUk",
        "_score": null,
        "_source": {
          "game.name": "Numeric Odyssey",
          "lucky_numbers": [
            {
              "number_list": "9"
            },
            {
              "number_list": "12"
            },
            {
              "number_list": "100"
            }
          ]
        },
        "sort": [
          "{number_list=9},{number_list=12},{number_list=100},"
        ]
      }
    ]
  }
}

相关问题