使用脚本访问Elasticsearch中的嵌套对象

时间:2019-07-02 08:08:12

标签: python elasticsearch kibana elasticsearch-painless

我正在尝试使用ElasticSearch 6结果中的数据来设置我的结果得分。

部分映射如下:

{
    "properties": {
        "annotation_date": {
            "type": "date"
        },
        "annotation_date_time": {
            "type": "date"
        },
        "annotations": {
            "properties": {
                "details": {
                    "type": "nested",
                    "properties": {
                        "filter": {
                            "type": "text",
                            "fielddata": True,
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "bucket":  {
                            "type": "text",
                            "fielddata": True,  
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "keyword":  {
                            "type": "text",
                            "fielddata": True,
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "frequency": { 
                            "type": "long",
                            }
                        }
                    }
                }
            }
        }
    }

文档JSON的示例部分:

"annotations": {
      "details": [
        {
          "filter": "filter_A",
          "bucket": "bucket_A",
          "keyword": "keyword_A",
          "frequency": 6
        },
        {
          "filter": "filter_B",
          "bucket": "bucket_B",
          "keyword": "keyword_B",
          "frequency": 7
        }
      ]

我想使用注释的频率。详细信息是否碰到了某个“桶”,我尝试使用以下方法:

GET my_index/_search
{
  "size": 10000,
  "query": {
    "function_score": {
      "query": {
            "match": { "title": "<search term>" }
      },
      "script_score": {
        "script": {
          "lang": "painless",
          "source": """ 

          int score = 0;
          for (int i = 0; i < doc['annotations.details.filter'].length; i++){
            if (doc['annotations.details.filter'][i].keyword == "bucket_A"){
              score += doc['annotations.details.frequency'][i].value;
            }
          }

          return score;


          """
        }
      }
    }
  }
}

最终,这意味着在这种特定情况下,预期得分为6。如果将它击中更多的存储桶,则得分会随着其击中的频率递增。

1 个答案:

答案 0 :(得分:0)

您应该使用bool,必须与range和gt一起使用

示例

GET /_search
{
    "query": {
        "nested" : {
            "path" : "obj1",
            "score_mode" : "avg",
            "query" : {
                "bool" : {
                    "must" : [
                    { "match" : {"obj1.name" : "blue"} },
                    { "range" : {"obj1.count" : {"gt" : 5}} }
                    ]
                }
            }
        }
    }
}