# How to search query in main and nested doc along with some filters in elasticsearch

**URL:** <https://discuss.elastic.co/t/how-to-search-query-in-main-and-nested-doc-along-with-some-filters-in-elasticsearch/161032>\
**Category:** Elasticsearch\
**Created:** [December 16, 2018, 1:40pm UTC](https://discuss.elastic.co/t/how-to-search-query-in-main-and-nested-doc-along-with-some-filters-in-elasticsearch/161032 "2018-12-16T13:40:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amit\_Gupta1](https://avatars.discourse-cdn.com/v4/letter/a/2acd7d/32.png) [@Amit\_Gupta1](https://discuss.elastic.co/u/Amit_Gupta1)\
**Post date:** [December 16, 2018, 1:40pm UTC](https://discuss.elastic.co/t/how-to-search-query-in-main-and-nested-doc-along-with-some-filters-in-elasticsearch/161032/1 "2018-12-16T13:40:00Z")

</div>

I have the following mapping for colleges in elasticsearch:

```
{
  "properties": {
    "name": {
      "type": "text"
    },
    "city": {
      "type": "keyword"
    },
    "programs": {
      "type": "nested",
      "properties": {
        "award": {
          "type": "text"
        },
        "level": {
          "type": "keyword"
        }
      }
    }
  }
}

```

I want to run search on this index as:

- search query on `name` and `programs.award`
- filter query on `city` and `programs.level`

We have a filter box for `city`, `level` and an open text field in the UI.

I am using the following query right now:

```
{
	"from": 0,
	"size": 10,
	"query": {
		"bool": {
			"must": [
				{
					"terms": {
						"city": [
							"Some City"
						]
					}
				},
				{
					"nested": {
						"path": "programs",
						"inner_hits": {
							"size": 10
						},
						"query": {
							"bool": {
								"must": [
									{
										"terms": {
											"programs.level": ["Under Graduate"]
										}
									}
								],
								"should": [
									{
										"simple_query_string": {
											"query": "engineering~",
											"fields": ["programs.award"]
										}
									}
								]
							}
						}
					}
				}
			],
			"should": [
				{
					"simple_query_string": {
						"query": "engineering",
						"fields": ["name"]
					}
				}
			]
		}
	}
}

```

For `terms` filter it's working fine but for `simple_query_string` it's not working as expected.

I want an `OR` query search between `name` and `programs.award`.

I am not sure what I am doing wrong here if anyone can help.

Note: If it matches the `name` property then it should return all `programs` in paginated form.

---

<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:** [January 13, 2019, 1:40pm UTC](https://discuss.elastic.co/t/how-to-search-query-in-main-and-nested-doc-along-with-some-filters-in-elasticsearch/161032/2 "2019-01-13T13:40:04Z")

</div>

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