0

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

  1. p1-liked but p2-disliked
  2. p1,p2 both liked
  3. p2-disliked but p1-liked
  4. p1,b2 both disliked

Thanks for your help

2
  • Is your query going to be for a specific P1 and P2, or are you trying to count all combinations where one person liked and another disliked? Commented Nov 21, 2018 at 14:37
  • I will be specifying two person ids in the query that i have to compare, and comparision is for all combinations. Commented Nov 21, 2018 at 14:38

1 Answer 1

1

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:

  • P1 liked but P2 disliked
  • P1 and P2 both liked


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


Sign up to request clarification or add additional context in comments.

1 Comment

Please note: I have added only two filters, you can add more for any number of combinations

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.