# What to do with query inside filter query

**URL:** <https://discuss.elastic.co/t/what-to-do-with-query-inside-filter-query/296246>\
**Category:** Elasticsearch\
**Tags:** migration\
**Created:** [February 4, 2022, 3:15am UTC](https://discuss.elastic.co/t/what-to-do-with-query-inside-filter-query/296246 "2022-02-04T03:15:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![JunyaT](https://avatars.discourse-cdn.com/v4/letter/j/ba8739/32.png) [@JunyaT](https://discuss.elastic.co/u/JunyaT)\
**Post date:** [February 4, 2022, 3:15am UTC](https://discuss.elastic.co/t/what-to-do-with-query-inside-filter-query/296246/1 "2022-02-04T03:15:10Z")

</div>

Hi, people from Elasticsearch.

I have struggles trying to migrate to Elasticsearch 7.11.1 from very old Elasticsearch 1.3.

"The original query" below is used in our codebase to be executed against Elasticsearch 1.3 and it used to be working fine.

After switching to Elasticsearch 7.11.1, we got an error that "filtered" query is deprecated and fixed it by replacing "filtered" with "bool" query which turned into "The fixed query".

Now we got an error of "unknown query [query]" when we executing the "The fixed query". Please see the "The error response" at the bottom for details.

I'm new to Elasticsearch, so not sure how should I fix this.

Please tell me how should I fix this to make the query work against the Elasticsearch 7.11.1.

**The original query:**

```auto
curl -H 'content-type: application/json' http://elasticsearch:9200/ticket-status/_search -d '
{
    "query": {
        "filtered": {
            "filter": {
                "and": [
                    {
                        "term": {
                            "_appId": "1"
                        }
                    },
                    {
                        "range": {
                            "@timestamp": {
                                "from": "1641222000",
                                "to": "1643986799"
                            }
                        }
                    },
                    {
                        "query": {
                            "query_string": {
                                "default_field": "_ticketContent",
                                "query": ""
                            }
                        }
                    }
                ]
            }
        }
    },
    "size": 0,
    "aggs": {
        "uu_heat_ticket_histogram": {
            "date_histogram": {
                "field": "_logTime",
                "time_zone": "Asia/Tokyo",
                "interval": "day"
            },
            "aggs": {
                "distinct_customers": {
                    "terms": {
                        "field": "_customerId",
                        "size": 0
                    }
                }
            }
        }
    }
}'

```

**The fixed query:**

```auto
curl -H 'content-type: application/json' http://elasticsearch:9200/ticket-status/_search -d '
{
    "query": {
        "bool": {
            "filter": [
                {
                    "term": {
                        "_appId": "1"
                    }
                },
                {
                    "range": {
                        "@timestamp": {
                            "from": "1641222000",
                            "to": "1643986799"
                        }
                    }
                },
                {
                    "query": { 👈 this caused an error
                        "query_string": {
                            "default_field": "_ticketContent",
                            "query": ""
                        }
                    }
                }
            ]
        }
    },
    "size": 10000,
    "aggs": {
        "uu_heat_ticket_histogram": {
            "date_histogram": {
                "field": "_logTime",
                "time_zone": "Asia/Tokyo",
                "calendar_interval": "day"
            },
            "aggs": {
                "distinct_customers": {
                    "terms": {
                        "field": "_customerId",
                        "size": 10000
                    }
                }
            }
        }
    }
}'

```

**The error response:**

```auto
{
  "error": {
    "root_cause": [
      {
        "type": "parsing_exception",
        "reason": "unknown query [query]",
        "line": 20,
        "col": 30
      }
    ],
    "type": "x_content_parse_exception",
    "reason": "[20:30] [bool] failed to parse field [filter]",
    "caused_by": {
      "type": "parsing_exception",
      "reason": "unknown query [query]",
      "line": 20,
      "col": 30,
      "caused_by": {
        "type": "named_object_not_found_exception",
        "reason": "[20:30] unknown field [query]"
      }
    }
  },
  "status": 400
}

```

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [February 4, 2022, 9:25am UTC](https://discuss.elastic.co/t/what-to-do-with-query-inside-filter-query/296246/2 "2022-02-04T09:25:03Z")

</div>

put "`query_string`" object out from `query` object and put it directly in the filter array just as other term, range queries.

---

<div class="post-metadata">

**Author:** ![JunyaT](https://avatars.discourse-cdn.com/v4/letter/j/ba8739/32.png) [@JunyaT](https://discuss.elastic.co/u/JunyaT)\
**Post date:** [February 4, 2022, 10:13am UTC](https://discuss.elastic.co/t/what-to-do-with-query-inside-filter-query/296246/3 "2022-02-04T10:13:10Z")

</div>

@Tomo_M  
Thanks for the help! Let me see if this works.

---

<div class="post-metadata">

**Author:** ![JunyaT](https://avatars.discourse-cdn.com/v4/letter/j/ba8739/32.png) [@JunyaT](https://discuss.elastic.co/u/JunyaT)\
**Post date:** [February 5, 2022, 4:26am UTC](https://discuss.elastic.co/t/what-to-do-with-query-inside-filter-query/296246/4 "2022-02-05T04:26:24Z")

</div>

@Tomo_M  
ありがとうございます！

it worked. 💯

---

<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 5, 2022, 4:26am UTC](https://discuss.elastic.co/t/what-to-do-with-query-inside-filter-query/296246/5 "2022-03-05T04:26:54Z")

</div>

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