# Condition on term

**URL:** <https://discuss.elastic.co/t/condition-on-term/229644>\
**Category:** Elasticsearch\
**Created:** [April 24, 2020, 12:11pm UTC](https://discuss.elastic.co/t/condition-on-term/229644 "2020-04-24T12:11:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nachomartin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nachomartin/32/65992_2.png) [@nachomartin](https://discuss.elastic.co/u/nachomartin)\
**Post date:** [April 24, 2020, 12:11pm UTC](https://discuss.elastic.co/t/condition-on-term/229644/1 "2020-04-24T12:11:44Z")

</div>

Hi guys, sorry to bother, just wanted to know if there is possible to add a condition for a term. So I have something like this:

` "should": [{ "term": { "results.value.testRun.name": "Int" } }, { "term": { "results.value.testRun.name": "Test" } }, { "term": { "results.value.testRun.name": "Live" } }]`

And then I have an aggs to show only if min\_doc\_count : 2.

My question is if there is anyway to add a condition to the last term "results.value.testRun.name": "Live" to allow me to show results even if min\_doc\_count: 1 (But should be :2 for the other ones)

Did I explain good ?

Thanks.

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [April 24, 2020, 12:27pm UTC](https://discuss.elastic.co/t/condition-on-term/229644/2 "2020-04-24T12:27:19Z")

</div>

I don't quite understand what the aggregation is for. And how the field content for `results.value.testRun.name` looks like for some example documents. Is this a multi valued keyword field? Or some sort of text field where you want to match any of the three given terms?  
Maybe an example with the mapping for the field, some example documents and a full example query including the agg would help to understand your goal better.

---

<div class="post-metadata">

**Author:** ![nachomartin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nachomartin/32/65992_2.png) [@nachomartin](https://discuss.elastic.co/u/nachomartin)\
**Post date:** [April 24, 2020, 12:32pm UTC](https://discuss.elastic.co/t/condition-on-term/229644/3 "2020-04-24T12:32:40Z")

</div>

Thanks!

So, basically the field content is a string ("Live", "Test"). The aggregation is to show the testcases that belong to that testrun but if they failed more than 2 times.

So,  
"aggs": {  
"testname": {  
"terms": {  
"field": "results.value.testCase.name",  
"size": 1000,  
"min\_doc\_count": 2  
},

What I would like to do is, if the testrun is == Live, then min\_doc\_count:1.

The idea is to retrieve the tests that failed more than 1 time in test, but if it failed just 1 time on Live, retrieve it anyways.

Is that better ?

Hope this helps:

"filter": {  
"bool": {  
"should": [  
{  
"term": {  
"results.value.testRun.name": "Test"  
}  
},  
{  
"term": {  
"results.value.testRun.name": "Live"  
}  
}  
]  
}  
}  
}  
},  
"aggs": {  
"testname": {  
"terms": {  
"field": "results.value.testCase.name",  
"size": 1000,  
"min\_doc\_count": 2  
},

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [April 24, 2020, 1:12pm UTC](https://discuss.elastic.co/t/condition-on-term/229644/4 "2020-04-24T13:12:11Z")

</div>

I'm making some bold guesses abour your use case here but maybe you could use a [`filter` aqggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filter-aggregation.html) to create a bucket with all the docs with a "Live" failure and another top level one with all the results where you would use `"min_doc_count" : 2`? I think you need to de-duplicate somewhere in your application then unfortunately. This is a short example of what I mean but it might need some adaption to fit your needs:

```auto
DELETE my_index

POST my_index/_bulk
{ "index" : { } }
{ "testRun" : "Test", "testCase" : "caseA" }
{ "index" : { } }
{ "testRun" : "Live", "testCase" : "caseA" }
{ "index" : { } }
{ "testRun" : "Test", "testCase" : "caseB" }
{ "index" : { } }
{ "testRun" : "Live", "testCase" : "caseC" }

POST my_index/_search?size=0
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "testRun": "test"
          }
        },
        {
          "term": {
            "testRun": "live"
          }
        }
      ]
    }
  },
  "aggs": {
    "live_only": {
      "filter": {
        "term": {
          "testRun": "live"
        }
      },
      "aggs": {
        "live_once": {
          "terms": {
            "field": "testCase.keyword",
            "size": 1000,
            "min_doc_count": 1
          }
        }
      }
    },
    "all": {
      "terms": {
        "field": "testCase.keyword",
        "size": 1000,
        "min_doc_count": 2
      }
    }
  }
}

```

Btw. using ``` around your code snippets for easier-to-read formatting is highly appreciated in the forum. Thanks.

---

<div class="post-metadata">

**Author:** ![nachomartin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nachomartin/32/65992_2.png) [@nachomartin](https://discuss.elastic.co/u/nachomartin)\
**Post date:** [April 24, 2020, 1:19pm UTC](https://discuss.elastic.co/t/condition-on-term/229644/5 "2020-04-24T13:19:34Z")

</div>

Thanks a lot, I will give that a try. Appreciate your help and sorry for formatting 😃

---

<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:** [May 22, 2020, 1:19pm UTC](https://discuss.elastic.co/t/condition-on-term/229644/6 "2020-05-22T13:19:36Z")

</div>

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