# Converting 1.7 "terms" with execution "and" query DSL to 2.1.1 due to "terms" breaking changes

**URL:** <https://discuss.elastic.co/t/converting-1-7-terms-with-execution-and-query-dsl-to-2-1-1-due-to-terms-breaking-changes/40291>\
**Category:** Elasticsearch\
**Created:** [January 27, 2016, 9:29pm UTC](https://discuss.elastic.co/t/converting-1-7-terms-with-execution-and-query-dsl-to-2-1-1-due-to-terms-breaking-changes/40291 "2016-01-27T21:29:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zelfapp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zelfapp/32/108888_2.png) [@Zelfapp](https://discuss.elastic.co/u/Zelfapp)\
**Post date:** [January 27, 2016, 9:29pm UTC](https://discuss.elastic.co/t/converting-1-7-terms-with-execution-and-query-dsl-to-2-1-1-due-to-terms-breaking-changes/40291/1 "2016-01-27T21:29:36Z")

</div>

The following 1.7 Query DSL (php-client formatted) code makes use of the "terms" filter and execution "and" to require all terms to be found in documents. With the ["terms" breaking change in 2.1.1](https://www.elastic.co/guide/en/elasticsearch/reference/current/breaking_20_query_dsl_changes.html) I need to rewrite the query. It appears that "terms" in 2.1.1 is only good for finding documents with "any" of the terms provided, which raises some questions about how to convert code like this.

1. Is the new best practice to separate all the "terms" formerly executed with "and" into separate "term" filters, and then wrap all the "term" filters in ["and" query](https://www.elastic.co/guide/en/elasticsearch/reference/2.1/query-dsl-and-query.html)? You can see my updated 2.1.1 query below. Seems to work, but I wonder if this is the new best practice for handling of "terms" filtered queries, formerly executed with "and"?

**1.7 Query DSL**

```
$query = [
    'query' => [
        'filtered' => [
            'filter' => [
                'bool' => [
                    'must' => [
                        [
                            'term' => [
                                'award_sku' => (string) $data['award_sku']
                            ]
                        ],
                        [
                            'term' => [
                                'class' => (string) $data['class']
                            ]
                        ],
                        [
                            'terms' => [
                                'params' => $data['params'],
                                'execution' => 'and',
                                '_cache' => true
                            ]
                        ],
                        [
                            'term' => [
                                'params_count' => $data['params_count']
                            ]
                        ]
                    ],
                    '_cache' => true
                ]
            ]
        ]
    ]
];

```

**2.1.1 Query DSL (so far)**

```
$query = [
    'query' => [
        'bool' => [
            'filter' => [
                'and' => [
                    [
                        'term' => [
                            'award_sku' => (string) $data['award_sku']
                        ]
                    ],
                    [
                        'term' => [
                            'class' => (string) $data['class']
                        ]
                    ],
                    [
                        'term' => [
                            'params_count' => $data['params_count']
                        ]
                    ],
                    [
                        'term' => [
                            'params' => $param1
                        ]
                    ],
                    [
                        'term' => [
                            'params' => $param2
                        ]
                    ]
                ]
            ]
        ]
    ]
];
```

---

<div class="post-metadata">

**Author:** ![Zelfapp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zelfapp/32/108888_2.png) [@Zelfapp](https://discuss.elastic.co/u/Zelfapp)\
**Post date:** [January 28, 2016, 6:32pm UTC](https://discuss.elastic.co/t/converting-1-7-terms-with-execution-and-query-dsl-to-2-1-1-due-to-terms-breaking-changes/40291/2 "2016-01-28T18:32:14Z")

</div>

Any ideas anybody?

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [January 28, 2016, 8:26pm UTC](https://discuss.elastic.co/t/converting-1-7-terms-with-execution-and-query-dsl-to-2-1-1-due-to-terms-breaking-changes/40291/3 "2016-01-28T20:26:47Z")

</div>

Two main things. First, the `and`/`or`/`not` queries are deprecated, and will be removed in the future. Instead, you can just use a `bool` with must clauses to replicate the and.

Second, I'd just iterate over `$data['params']` and add those individually to the query as mandatory `term` clauses. It's a bit more work than pre 2.0, but it should give the same result (and'ing a bunch of terms together, effectively). Something like:

```php
$query = [
    'query' => [
        'bool' => [
            'filter' => [
                'bool' => [
                    'must' => [
                        [
                            'term' => [
                                'award_sku' => (string) $data['award_sku']
                            ]
                        ],
                        [
                            'term' => [
                                'class' => (string) $data['class']
                            ]
                        ],
                        [
                            'term' => [
                                'params_count' => $data['params_count']
                            ]
                        ]
                    ]
                ]
            ]
        ]
    ]
];

foreach ($data['params'] as $param) {
  $query['bool']['filter']['bool']['must'][] = [
    'term' => [
      'params' => $param
    ]
  ]
}

```

---

<div class="post-metadata">

**Author:** ![Zelfapp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zelfapp/32/108888_2.png) [@Zelfapp](https://discuss.elastic.co/u/Zelfapp)\
**Post date:** [January 28, 2016, 10:30pm UTC](https://discuss.elastic.co/t/converting-1-7-terms-with-execution-and-query-dsl-to-2-1-1-due-to-terms-breaking-changes/40291/4 "2016-01-28T22:30:10Z")

</div>

polyfractal, as always, thanks for your help.

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [January 31, 2016, 8:07pm UTC](https://discuss.elastic.co/t/converting-1-7-terms-with-execution-and-query-dsl-to-2-1-1-due-to-terms-breaking-changes/40291/5 "2016-01-31T20:07:10Z")

</div>

Np, happy to help! 🙂

---

<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:20pm UTC](https://discuss.elastic.co/t/converting-1-7-terms-with-execution-and-query-dsl-to-2-1-1-due-to-terms-breaking-changes/40291/6 "2017-07-05T23:20:04Z")

</div>


