弹性:查询嵌套文档的已过滤子集的总和

时间:2019-05-30 11:29:56

标签: elasticsearch

在elasticsearch索引中考虑这样的文档(帖子):

    {
      title: "I love ice cream!"
      comments: [
        {
          body: "me too!",
          reaction: 'positive',
          likes: 20
        },
        {
          body: "huh!",
          reaction: 'sarcastic',
          likes: 5
        }
      ]
    }

注释是nested类型的字段。

弹性如何回答: 给我所有帖子,所有对“讽刺”评论的赞总和大于100。 我乐于接受任何其他可以帮助回答此类查询的数据建模方法。

1 个答案:

答案 0 :(得分:2)

可以使用bucket selector aggregation解决此问题。

映射:

{
  "index1" : {
    "mappings" : {
      "properties" : {
        "comments" : {
          "type" : "nested",
          "properties" : {
            "body" : {
              "type" : "text"
            },
            "likes" : {
              "type" : "integer"
            },
            "reaction" : {
              "type" : "text"
            }
          }
        },
        "title" : {
          "type" : "keyword"
        }
      }
    }
  }
}

数据:

  "hits" : [
      {
        "_index" : "index1",
        "_type" : "_doc",
        "_id" : "p0y9DGsBfPdKzuAGdQrm",
        "_score" : 1.0,
        "_source" : {
          "title" : "I love ice cream!",
          "comments" : [
            {
              "body" : "me too!",
              "reaction" : "positive",
              "likes" : 20
            },
            {
              "body" : "huh!",
              "reaction" : "sarcastic",
              "likes" : 5
            }
          ]
        }
      },
      {
        "_index" : "index1",
        "_type" : "_doc",
        "_id" : "qEy9DGsBfPdKzuAGnwox",
        "_score" : 1.0,
        "_source" : {
          "title" : "I hate ice cream!",
          "comments" : [
            {
              "body" : "me too!",
              "reaction" : "positive",
              "likes" : 10
            },
            {
              "body" : "huh!",
              "reaction" : "sarcastic",
              "likes" : 5
            }
          ]
        }
      }
    ]
  }

查询:

GET index1/_search
{
  "size": 0,
  "aggs": {
    "title": {
      "terms": {
        "field": "title"
      },
      "aggs": {
        "comments": {
          "nested": {
            "path": "comments"
          },
          "aggs": {
            "reaction": {
              "filter": {
                "term": {
                  "comments.reaction": "positive"
                }
              },
              "aggs": {
                "total_likes": {
                  "sum": {
                    "field": "comments.likes"
                  }
                }
              }
            }
          }
        },
        "total_likes_filter": {
          "bucket_selector": {
            "buckets_path": {
              "likes": "comments>reaction>total_likes"
            },
            "script": "params.likes > 15"
          }
        }
      }
    }
  }
}

结果:

  "aggregations" : {
    "title" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "I love ice cream!",
          "doc_count" : 1,
          "comments" : {
            "doc_count" : 2,
            "reaction" : {
              "doc_count" : 1,
              "total_likes" : {
                "value" : 20.0
              }
            }
          }
        }
      ]
    }
  }
}

存储桶仅包含“我爱冰淇淋!”正面反应的总喜欢次数大于20。 我讨厌冰淇淋!阳性反应的总和为5,因此不包括在内。

相关问题