# How to wrap nested query with filter by \_index field in ElasticSearch?

**URL:** <https://discuss.elastic.co/t/how-to-wrap-nested-query-with-filter-by-index-field-in-elasticsearch/113095>\
**Category:** Elasticsearch\
**Created:** [December 24, 2017, 5:14am UTC](https://discuss.elastic.co/t/how-to-wrap-nested-query-with-filter-by-index-field-in-elasticsearch/113095 "2017-12-24T05:14:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SimpleCode](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@SimpleCode](https://discuss.elastic.co/u/SimpleCode)\
**Post date:** [December 24, 2017, 5:14am UTC](https://discuss.elastic.co/t/how-to-wrap-nested-query-with-filter-by-index-field-in-elasticsearch/113095/1 "2017-12-24T05:14:42Z")

</div>

I have the following query  
GET product,account/producttype,accounttype/\_search  
{  
"query": {  
"bool": {  
"should": [  
{  
"bool": {  
"must": [  
{  
"nested": {  
"query": {  
"bool": {  
"should": [  
{  
"match": {  
"keywords.keyword": {  
"query": "search query"  
}  
}  
},  
{  
"term": {  
"keywords.keyword.keyword": {  
"value": "search query"  
}  
}  
}  
]  
}  
},  
"path": "keywords"  
}  
}  
],  
"filter": [  
{  
"term": {  
"\_type": {  
"value": "producttype"  
}  
}  
}  
]  
}  
},  
{  
"bool": {  
"must": [  
{  
"multi\_match": {  
"query": "search query",  
"operator": "or",  
"fields": [  
"accountName^1.5",  
"description^0.8"  
]  
}  
}  
],  
"filter": [  
{  
"term": {  
"\_type": {  
"value": "accounttype"  
}  
}  
}  
]  
}  
}  
]  
}  
}  
}

When I run the above query I get this exception:

> "index": "account", "caused\_by": { "type": "illegal\_state\_exception", "reason": "[nested] failed to find nested object under path [keywords]" }

Inside index account there is no nested object called Keywords.

In the past Indices query was used to solve this issue, but now it's deprecated.

Now as the query above shows: I am using filtering by \_index but it still gives me that error.

So, could you please guide me to get rid of that exception and get my query working?

PS:

I am using elasticsearch v5.1.1

---

<div class="post-metadata">

**Author:** ![luiz.santos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/luiz.santos/32/24664_2.png) [@luiz.santos](https://discuss.elastic.co/u/luiz.santos)\
**Post date:** [December 26, 2017, 5:51pm UTC](https://discuss.elastic.co/t/how-to-wrap-nested-query-with-filter-by-index-field-in-elasticsearch/113095/2 "2017-12-26T17:51:11Z")

</div>

Hi @SimpleCode,

Instead of filtering by index "product" in the query you can search only in index product by specifying only it in the URL:

```
GET product/producttype/_search
{
   ...
}  

```

Hope it helps.

Cheers,  
LG

---

<div class="post-metadata">

**Author:** ![SimpleCode](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@SimpleCode](https://discuss.elastic.co/u/SimpleCode)\
**Post date:** [December 26, 2017, 6:23pm UTC](https://discuss.elastic.co/t/how-to-wrap-nested-query-with-filter-by-index-field-in-elasticsearch/113095/3 "2017-12-26T18:23:37Z")

</div>

@luiz.santos

Thanks for reply

I have modified my question, my query will hit multiple indices. So I can't specify only one index I need to search inside two indices in the same query

---

<div class="post-metadata">

**Author:** ![MGerasimchuk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mgerasimchuk/32/26788_2.png) [@MGerasimchuk](https://discuss.elastic.co/u/MGerasimchuk)\
**Post date:** [January 22, 2018, 7:40am UTC](https://discuss.elastic.co/t/how-to-wrap-nested-query-with-filter-by-index-field-in-elasticsearch/113095/4 "2018-01-22T07:40:21Z")

</div>

@luiz.santos Hello, I'm also interested in the answer to this question.

---

<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:** [February 19, 2018, 7:40am UTC](https://discuss.elastic.co/t/how-to-wrap-nested-query-with-filter-by-index-field-in-elasticsearch/113095/5 "2018-02-19T07:40:49Z")

</div>

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