# Count unique values from the filter query result, taking two fields into account

**URL:** <https://discuss.elastic.co/t/count-unique-values-from-the-filter-query-result-taking-two-fields-into-account/245220>\
**Category:** Elasticsearch\
**Created:** [August 17, 2020, 10:32am UTC](https://discuss.elastic.co/t/count-unique-values-from-the-filter-query-result-taking-two-fields-into-account/245220 "2020-08-17T10:32:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Miguel\_BA](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/miguel_ba/32/46926_2.png) [@Miguel\_BA](https://discuss.elastic.co/u/Miguel_BA)\
**Post date:** [August 17, 2020, 10:32am UTC](https://discuss.elastic.co/t/count-unique-values-from-the-filter-query-result-taking-two-fields-into-account/245220/1 "2020-08-17T10:32:05Z")

</div>

I have a query that filters the "desc = mls or door = 33". Next I want a count value with a agregations the unique values in two fields.

```auto
 "query": {
    "nested": {
      "path": "data.addresses",
      "inner_hits": {
        "_source": ["data.addresses.p"],
        "size":100
      },
      "query": {
        "nested": {
          "path": "data.addresses.services",
          "inner_hits": {
        "_source": ["data.addresses.p", "data.addresses.services.door",
        "size":100
      }, 
      "query": {
        "bool": {
          "should": [
              {"match": {"data.addresses.services.desc": "mls"}},
              {"term": { "data.addresses.services.door.keyword": "33"}}
              ]
            }
          }
        }
      }
    }
  },
  "aggs": {
    "nestedP": {
      "nested": {
        "path": "data.addresses"
      },
      "aggs": {
        "termField": {
          "terms": {
            "field": "data.addresses.p.keyword",
            "size": 1000
          },
          "aggs": {
            "NESTEdoor": {
              "nested": {
                "path": "data.addresses.services"
              },
              "aggs": {
                "AGGPOR": {
                  "terms": {
                    "field": "data.addresses.services.door.keyword",
                    "size": 1000
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

```

I have a query that filters the "desc = mls or door = 33". Next I want a count value with a agregations the unique values in two fields.

output of fitler query:

```auto
  {
    "data.addresses.services.desc.keyword" : "mls",<---- same mls and 33
    "data.addresses.services.door.keyword" : 33
  },
  {
    "data.addresses.services.desc.keyword" : "mls",<---- same mls and 33
    "data.addresses.services.door.keyword" : 33
  },
  {
    "data.addresses.services.desc.keyword" : "mls",<---- diferent port 33
    "data.addresses.services.door.keyword" : 98

  },

```

I am trying to make agregations about the output of the filter query for to get a count value :  
Desired output aggregation

```auto
 {
    "key" : "mls",
    "doc_count" : 2
  },

```

Is possible to get a query that counts how many unique values ​​the result of the query filter has taking into account two fields?

---

<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:** [September 14, 2020, 10:32am UTC](https://discuss.elastic.co/t/count-unique-values-from-the-filter-query-result-taking-two-fields-into-account/245220/2 "2020-09-14T10:32:29Z")

</div>

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