# Filtering by Number of Nested Objects

**URL:** <https://discuss.elastic.co/t/filtering-by-number-of-nested-objects/72375>\
**Category:** Elasticsearch\
**Created:** [January 21, 2017, 8:29pm UTC](https://discuss.elastic.co/t/filtering-by-number-of-nested-objects/72375 "2017-01-21T20:29:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![krmathieu](https://avatars.discourse-cdn.com/v4/letter/k/b5e925/32.png) [@krmathieu](https://discuss.elastic.co/u/krmathieu)\
**Post date:** [January 21, 2017, 8:29pm UTC](https://discuss.elastic.co/t/filtering-by-number-of-nested-objects/72375/1 "2017-01-21T20:29:36Z")

</div>

I am trying to find all documents in an index where at least one nested object (segments.entities) has more than one value and here is the relevant snippet from the mapping:

```
..........
"segments" : {
            "type" : "nested",
            "include_in_parent" : true,
            "properties" : {
              "anchor" : {
                "type" : "nested",
                "include_in_parent" : true,
                "properties" : {
                  "section" : {
                    "type" : "string"
                  },
                  "start" : {
                    "type" : "long"
                  },
                  "stop" : {
                    "type" : "long"
                  },
                  "text" : {
                    "type" : "string",
                    "index" : "not_analyzed"
                  }
                }
              },
              "entities" : {
                "type" : "nested",
                "include_in_parent" : true,
                "properties" : {
                  "anchorText" : {
                    "type" : "string",
                    "index" : "not_analyzed"
                  },.........
```

And here is my query:

```
{
	"size": 100,
	"fields": ["document.content", "segments.entities.anchorText"],
    	"filter": {
		"script": {
			"script": "doc['segments.entities'].values.length > 1"
		}
	}
}
```

Running this query results in an error stating "No field found for [segments.entities] in mapping with types [doc]". If I remove the filter bit from the query above I get the expected results back so I know that the segments.entities path is valid. The field segments.entities should be an array and I am trying to find all documents where at least one segments.entities array has more than one element. Does anybody see what I am doing wrong? Any help would be appreciated. Thanks.

Kevin

---

<div class="post-metadata">

**Author:** ![GyllingSW](https://avatars.discourse-cdn.com/v4/letter/g/6f9a4e/32.png) [@GyllingSW](https://discuss.elastic.co/u/GyllingSW)\
**Post date:** [January 23, 2017, 11:56am UTC](https://discuss.elastic.co/t/filtering-by-number-of-nested-objects/72375/2 "2017-01-23T11:56:00Z")

</div>

Try to see if this issue would do the trick:

> [@Scripted filter on nested field](https://discuss.elastic.co/t/scripted-filter-on-nested-field/27162):
>
> I have to filter documents based on scripted value of nested element. My data structure looks like this: { "product-group-name" : "group1" "products": [{ "name" : "product1" "price" : 10 },{ "name" : "product2" "price" : 20 }] } "products" is nested field. Now I need to find product groups (top level documents) where one of the nested "price" fields satisfy condition I define with the script (I have complex script expression that can not be replaced with simple nested q…

---

<div class="post-metadata">

**Author:** ![krmathieu](https://avatars.discourse-cdn.com/v4/letter/k/b5e925/32.png) [@krmathieu](https://discuss.elastic.co/u/krmathieu)\
**Post date:** [January 23, 2017, 6:03pm UTC](https://discuss.elastic.co/t/filtering-by-number-of-nested-objects/72375/4 "2017-01-23T18:03:16Z")

</div>

I just tried updating my query and still see the following error:

No field found for [segments.entities] in mapping with types [doc]

Any idea what this means? Also, below is my updated query. Any additional thoughts that you may have would be appreciated. Thanks.

Kevin

{  
"size": 100,  
"filter": {  
"nested": {  
"path": "segments.entities",  
"filter": {  
"script": {  
"script": "if (doc['segments.entities'].value \> 1) {return (true)}"  
}  
}  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![GyllingSW](https://avatars.discourse-cdn.com/v4/letter/g/6f9a4e/32.png) [@GyllingSW](https://discuss.elastic.co/u/GyllingSW)\
**Post date:** [January 23, 2017, 6:34pm UTC](https://discuss.elastic.co/t/filtering-by-number-of-nested-objects/72375/5 "2017-01-23T18:34:59Z")

</div>

Change the Path value to:

```
"path" : "segments"

```

The path value is to the parent level compared to the actual query. If this doesn't help please post a snippet of your actual mapping

---

<div class="post-metadata">

**Author:** ![krmathieu](https://avatars.discourse-cdn.com/v4/letter/k/b5e925/32.png) [@krmathieu](https://discuss.elastic.co/u/krmathieu)\
**Post date:** [January 23, 2017, 7:49pm UTC](https://discuss.elastic.co/t/filtering-by-number-of-nested-objects/72375/6 "2017-01-23T19:49:01Z")

</div>

I changed my path to just segments and am still seeing the same issue when using Advanced REST client and the relevant mapping snippet is below. I did not create the mapping so I am not sure why the include\_in\_parent bit is used for segments when it is on the root level of the document. Any ideas why I am still seeing the problem?

Kevin

```
..........
"segments" : {
            "type" : "nested",
            "include_in_parent" : true,
            "properties" : {
              "anchor" : {
                "type" : "nested",
                "include_in_parent" : true,
                "properties" : {
                  "section" : {
                    "type" : "string"
                  },
                  "start" : {
                    "type" : "long"
                  },
                  "stop" : {
                    "type" : "long"
                  },
                  "text" : {
                    "type" : "string",
                    "index" : "not_analyzed"
                  }
                }
              },
              "entities" : {
                "type" : "nested",
                "include_in_parent" : true,
                "properties" : {
                  "anchorText" : {
                    "type" : "string",
                    "index" : "not_analyzed"
                  },
```

..........

---

<div class="post-metadata">

**Author:** ![krmathieu](https://avatars.discourse-cdn.com/v4/letter/k/b5e925/32.png) [@krmathieu](https://discuss.elastic.co/u/krmathieu)\
**Post date:** [January 23, 2017, 7:59pm UTC](https://discuss.elastic.co/t/filtering-by-number-of-nested-objects/72375/7 "2017-01-23T19:59:11Z")

</div>

For giggles I created a Bash script to use curl to execute the query from a Linux box and see this error vice the one I am seeing when using Advanced REST client:

"type" : "missing\_property\_exception",  
"reason" : "No such property: segments for class: 88a4e7b9685857189da450004518aa361a991a47"

Not sure if the info here is related to these particular errors or not:

[https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-security.html#modules-scripting-security](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-security.html#modules-scripting-security)

Thoughts???

---

<div class="post-metadata">

**Author:** ![krmathieu](https://avatars.discourse-cdn.com/v4/letter/k/b5e925/32.png) [@krmathieu](https://discuss.elastic.co/u/krmathieu)\
**Post date:** [January 23, 2017, 8:32pm UTC](https://discuss.elastic.co/t/filtering-by-number-of-nested-objects/72375/8 "2017-01-23T20:32:30Z")

</div>

Actually, the issue with the Bash script was unescaped quotes. Once I fixed that I get the same error as I see in Advanced REST client. Sorry for the confusion.

Kevin

---

<div class="post-metadata">

**Author:** ![krmathieu](https://avatars.discourse-cdn.com/v4/letter/k/b5e925/32.png) [@krmathieu](https://discuss.elastic.co/u/krmathieu)\
**Post date:** [January 26, 2017, 7:12pm UTC](https://discuss.elastic.co/t/filtering-by-number-of-nested-objects/72375/9 "2017-01-26T19:12:21Z")

</div>

Update to this issue. Instead of using doc I tried using the following query against \_source:

```
{
	"size": 100,
	"fields": ["document.content", "segments.entities.anchorText"],
    	"filter": {
		"script": {
			"script": "_source.segments.entities.values.flatten().length > 1"
		}
	}
}
```

Now the error I am seeing is this even though I am "flattening" the array:

Cannot compare java.util.ArrayList with value '[]' and java.lang.Integer with value '1'

I also tried this query and it just hangs, but does not throw any errors:

```
{
	"size": 100,
	"fields": ["document.content", "segments.entities.anchorText"],
    	"filter": {
		"script": {
			"script": "_source.segments.entities.size() > 1"
		}
	}
}
```

Any ideas on what is going on here? Any help getting this sorted out would be appreciated. Thanks.

Kevin

---

<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 23, 2017, 7:12pm UTC](https://discuss.elastic.co/t/filtering-by-number-of-nested-objects/72375/10 "2017-02-23T19:12:23Z")

</div>

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