# Query to get distinct values satisfying two or more conditions

**URL:** <https://discuss.elastic.co/t/query-to-get-distinct-values-satisfying-two-or-more-conditions/202659>\
**Category:** Elasticsearch\
**Created:** [October 8, 2019, 11:57am UTC](https://discuss.elastic.co/t/query-to-get-distinct-values-satisfying-two-or-more-conditions/202659 "2019-10-08T11:57:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ajsal\_shereef](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ajsal_shereef/32/55537_2.png) [@ajsal\_shereef](https://discuss.elastic.co/u/ajsal_shereef)\
**Post date:** [October 8, 2019, 11:57am UTC](https://discuss.elastic.co/t/query-to-get-distinct-values-satisfying-two-or-more-conditions/202659/1 "2019-10-08T11:57:12Z")

</div>

Hi,  
I'm a newbie to elastic search. I was working on some example queries. I have loaded Kibana flight data to the console. Now, I wanted to know the query which will fetch distict flights which land in Italy. Any help would be largely appreciated.  
Thanks

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [October 9, 2019, 11:16am UTC](https://discuss.elastic.co/t/query-to-get-distinct-values-satisfying-two-or-more-conditions/202659/2 "2019-10-09T11:16:46Z")

</div>

Distinct, based on what criterion?

To find all flights to Italy, you can use a [`match` query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html) on the `DestCountry` field:

```auto
GET kibana_sample_data_flights/_search
{
  "size": 10,
  "query": {
    "match": {
      "DestCountry": "IT"
    }
  }
}

```

The response will tell you the total number of hits. The default behavior of this query is to return just 10 hits, but by increasing the `size` you can retrieve more. Or, you can [combine `size` with `from` to paginate](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-from-size) through the search results.

---

<div class="post-metadata">

**Author:** ![ajsal\_shereef](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ajsal_shereef/32/55537_2.png) [@ajsal\_shereef](https://discuss.elastic.co/u/ajsal_shereef)\
**Post date:** [October 9, 2019, 11:39am UTC](https://discuss.elastic.co/t/query-to-get-distinct-values-satisfying-two-or-more-conditions/202659/3 "2019-10-09T11:39:28Z")

</div>

Distinct I mean, A flight can go to Italy more than once. The dataset records this as the same flight going to Italy but as a different observation. But then, I wanted to remove those duplicate flight entries. I have tried one piece of code. I'm attaching the query here.

```
GET /kibana_sample_data_flights/_doc/_search?q=DestCountry : IT
{
  "size":0,
  "aggs":{
    "distinct_flights":{
      "cardinality":{
        "field":"FlightNum"
      }
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [October 9, 2019, 11:42am UTC](https://discuss.elastic.co/t/query-to-get-distinct-values-satisfying-two-or-more-conditions/202659/4 "2019-10-09T11:42:43Z")

</div>

Alright, in the sample dataset all flights only go to a destination once, but yes - your query will tell you the unique number of flights to a destination.

---

<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:** [November 6, 2019, 11:43am UTC](https://discuss.elastic.co/t/query-to-get-distinct-values-satisfying-two-or-more-conditions/202659/5 "2019-11-06T11:43:02Z")

</div>

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