# How can I use query\_string to match both nested and non-nested fields at the same time?

**URL:** <https://discuss.elastic.co/t/how-can-i-use-query-string-to-match-both-nested-and-non-nested-fields-at-the-same-time/288023>\
**Category:** Elasticsearch\
**Created:** [October 29, 2021, 4:43pm UTC](https://discuss.elastic.co/t/how-can-i-use-query-string-to-match-both-nested-and-non-nested-fields-at-the-same-time/288023 "2021-10-29T16:43:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Max\_Carey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/max_carey/32/96537_2.png) [@Max\_Carey](https://discuss.elastic.co/u/Max_Carey)\
**Post date:** [October 29, 2021, 4:43pm UTC](https://discuss.elastic.co/t/how-can-i-use-query-string-to-match-both-nested-and-non-nested-fields-at-the-same-time/288023/1 "2021-10-29T16:43:43Z")

</div>

I have an index with a mapping something like this:

```auto
"email" : {
  "type" : "nested",
  "properties" : {
    "from" : {
      "type" : "text",
      "analyzer" : "lowercase_keyword",
      "fielddata" : true
    },
    "subject" : {
      "type" : "text",
      "analyzer" : "lowercase_keyword",
      "fielddata" : true
    },
    "to" : {
      "type" : "text",
      "analyzer" : "lowercase_keyword",
      "fielddata" : true
    }
  }
},
"textExact" : {
  "type" : "text",
  "analyzer" : "lowercase_standard",
  "fielddata" : true
}

```

I want to use query\_string to search for matches in both the nested and the non-nested field at the same time, e.g.

```auto
email.to:foo@example.com AND textExact:bar

```

But I can't figure out how to write a query that will search both fields at once. The following doesn't work, because [query\_string searches do not return nested documents](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#query-string-nested):

```auto
"query": {
  "query_string": {
    "fields": [
      "textExact",
      "email.to"
    ],
    "query": "email.to:foo@example.com AND textExact:bar"
  }
}

```

I can write a separate nested query, but that will only search against nested fields. Is there any way I can use query\_string to match both nested and non-nested fields at the same time?

---

<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:** [November 26, 2021, 4:43pm UTC](https://discuss.elastic.co/t/how-can-i-use-query-string-to-match-both-nested-and-non-nested-fields-at-the-same-time/288023/2 "2021-11-26T16:43:44Z")

</div>

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