elasticsearch painless 排序脚本中使用 Map (HashMap)

参考资料https://www.elastic.co/guide/en/elasticsearch/painless/master/painless-casting.html

https://blog.csdn.net/weixin_38809962/article/details/79768002

https://blog.csdn.net/hechaojie_com/article/details/88881503

有一个业务需求,在给 es 中传入一个 id 集合,然后根据条件去过滤掉一些记录。然后分页返回 10 条记录,但是有个条件,排序要按照传入的 id 的顺序进行排序,传入的时候,哪个 id 在前,返回的时候这个 id 也得在前面。只能根据脚本写排序。

GET xxxxx/_doc/_search?size=1000
{
  "query": {
    "bool": {
      "filter": [
        {
          "terms": {
            "exerciseId": [
              268704949713821702,
              268704949713821698,
              268704949713821705,
              330379959905607682,
              330379959905607681,
              330379959905607680,
              330379959905607687,
              330379959905607690,
              330354511029002255,
              330354511029002245,
              330354511029002247,
              330354511029002240,
              330354511029002243,
              330354511029002253,
              268704949713821707,
              395591013664874499,
              329589022711406596,
              329589022711406597,
              395591013664874500,
              268704949713821709,
              395943043096961027,
              268704949713821706,
              268704949713821704,
              330379959905607693,
              329589022711406598,
              329589022711406613,
              395943043096961025,
              395943043096961024,
              395591013664874498,
              329589022711406612,
              329589022711406601,
              268704949713821696,
              268704949713821700,
              268704949713821697,
              329589022711406611,
              329589022711406594,
              395591013664874497,
              329589022711406600,
              330379959905607683,
              330379959905607685,
              330379959905607684,
              330379959905607691,
              330379959905607689,
              330379959905607688,
              330379959905607692,
              330354511029002248,
              330354511029002249,
              330354511029002250,
              330354511029002251,
              330354511029002241,
              330354511029002242,
              329589022711406595,
              395943043096961026,
              330379959905607686,
              330354511029002246,
              395591013664874496,
              329589022711406602,
              268704949713821701,
              268704949713821708,
              329589022711406610,
              329589022711406599,
              330354511029002254,
              329589022711406609,
              329589022711406614
            ],
            "boost": 1.0
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1.0
    }
  },
  "sort": [
    {
      "_script": {
        "script": {
          "source": "def exerciseId = doc['exerciseId'].value;def str = String.valueOf(exerciseId);params.exerciseIdToScoreMap.get(str);",
          "lang": "painless",
          "params": {
            "exerciseIdToScoreMap": {
              "329589022711406599": 4,
              "329589022711406598": 40,
              "329589022711406597": 47,
              "329589022711406596": 48,
              "329589022711406595": 13,
              "329589022711406594": 29,
              "395591013664874497": 28,
              "330354511029002250": 17,
              "395591013664874496": 9,
              "395591013664874499": 49,
              "395591013664874498": 36,
              "330354511029002242": 14,
              "330354511029002243": 52,
              "330354511029002240": 53,
              "330354511029002241": 15,
              "330354511029002246": 10,
              "330354511029002247": 54,
              "330354511029002245": 55,
              "330354511029002248": 19,
              "330354511029002249": 18,
              "268704949713821697": 31,
              "268704949713821696": 33,
              "268704949713821698": 63,
              "330354511029002253": 51,
              "330354511029002254": 3,
              "330354511029002251": 16,
              "330354511029002255": 56,
              "329589022711406614": 1,
              "329589022711406613": 39,
              "329589022711406612": 35,
              "329589022711406611": 30,
              "329589022711406610": 5,
              "268704949713821701": 7,
              "268704949713821700": 32,
              "268704949713821702": 64,
              "268704949713821705": 62,
              "268704949713821704": 42,
              "268704949713821707": 50,
              "268704949713821706": 43,
              "268704949713821709": 45,
              "268704949713821708": 6,
              "330379959905607687": 58,
              "395943043096961027": 44,
              "395591013664874500": 46,
              "330379959905607688": 21,
              "330379959905607685": 25,
              "330379959905607686": 11,
              "330379959905607683": 26,
              "330379959905607684": 24,
              "330379959905607681": 60,
              "330379959905607682": 61,
              "329589022711406609": 2,
              "330379959905607680": 59,
              "395943043096961024": 37,
              "395943043096961025": 38,
              "329589022711406602": 8,
              "329589022711406601": 34,
              "395943043096961026": 12,
              "329589022711406600": 27,
              "330379959905607689": 22,
              "330379959905607692": 20,
              "330379959905607693": 41,
              "330379959905607690": 57,
              "330379959905607691": 23
            }
          }
        },
        "type": "number",
        "order": "desc"
      }
    }
  ]
}

map 在使用的时候有两种方式:

  1. 一种是 map.key 这样
  2. 另一种是 map.get(key) 这种形式。

如果 key 是脚本中定义的变量,则只能 map.get(key) 这种形式,今天尝试了好久终于调好了,特此记录一下,以飨后来人。

  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值