dou1908 2018-11-26 06:54
浏览 345

使用ElasticSearch无痛脚本在params数组中存在检查值

I am new to ElasticSearch. I am working on a project where I have to search listings which matches the input http://foo.bar/search?listing=123,456. I have built an array which key and value are the same and representing the listing ids. I have tried to run the following example but it failed near contains. The script is built for painless language. However, I always get the runtime error.

I cannot get this to work, what could I be doing wrong?

 'function_score' => [
       'query' => $query,
           'score_mode' => 'sum',
              'functions' => [[
                'script_score' => [
                 'script' => [
                   'params' => ['listing' => [123 => 123, 456 => 456]],
    'source' => "(params.listing.contains(doc['id'].value) ? Math.pow(3, 3) : 0)",
                            ],
                        ],
                    ]],
                ],
  • 写回答

1条回答 默认 最新

  • dongzhuxun5136 2018-11-26 07:11
    关注

    You can make use of the below query. You can check the script part as how what you need has been implemented.

    POST <your_index_name>/_search
    {
      "query": {
        "function_score": {
          "score_mode": "sum", 
          "query": {
            "match_all": {}
          },
          "functions": [
            {
              "script_score": {
                "script": {
                  "source": """
                      boolean allValues = false;
                      for(int i=0; i<params.value.size(); i++){
                        if(doc['id'].value.contains(params.value.get(i))){
                          allValues = true;
                        }else{
                          allValues = false;
                          break;
                        }
                      } 
                      if(allValues){
                        return Math.pow(3,3)
                      } else {
                        return 0;
                      }
                    """,
                  "params":{
                    "value": ["123","456"]
                  }
                }
              }
            }
          ]
        }
      }
    }
    

    What the query does is, it checks to see if all the values present in params i.e 123 & 456 is present in the id field, if it does, it would go ahead and calculate score accordingly.

    Not that id field is a keyword when I tested the query at my end.

    You can use the query accordingly in your php script. Let me know if this helps!

    评论

报告相同问题?

悬赏问题

  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构