# Elasticsearch aggs filter with condition

**URL:** <https://discuss.elastic.co/t/elasticsearch-aggs-filter-with-condition/216372>\
**Category:** Elasticsearch\
**Created:** [January 24, 2020, 6:55am UTC](https://discuss.elastic.co/t/elasticsearch-aggs-filter-with-condition/216372 "2020-01-24T06:55:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Noctis17](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/noctis17/32/41656_2.png) [@Noctis17](https://discuss.elastic.co/u/Noctis17)\
**Post date:** [January 24, 2020, 6:55am UTC](https://discuss.elastic.co/t/elasticsearch-aggs-filter-with-condition/216372/1 "2020-01-24T06:55:36Z")

</div>

Elasticsearch v 7.5

Hello and good day!

I have an index named **articles** , and has the following sample contents:

```auto
{
    "_id" : 1,
    "title" : "Title 1"
    "cms" : [
        {
          "cli_id" : 0,
          "cmx_sentiment" : "Negative"
        },
        {
          "cli_id" : 4599,
          "cmx_sentiment" : "Positive"
        }
    ]
},
{
    "_id" : 2,
    "title" : "Title 2"
    "cms" : [
        {
          "cli_id" : 0,
          "cmx_sentiment" : "Positive"
        }
    ]
},
{
    "_id" : 3,
    "title" : "Title 3"
    "cms" : [
        {
          "cli_id" : 0,
          "cmx_sentiment" : "Neutral"
        }
    ]
},
{
    "_id" : 4,
    "title" : "Title 4"
    "cms" : [
        {
          "cli_id" : 0,
          "cmx_sentiment" : "Positive"
        },
        {
          "cli_id" : 12,
          "cmx_sentiment" : "Negative"
        }
    ]
}

```

I want to create an ES query with aggs to display the **cms.cmx\_sentiment** for a report, but with a condition

I have this skeleton query:

```auto
GET socialmedia/_search
{
  "size" : 1,
  "query": {
    "match_all": {}
  },
  "aggs": {
    "CMS": {
      "nested": {
        "path": "cms"
      }
    }
  }
}

```

this is the output:

```auto
"aggregations" : {
    "CMS" : {
      "doc_count" : 6
    }
  }

```

**doc\_count** is 6 because

- **article 1** has 2 cms nested elements
- **article 2** has 1 nested element
- **article 3** has 1 nested element and
- **article 4** has 2 nested elements

Now, I want to display the **cms.cmx\_sentiment** on a **filter** basis to produce a report **BUT** I must apply a condition

**IMPROVING THE QUERY ABOVE**

```auto
GET socialmedia/_search
{
  "size" : 0,
  "query": {
    "match_all": {}
  },
  "aggs": {
    "CMS": {
      "nested": {
        "path": "cms"
      },
      "aggs": {
        "FILTER": {
          "filter": {
            "bool": {
              "must" : [
                {
                  "match" : {
                    "cms.cli_id" : 0 <--- I need a query to make this dynamic
                  }
                }
              ]
            }
          },
          "aggs": {
            "SENTIMENT": {
              "terms": {
                "field" : "cms.cmx_sentiment"
              }
            }
          }
        }
      }
    }
  }
}

```

**OUTPUT**

```auto
"aggregations" : {
  "CMS" : {
    "doc_count" : 6,
    "FILTER" : {
      "doc_count" : 1,
      "SENTIMENT" : {
        "doc_count_error_upper_bound" : 0,
        "sum_other_doc_count" : 0,
        "buckets" : [
          {
            "key" : "Positive",
            "doc_count" : 2
          },
          {
            "key" : "Negative",
            "doc_count" : 1
          },
          {
            "key" : "Neutral",
            "doc_count" : 1
          }
        ]
      }
    }
  }
}

```

**OBJECTIVE**

Is there a way for me to perform an ES script with this PHP logic?

- if the **cli\_id** of the logged-in user is **in\_array** of the **cms.cli\_id** nested field, fetch its corresponding **cmx\_sentiment**
- if the **cli\_id** of the logged-in user **IS NOT** in the nested elements of **cms** , fetch the elements with **cli\_id : 0** , then get its **cmx\_sentiment**

**USE CASES**

with the given examples above of the **articles** index

**IF cli\_id 4599 logs in to the app**

```auto
"SENTIMENT" : {
  "doc_count_error_upper_bound" : 0,
  "sum_other_doc_count" : 0,
  "buckets" : [
    {
      "key" : "Positive",
      "doc_count" : 3
    },
    {
      "key" : "Neutral",
      "doc_count" : 1
    }
  ]
}

```

**IF cli\_id 12 logs in to the app**

```auto
"SENTIMENT" : {
  "doc_count_error_upper_bound" : 0,
  "sum_other_doc_count" : 0,
  "buckets" : [
    {
      "key" : "Negative",
      "doc_count" : 2
    },
    {
      "key" : "Positive",
      "doc_count" : 1
    },
    {
      "key" : "Neutral",
      "doc_count" : 1
    }
  ]
}

```

**IF any cli\_id logs in to the app**

```auto
"SENTIMENT" : {
  "doc_count_error_upper_bound" : 0,
  "sum_other_doc_count" : 0,
  "buckets" : [
    {
      "key" : "Positive",
      "doc_count" : 2
    },
    {
      "key" : "Negative",
      "doc_count" : 1
    },
    {
      "key" : "Neutral",
      "doc_count" : 1
    }
  ]
}

```

How can improve my query so I could place a **conditional statement** in the **FILTER** aggs of my query?

---

<div class="post-metadata">

**Author:** ![Noctis17](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/noctis17/32/41656_2.png) [@Noctis17](https://discuss.elastic.co/u/Noctis17)\
**Post date:** [January 30, 2020, 2:25am UTC](https://discuss.elastic.co/t/elasticsearch-aggs-filter-with-condition/216372/2 "2020-01-30T02:25:40Z")

</div>

Any ideas about this, ES admins and devs?

---

<div class="post-metadata">

**Author:** ![Noctis17](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/noctis17/32/41656_2.png) [@Noctis17](https://discuss.elastic.co/u/Noctis17)\
**Post date:** [February 4, 2020, 2:19am UTC](https://discuss.elastic.co/t/elasticsearch-aggs-filter-with-condition/216372/3 "2020-02-04T02:19:04Z")

</div>

up, anyone please?

---

<div class="post-metadata">

**Author:** ![Noctis17](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/noctis17/32/41656_2.png) [@Noctis17](https://discuss.elastic.co/u/Noctis17)\
**Post date:** [February 13, 2020, 2:25am UTC](https://discuss.elastic.co/t/elasticsearch-aggs-filter-with-condition/216372/4 "2020-02-13T02:25:43Z")

</div>

Anyone from the respected devs / admins please?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 12, 2020, 2:25am UTC](https://discuss.elastic.co/t/elasticsearch-aggs-filter-with-condition/216372/5 "2020-03-12T02:25:46Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
