# Need to implement a if then do this kind of query on elastic search

**URL:** <https://discuss.elastic.co/t/need-to-implement-a-if-then-do-this-kind-of-query-on-elastic-search/112829>\
**Category:** Elasticsearch\
**Created:** [December 21, 2017, 1:13pm UTC](https://discuss.elastic.co/t/need-to-implement-a-if-then-do-this-kind-of-query-on-elastic-search/112829 "2017-12-21T13:13:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shashank\_Paliwal](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@Shashank\_Paliwal](https://discuss.elastic.co/u/Shashank_Paliwal)\
**Post date:** [December 21, 2017, 1:13pm UTC](https://discuss.elastic.co/t/need-to-implement-a-if-then-do-this-kind-of-query-on-elastic-search/112829/1 "2017-12-21T13:13:37Z")

</div>

I have an elastic search structure like below

```auto
    {
  "_source": {
    "@timestamp": "2017-12-08T16:31:27.106Z",
    "@version": "1",
    "doc": {
      "core": {
        "type": "admin",
        "component": {
          "items": {}
        }
      }
    }
  },
  "sort": [
    1512641620092
  ]
}

```

Here the value of type may change, like "advisor" etc, I need a query to fetch all types but for admin all the data where the "component.items" is not blank

SO the result should contain all types + (admin with component.type not blank )

I am new to elastic search please help here.

Thanks in advance for any help

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [December 21, 2017, 2:13pm UTC](https://discuss.elastic.co/t/need-to-implement-a-if-then-do-this-kind-of-query-on-elastic-search/112829/2 "2017-12-21T14:13:27Z")

</div>

```auto
{
  "query": {
    "bool": {
      "should": [
        { "exists": { "field": "doc.core.component.items" } },
        { "bool": { "must": { "match_all": {} }, "must_not": { "match": { "doc.core.type": "admin" } } } }
      ]
    }
  }
}

```

---

<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:** [January 18, 2018, 2:13pm UTC](https://discuss.elastic.co/t/need-to-implement-a-if-then-do-this-kind-of-query-on-elastic-search/112829/3 "2018-01-18T14:13:27Z")

</div>

Closed on January 18, 2018, 2:13pm UTC

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