# Nested Filter Problem

**URL:** https://discuss.elastic.co/t/nested-filter-problem/116351
**Category:** Elasticsearch
**Created:** [January 20, 2018, 7:16pm UTC](https://discuss.elastic.co/t/nested-filter-problem/116351 "2018-01-20T19:16:01Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![awakevzla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/awakevzla/32/26901_2.png) [@awakevzla](https://discuss.elastic.co/u/awakevzla)
#### Post date: [January 20, 2018, 7:16pm UTC](https://discuss.elastic.co/t/nested-filter-problem/116351/1 "2018-01-20T19:16:02Z")

</div>

Hi everyone, i am facing a problem implementing nested query on my elasticsearch 5.6 cluster.

I've this mapping:

```
    {
       "my_test": {
         "mappings": {
           "co_product": {
             "properties": {
               "photos": {
                 "type": "keyword"
               },
               "specs": {
                 "type": "nested",
                 "properties": {
                   "value": {
                     "type": "keyword"
                   }
                 }
               }
             }
           }
         }
       }
    }

```

And this data:

```
[
    {
         "photos": [
        "http://foto2.png",
      ],
      "specs": [
        {
          "value": "Tornillo"
        },
        {
          "value": "Dorado"
        }
      ]
    },{
         "photos": [
        "http://foto2.png",
      ],
      "specs": [
        {
          "value": "Tornillo"
        },
        {
          "value": "Plata"
        }
      ]
    }
]

```

I need a query to get a exactly the items that match with all the filters that i send, i tried like this:

```
"query": {
    "nested": {
      "path": "specs",
      "query": {
        "bool": {
          "must": [
            {"match": {"specs.value": "Tornillo"}},
            {"match": {"specs.value": "Dorado"}}
          ]
        }
      }
    }
  }

```

But that brings me back an empty response.

I tried this way too:

```
"query": {
    "nested": {
      "path": "specs",
      "query": {
        "bool": {
          "should": [
            {"match": {"specs.value": "Tornillo"}},
            {"match": {"specs.value": "Dorado"}}
          ]
        }
      }
    }
  }

```

But that brings me back all the items, and i just want the item that match exaclty.

Please i've headache trying....

---

<div class="post-metadata">

### Author: ![Arvind\_Rao](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/arvind_rao/32/26758_2.png) [@Arvind\_Rao](https://discuss.elastic.co/u/Arvind_Rao)
#### Post date: [January 22, 2018, 7:30am UTC](https://discuss.elastic.co/t/nested-filter-problem/116351/2 "2018-01-22T07:30:42Z")

</div>

See my response to a [similar question](https://discuss.elastic.co/t/querying-nested-datatype/116408)

In the context of queries on nested structures, an element in the "specs" array can either match "Tornillo" or "Dorado" but not both.

Try what I suggested in the other question, i.e. use the inner\_hits object to determine if the count of matched elements is 2. Only the records where it is 2 are valid results.

```auto
{
	"query": {
		"nested": {
			"path": "specs",
			"query": {
				"bool": {
					"filter": [{
							"terms": {
								"specs.value": [
									"Tornillo", "Dorado"
								]
							}
						}
					]
				}
			},
			"inner_hits": {}
		}
	}
}

```

---

<div class="post-metadata">

### Author: ![awakevzla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/awakevzla/32/26901_2.png) [@awakevzla](https://discuss.elastic.co/u/awakevzla)
#### Post date: [January 22, 2018, 2:12pm UTC](https://discuss.elastic.co/t/nested-filter-problem/116351/3 "2018-01-22T14:12:22Z")

</div>

That do not work for me, because i need to retorn the exactly number of items that match with the filter, i need to response to a web page that will paginate the results.

Its there a some way to do that? Changing the mapping too i don't know, im noob in this. (sorry my bad english)

---

<div class="post-metadata">

### Author: ![animageofmine](https://avatars.discourse-cdn.com/v4/letter/a/7feea3/32.png) [@animageofmine](https://discuss.elastic.co/u/animageofmine)
#### Post date: [January 22, 2018, 4:16pm UTC](https://discuss.elastic.co/t/nested-filter-problem/116351/4 "2018-01-22T16:16:33Z")

</div>

> [@awakevzla](#):
>
> esponse to a web page that will paginate the results.

Can you share the expected answer? Have you looked at the object type if that fits your requirement?

---

<div class="post-metadata">

### Author: ![awakevzla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/awakevzla/32/26901_2.png) [@awakevzla](https://discuss.elastic.co/u/awakevzla)
#### Post date: [January 22, 2018, 4:23pm UTC](https://discuss.elastic.co/t/nested-filter-problem/116351/5 "2018-01-22T16:23:43Z")

</div>

Thanks a lot for your answers, i appreciate it. @animageofmine & @Arvind_Rao

I need a response like that:

```
{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "my_test",
        "_type": "co_product",
        "_id": "AWEU4KdEcjKtthiRBLYL",
        "_score": 1,
        "_source": {
          "photos": [
            "http://foto2.png",
            "http://foto1.png",
            "http://foto.png"
          ],
          "specs": [
            {
              "value": "Tornillo"
            },
            {
              "value": "Dorado"
            }
          ]
        }
      }
    ]
  }
}

```

Because i need to sent a equivalente query like that:

```
WHERE specs.value= "Tornillo" 
AND specs.value = "Dorado"
```

---

<div class="post-metadata">

### Author: ![Arvind\_Rao](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/arvind_rao/32/26758_2.png) [@Arvind\_Rao](https://discuss.elastic.co/u/Arvind_Rao)
#### Post date: [January 24, 2018, 7:46am UTC](https://discuss.elastic.co/t/nested-filter-problem/116351/6 "2018-01-24T07:46:13Z")

</div>

Just responded on the other thread. Found solution on [StackOverflow](https://stackoverflow.com/a/24540656)

```auto
{
	"query": {
		"bool": {
			"must": [{
					"nested": {
						"path": "specs",
						"query": {
							"bool": {
								"must": [{
										"term": {
											"specs.value": "Tornillo"
										}

									}
								]
							}
						}
					}
				},
				{
					"nested": {
						"path": "specs",
						"query": {
							"bool": {
								"must": [{
										"term": {
											"specs.value": "Dorado"
										}

									}
								]
							}
						}
					}
				}
			]
		}
	}
}

```

---

<div class="post-metadata">

### Author: ![awakevzla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/awakevzla/32/26901_2.png) [@awakevzla](https://discuss.elastic.co/u/awakevzla)
#### Post date: [January 24, 2018, 2:54pm UTC](https://discuss.elastic.co/t/nested-filter-problem/116351/7 "2018-01-24T14:54:49Z")

</div>

OMG, i was searching all night with no solution. Thank u very much. Solved!

---

<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 21, 2018, 2:55pm UTC](https://discuss.elastic.co/t/nested-filter-problem/116351/8 "2018-02-21T14:55:08Z")

</div>

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