# Elasticsearch seach using wildcard, range and terms

**URL:** <https://discuss.elastic.co/t/elasticsearch-seach-using-wildcard-range-and-terms/264811>\
**Category:** Elasticsearch\
**Created:** [February 19, 2021, 7:55am UTC](https://discuss.elastic.co/t/elasticsearch-seach-using-wildcard-range-and-terms/264811 "2021-02-19T07:55:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lalitesh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lalitesh/32/84165_2.png) [@lalitesh](https://discuss.elastic.co/u/lalitesh)\
**Post date:** [February 19, 2021, 7:55am UTC](https://discuss.elastic.co/t/elasticsearch-seach-using-wildcard-range-and-terms/264811/1 "2021-02-19T07:55:14Z")

</div>

I am trying to write a search DSL query on a Elasticsearch ( Version 7.1 ) index that has fields like, product\_name, exported\_at and category\_id.

From the document I understand that I would need to use the wildcard, range to search on the date field and terms to filter the required category\_id.

Example: I need to filter the data whose SQL would be:

```
SELECT * from products table
WHERE product_name LIKE '%geography%'
AND exported_at >= '2020-01-01' AND exported_at <= '2020-12-31'
AND category_id IN [10,20,30,40]

```

The DSL query that I build in PHP is:

```
    $params = [
                'index' => ExportData::ELASTIC_INDEX,
                'type' => ExportData::ELASTIC_TYPE,
                'from' => $start,
                'size' => $length,
                'body' => [
                    'query' => [
                        'wildcard' => [
                                'product_name' => [
                                'value' => "*".$keyword."*", 
                                'boost' => 1.0,
                                'rewrite' => 'constant_score'
                            ],
                        ],

                        "range" => [
                            "exported_on" => [
                                "gte" => $requestVars['date_from'],
                                "lte" => $requestVars['date_to'],
                                "boost" => 2.0
                            ]
                        ],
                        "terms" => [
                            "category_id" => [10,20,30,40],
                            "boost" => '1.0'
                        ]
                    ],
                 ]
            ];

```

However, as expected, this fails with the error:

`"message": "{\"error\":{\"root_cause\":[{\"type\":\"parsing_exception\",\"reason\":\"[wildcard] malformed query, expected [END_OBJECT] but found [FIELD_NAME]`

---

<div class="post-metadata">

**Author:** ![nugusbayevkk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nugusbayevkk/32/126683_2.png) [@nugusbayevkk](https://discuss.elastic.co/u/nugusbayevkk)\
**Post date:** [February 19, 2021, 8:10am UTC](https://discuss.elastic.co/t/elasticsearch-seach-using-wildcard-range-and-terms/264811/2 "2021-02-19T08:10:38Z")

</div>

Hi @lalitesh , I think that you need use [bool query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html):

---

<div class="post-metadata">

**Author:** ![matriv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matriv/32/43656_2.png) [@matriv](https://discuss.elastic.co/u/matriv)\
**Post date:** [February 22, 2021, 9:28am UTC](https://discuss.elastic.co/t/elasticsearch-seach-using-wildcard-range-and-terms/264811/3 "2021-02-22T09:28:15Z")

</div>

You can use the [SQL translate API](https://www.elastic.co/guide/en/elasticsearch/reference/current/sql-translate.html) to see the query DSL.

---

<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:** [March 22, 2021, 9:28am UTC](https://discuss.elastic.co/t/elasticsearch-seach-using-wildcard-range-and-terms/264811/4 "2021-03-22T09:28:39Z")

</div>

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