# Elastic search Add additional condition if type is different

**URL:** <https://discuss.elastic.co/t/elastic-search-add-additional-condition-if-type-is-different/299706>\
**Category:** Elasticsearch\
**Tags:** kql-kibana-query-language, eql-elastic-query-language\
**Created:** [March 15, 2022, 9:24am UTC](https://discuss.elastic.co/t/elastic-search-add-additional-condition-if-type-is-different/299706 "2022-03-15T09:24:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![painless\_elastic](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/painless_elastic/32/94652_2.png) [@painless\_elastic](https://discuss.elastic.co/u/painless_elastic)\
**Post date:** [March 15, 2022, 9:24am UTC](https://discuss.elastic.co/t/elastic-search-add-additional-condition-if-type-is-different/299706/1 "2022-03-15T09:24:28Z")

</div>

I have 2 sets of documents, which are joined by **fragmentId**. I have written a query that pulls both documents, but I am thinking is there any other way to write it.

Parent Document - There could be only 1 such document which has **type = fragment** and **fragmentId = 1**

Multiple Child Documents - All will have **type=cf, and fragmentId = 1** , they will have **start and end values.** which will distinguish the children.

```auto
GET test/_search
{
  "query": {
    "bool": {
      "minimum_should_match": 1,
      "should": [
        {
          "bool": {
            "must": [
              {
                "term": {
                  "fragmentId": "1"
                }
              },
              {
                "term": {
                  "type": "fragment"
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must": [
              {
                "term": {
                  "fragmentId": "1"
                }
              },
              {
                "term": {
                  "type": "cf"
                }
              },
              {
                "range" :{
                  "start": {
                    "gte": 1,
                    "lte": 5
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

```

In the above query, you can see, I need to **duplicate** the condition for **type and fragment id** , as when type=cf, I have to **add an extra range filter.**

Is there a way to re-write this query in a more simple form, basically a join operation on fragmentId  
like a parent-child relationship, with an extra check on child docs.?

---

<div class="post-metadata">

**Author:** ![casterQ](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/casterq/32/93257_2.png) [@casterQ](https://discuss.elastic.co/u/casterQ)\
**Post date:** [March 16, 2022, 2:36am UTC](https://discuss.elastic.co/t/elastic-search-add-additional-condition-if-type-is-different/299706/2 "2022-03-16T02:36:11Z")

</div>

like this ? or you want script search?

```auto
GET test/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "fragmentId": "1"
          }
        }
      ],
      "should": [
        {
          "bool": {
            "filter": [
              {
                "term": {
                  "type": "cf"
                }
              },
              {
                "range": {
                  "start": {
                    "gte": 1,
                    "lte": 5
                  }
                }
              }
            ]
          }
        },
        {
          "term": {
            "type": "fragment"
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![painless\_elastic](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/painless_elastic/32/94652_2.png) [@painless\_elastic](https://discuss.elastic.co/u/painless_elastic)\
**Post date:** [March 16, 2022, 10:55am UTC](https://discuss.elastic.co/t/elastic-search-add-additional-condition-if-type-is-different/299706/3 "2022-03-16T10:55:40Z")

</div>

Thanks for the reply, yes this helps in re-structuring the query correctly.  
But is it possible to write a query in such a way that if **type =" fragment" and fragment id = 1** , is not present, then it should not pick the other document with **type="cf"**.  
Basically, I want the combo of both documents, or else no result.

---

<div class="post-metadata">

**Author:** ![casterQ](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/casterq/32/93257_2.png) [@casterQ](https://discuss.elastic.co/u/casterQ)\
**Post date:** [March 17, 2022, 2:32am UTC](https://discuss.elastic.co/t/elastic-search-add-additional-condition-if-type-is-different/299706/4 "2022-03-17T02:32:05Z")

</div>

maybe you can try `script`, However, it’s not recommended and unnecessary

---

<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:** [April 14, 2022, 2:32am UTC](https://discuss.elastic.co/t/elastic-search-add-additional-condition-if-type-is-different/299706/5 "2022-04-14T02:32:11Z")

</div>

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