0

I have some articles, each article may have some reviewers and each reviewer may have different approve status like following:

{
  ArticleId: 1,
  Title: "1",
  Reviewers: [
    {
       "Reviewer": "abc",
       "Status": "Pending"
    },
    {
       "Reviewer": "def",
       "Status": "Approved"
    }
  ]
}

{
  ArticleId: 2,
  Title: "2",
  Reviewers: [
    {
       "Reviewer": "abc",
       "Status": "Approved"
    },
    {
       "Reviewer": "def",
       "Status": "Approved"
    }
  ]
}

{
  ArticleId: 3,
  Title: "2",
  Reviewers: [
    {
       "Reviewer": "def",
       "Status": "Approved"
    }
  ]
}

I want to saves this data in the ES and search all articles having abc as reviewer, and aggregate by abc's review status.

{
 ...
 buckets:[{
        "doc_count": "1",
        "Key": "Pending"
    },{
        "doc_count": "1",
        "Key": "Approved"
    }]
}

I was wondering how to do it in ES?

1 Answer 1

2

Assumption: Reviewers is a nested type.

Try below query. Details as comment in the code.

 {
  "size": 0, 
  "aggs": {
    "NAME": {
      "nested": {              // <========== Use nested aggregation
        "path": "Reviewers"
      },
      "aggs": {
        "NAME": {
          "filter": {
            "term": {
              "Reviewers.Reviewer.keyword": "abc" // <========= Filter docs with reviewer = abc
            }
          },
          "aggs": {
            "NAME": {
              "terms": {
                "field": "Reviewers.Status.keyword", // <======= aggregate based on reviewer status
                "size": 10
              }
            }
          }
        }
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

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.