【问题标题】:Query for Elastic search aggregations based on nested array基于嵌套数组查询弹性搜索聚合
【发布时间】:2019-04-24 03:49:47
【问题描述】:

我的索引中确实有如下文档

{
      "bookName" : "coolbook",
      "timeStamp" : "2018-11-19T12:52:17.000Z",
      "referenceId" : "auth_test_01_01_000004",
      "peoplestatus" : [
        {
          "personId" : "p1",
          "status" : "like"
        },
        {
          "personId" : "p2",
          "status" : "dislike"
        },{
          "personId" : "p3",
          "status" : "netrual"
        }
      ]
    }

现在我想查询人 p1,p2 的书数聚合,如下所示 书的数量

  1. p1 喜欢但 p2 不喜欢
  2. p1,p2 都喜欢
  3. p2 不喜欢但 p1 喜欢
  4. p1,b2 都不喜欢

感谢您的帮助

【问题讨论】:

  • 您的查询是针对特定的 P1 和 P2,还是要计算一个人喜欢而另一个人不喜欢的所有组合?
  • 我将在查询中指定两个我必须比较的人员 ID,并且比较适用于所有组合。

标签: java elasticsearch indexing elasticsearch-5


【解决方案1】:

由于您需要为每个存储桶使用不同过滤器的存储桶,filters aggregation 最适合此。 根据您的评论,将有两个要比较的人员 ID 以下是您的以下两种组合的查询:

  • P1 喜欢但 P2 不喜欢
  • P1 和 P2 都喜欢


    {
      "query": {
        "match_all": {}
      },
      "aggs": {
        "books": {
          "filters": {
            "filters": {
              "P1L_P2DL": {
                "bool": {
                  "must": [
                    {
                      "nested": {
                        "path": "peoplestatus",
                        "query": {
                          "bool": {
                            "must": [
                              {
                                "bool": {
                                  "must": [
                                    {
                                      "term": {
                                        "peoplestatus.personId": "p1"
                                      }
                                    },
                                    {
                                      "term": {
                                        "peoplestatus.status": "like"
                                      }
                                    }
                                  ]
                                }
                              }
                            ]
                          }
                        }
                      }
                    },
                    {
                      "nested": {
                        "path": "peoplestatus",
                        "query": {
                          "bool": {
                            "must": [
                              {
                                "bool": {
                                  "must": [
                                    {
                                      "term": {
                                        "peoplestatus.personId": "p2"
                                      }
                                    },
                                    {
                                      "term": {
                                        "peoplestatus.status": "dislike"
                                      }
                                    }
                                  ]
                                }
                              }
                            ]
                          }
                        }
                      }
                    }
                  ]
                }
              },
              "L1N3": {
                "bool": {
                  "must": [
                    {
                      "nested": {
                        "path": "peoplestatus",
                        "query": {
                          "bool": {
                            "must": [
                              {
                                "bool": {
                                  "must": [
                                    {
                                      "term": {
                                        "peoplestatus.personId": "p1"
                                      }
                                    },
                                    {
                                      "term": {
                                        "peoplestatus.status": "like"
                                      }
                                    }
                                  ]
                                }
                              }
                            ]
                          }
                        }
                      }
                    },
                    {
                      "nested": {
                        "path": "peoplestatus",
                        "query": {
                          "bool": {
                            "must": [
                              {
                                "bool": {
                                  "must": [
                                    {
                                      "term": {
                                        "peoplestatus.personId": "p2"
                                      }
                                    },
                                    {
                                      "term": {
                                        "peoplestatus.status": "like"
                                      }
                                    }
                                  ]
                                }
                              }
                            ]
                          }
                        }
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      },
      "size": 0
    }


【讨论】:

  • 请注意:我只添加了两个过滤器,您可以为任意数量的组合添加更多
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-01
  • 2015-09-24
  • 1970-01-01
相关资源
最近更新 更多