# Is there an alternative solution to terms "execution": "and" on ES 2.x?

**URL:** <https://discuss.elastic.co/t/is-there-an-alternative-solution-to-terms-execution-and-on-es-2-x/41089>\
**Category:** Elasticsearch\
**Created:** [February 5, 2016, 5:27pm UTC](https://discuss.elastic.co/t/is-there-an-alternative-solution-to-terms-execution-and-on-es-2-x/41089 "2016-02-05T17:27:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![luisrpp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/luisrpp/32/7625_2.png) [@luisrpp](https://discuss.elastic.co/u/luisrpp)\
**Post date:** [February 5, 2016, 5:27pm UTC](https://discuss.elastic.co/t/is-there-an-alternative-solution-to-terms-execution-and-on-es-2-x/41089/1 "2016-02-05T17:27:30Z")

</div>

Given that I have following document in an index:

```auto
PUT /test_index/my_type/1
{
    "id": 1,
    "my_array": ["1", "2", "3", "4"]
}

```

If I run the following query on ES 1.x, it gives me 0 total hits, as expected:

```auto
POST /test_index/my_type/_search
{
    "query": {
        "function_score": {
            "score_mode": "sum",
            "filter": {
                "bool": {
                    "must": {
                        "terms": {
                            "my_array": ["1", "5"],
                            "execution": "and"
                        }
                    }
                }
            }
        }
    },
    "size": 50,
    "from": 0
}

```

But on ES 2.x, as the **execution** does not work with terms anymore, what would be the best aproach to rewrite this query? Is there a better option than the following one?

```auto
POST /test_index/my_type/_search
{
    "query": {
        "function_score": {
            "score_mode": "sum",
            "filter": {
                "bool": {
                    "must": [{
                        "terms": {
                            "my_array": ["1"]
                        }
                    },
                    {
                        "terms": {
                            "my_array": ["5"]
                        }
                    }]
                }
            }
        }
    },
    "size": 50,
    "from": 0
}

```

Best Regards,  
Luis

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [February 5, 2016, 6:39pm UTC](https://discuss.elastic.co/t/is-there-an-alternative-solution-to-terms-execution-and-on-es-2-x/41089/2 "2016-02-05T18:39:37Z")

</div>

I'd write it as.

```auto
"query": {
  "bool": {
    "filter": [
      { "term": { "my_array": "1" } },
      { "term": { "my_array": "5" } }
    ]
  }
}

```

I can't speak for why the execution mode is gone. It could have to do with Elasticsearch making a conscious effort to move its queries closer to the queries available in Lucene. And the TermsQuery only supports OR.

Fun fact: its rewritten into a bool query with lots of must not clauses if the number of terms is less than 12 or so because its particular optimizations don't help without a boat load of terms.

---

<div class="post-metadata">

**Author:** ![luisrpp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/luisrpp/32/7625_2.png) [@luisrpp](https://discuss.elastic.co/u/luisrpp)\
**Post date:** [February 5, 2016, 7:11pm UTC](https://discuss.elastic.co/t/is-there-an-alternative-solution-to-terms-execution-and-on-es-2-x/41089/3 "2016-02-05T19:11:26Z")

</div>

Thanks for your reply!

Your solution is slightly better!

That would become something like this:

```auto
POST /test_index/my_type/_search
{
    "query": {
        "function_score": {
            "score_mode": "sum",
            "filter": {
                "and": {
                    "filter": [
                        { "term": { "my_array": "1" } },
                        { "term": { "my_array": "2" } },
                        { "term": { "my_array": "5" } }
                    ]
                }
            }
        }
    },
    "size": 50,
    "from": 0
}

```

However, I was looking for a way to pass an array instead of a term for each item. I guess there is no other way.

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [February 5, 2016, 7:38pm UTC](https://discuss.elastic.co/t/is-there-an-alternative-solution-to-terms-execution-and-on-es-2-x/41089/4 "2016-02-05T19:38:09Z")

</div>

You'd have to make a list of queries, yes. I wouldn't swap `bool` query for `and`. Keep `bool`. `and` is deprecated. Presumably you need the `function_score` because you have some function that you aren't adding to the post?

---

<div class="post-metadata">

**Author:** ![luisrpp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/luisrpp/32/7625_2.png) [@luisrpp](https://discuss.elastic.co/u/luisrpp)\
**Post date:** [February 5, 2016, 7:51pm UTC](https://discuss.elastic.co/t/is-there-an-alternative-solution-to-terms-execution-and-on-es-2-x/41089/5 "2016-02-05T19:51:18Z")

</div>

You are right! Thanks for the tip. I will use **bool**. And yes, I have a function here, but **function\_score** is not relevant for the scope of this post.

---

<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:** [July 5, 2017, 11:18pm UTC](https://discuss.elastic.co/t/is-there-an-alternative-solution-to-terms-execution-and-on-es-2-x/41089/6 "2017-07-05T23:18:29Z")

</div>


