简体   繁体   English

基于嵌套数组查询弹性搜索聚合

[英]Query for Elastic search aggregations based on nested array

I do have the documents like below in my index 我的索引中确实有以下文件

{
      "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"
        }
      ]
    }

Now I want to query the aggregations of book count for person p1,p2 like below the counts of books 现在我想查询人p1,p2的帐簿汇总,如下所示

  1. p1-liked but p2-disliked p1喜欢但p2不喜欢
  2. p1,p2 both liked p1,p2都喜欢
  3. p2-disliked but p1-liked p2不喜欢,但p1喜欢
  4. p1,b2 both disliked p1,b2都不喜欢

Thanks for your help 谢谢你的帮助

Since you require buckets with different filter for each bucket, filters aggregation is best fit for this. 由于您需要每个存储桶都具有不同过滤器的存储桶,因此最适合使用过滤器聚合 As per your comment there will be two person ids to be compared following is the query for your following two combinations: 根据您的评论,将比较两个人的ID,以下是您的以下两个组合的查询:

  • P1 liked but P2 disliked P1喜欢但P2不喜欢
  • P1 and P2 both liked 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
    }


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM