# Querying multiple condition on same field

**URL:** https://discuss.elastic.co/t/querying-multiple-condition-on-same-field/207233
**Category:** Elasticsearch
**Created:** [November 10, 2019, 9:39am UTC](https://discuss.elastic.co/t/querying-multiple-condition-on-same-field/207233 "2019-11-10T09:39:18Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![rsganesh83](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rsganesh83/32/57418_2.png) [@rsganesh83](https://discuss.elastic.co/u/rsganesh83)
#### Post date: [November 10, 2019, 9:39am UTC](https://discuss.elastic.co/t/querying-multiple-condition-on-same-field/207233/1 "2019-11-10T09:39:18Z")

</div>

These are the sample documents(event based) indexed in my ES cluster version 6.8

```auto
{
  "user_id": "user1",
  "account_id": "account1",
  "event_name": "event-1",
  "created_dt": "2019-09-30T08:40:42.297Z"
}

{
"user_id": "user1",
"account_id": "account1",
"event_name": "event-2",
"created_dt": "2019-10-30T08:40:42.297Z"
}

{
"user_id": "user2",
"account_id": "account1",
"event_name": "event-1",
"created_dt": "2019-10-30T08:40:42.297Z"
}

```

What i am trying to accomplish is fetching the list of users who have taken event\_name "event-1" and "event-2" on stipulated time dynamically.

The query below which is having 2 must condition on same field "event\_name" which eventually results empty.

```auto
{
"query": {
"bool": {
  "must": [
    {
      "match": {
        "account_id": "account1"
      }
    },
    {
      "bool": {
        "must": [
          {
            "match": {
              "event_name.raw": "event-1"
            }
          },
          {
            "range": {
              "created_dt": {
                "gte": "2019-09-30",
                "lte": "2019-10-30"
              }
            }
          }
        ]
      }
    },
    {
      "bool": {
        "must": [
          {
            "match": {
              "event_name.raw": "event-2"
            }
          },
          {
            "range": {
              "created_dt": {
                "gte": "2019-10-30",
                "lte": "2019-11-05"
              }
            }
          }
        ]
      }
    }
  ]
}
},
"size": 0,
"aggs": {
"user_list": {
  "terms": {
    "field": "user_id.raw"
  }
}
}
}

```

what is the ideal way to get the desired bucket documents? It can be done in SQL using IN and a sub query but couldn't look easier in ES

---

<div class="post-metadata">

### Author: ![rsganesh83](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rsganesh83/32/57418_2.png) [@rsganesh83](https://discuss.elastic.co/u/rsganesh83)
#### Post date: [November 13, 2019, 6:57am UTC](https://discuss.elastic.co/t/querying-multiple-condition-on-same-field/207233/2 "2019-11-13T06:57:30Z")

</div>

any update please?

---

<div class="post-metadata">

### Author: ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)
#### Post date: [November 13, 2019, 6:49pm UTC](https://discuss.elastic.co/t/querying-multiple-condition-on-same-field/207233/3 "2019-11-13T18:49:44Z")

</div>

A SQL IN clause often results in a join, which Elasticsearch does not support. To do this you may need to resort to [an entity-centric index](https://www.elastic.co/elasticon/2015/sf/building-entity-centric-indexes). You may also be able to do this through the use of [transforms](https://www.elastic.co/guide/en/elasticsearch/reference/7.4/transform-usage.html).

---

<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: [December 11, 2019, 6:49pm UTC](https://discuss.elastic.co/t/querying-multiple-condition-on-same-field/207233/4 "2019-12-11T18:49:52Z")

</div>

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