# Query nested objects gives incorrect results

**URL:** https://discuss.elastic.co/t/query-nested-objects-gives-incorrect-results/14258
**Category:** Elasticsearch
**Created:** [November 5, 2013, 11:23am UTC](https://discuss.elastic.co/t/query-nested-objects-gives-incorrect-results/14258 "2013-11-05T11:23:04Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Ben\_Ryan](https://avatars.discourse-cdn.com/v4/letter/b/dfb087/32.png) [@Ben\_Ryan](https://discuss.elastic.co/u/Ben_Ryan)
#### Post date: [November 5, 2013, 11:23am UTC](https://discuss.elastic.co/t/query-nested-objects-gives-incorrect-results/14258/1 "2013-11-05T11:23:04Z")

</div>

Hi,  
I have the following mapping for an index:

{

```
"jorumindex": {
    "oerdocument": {
        "dynamic_templates": [
            {
                "default": {
                    "mapping": {
                        "type": "multi_field",
                        "fields": {
                            "exact": {
                                "index": "not_analyzed",
                                "store": "yes",
                                "type": "{dynamic_type}"
                            },
                            "{name}": {
                                "index": "analyzed",
                                "store": "no",
                                "analyzer": "standard",
                                "type": "{dynamic_type}"
                            }
                        }
                    },
                    "match": "*",
                    "match_mapping_type": "string"
                }
            }
        ],
        "properties": {
            "author": {
                "type": "multi_field",
                "fields": {
                    "author": {
                        "type": "string",
                        "analyzer": "standard"
                    },
                    "exact": {
                        "type": "string",
                        "index": "not_analyzed",
                        "store": true,
                        "omit_norms": true,
                        "index_options": "docs",
                        "include_in_all": false
                    }
                }
            },
            "statistics": {
                "type": "nested",
                "include_in_parent": true,
                "include_in_root": true,
                "properties": {
                    "event_type": {
                        "type": "string",
                        "index": "not_analyzed",
                        "omit_norms": true,
                        "index_options": "docs",
                        "include_in_all": false
                    },
                   
                    "time": {
                        "type": "date",
                        "format": "dateOptionalTime",
                        "include_in_all": false
                    }
                }
            },
            "subject": {
                "type": "multi_field",
                "fields": {
                    "subject": {
                        "type": "string",
                        "analyzer": "standard"
                    },
                    "exact": {
                        "type": "string",
                        "index": "not_analyzed",
                        "store": true,
                        "omit_norms": true,
                        "index_options": "docs",
                        "include_in_all": false
                    }
                }
            }
        }
    }
}

```

}

I am attempting to get a count from the nested statistics objects for a  
time period with the following query:

q=statistics.event\_type:"download" AND  
statistics.time:[2013-09-04T00:00:00.000Z TO 2013-10-30T23:59:00.000Z]

This appears to return the count of documents that meet the criteria and  
not the total count (I have taken this query straight from our SOLR  
application).

Can anybody point me in the right direction to get the total count?

Regards,  
Ben

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)
#### Post date: [November 5, 2013, 5:16pm UTC](https://discuss.elastic.co/t/query-nested-objects-gives-incorrect-results/14258/2 "2013-11-05T17:16:51Z")

</div>

Are you saying you are getting a count of the top-level documents, but you  
want to get a count of the nested documents? Hopefully someone will correct  
me if I am wrong, but I do not think that is possible. The queries execute  
a join query in the background and only the top level documents are  
returned. You can execute a facet on a nested field to get the correct  
count of nested documents.

Cheers,

Ivan

On Tue, Nov 5, 2013 at 3:23 AM, Ben Ryan [argaric@gmail.com](mailto:argaric@gmail.com) wrote:

> Hi,  
> I have the following mapping for an index:
> 
> {
> 
> ```
> "jorumindex": {
> "oerdocument": {
> "dynamic_templates": [
> {
> "default": {
> "mapping": {
> "type": "multi_field",
> "fields": {
> "exact": {
> "index": "not_analyzed",
> "store": "yes",
> "type": "{dynamic_type}"
> },
> "{name}": {
> "index": "analyzed",
> "store": "no",
> "analyzer": "standard",
> "type": "{dynamic_type}"
> }
> }
> },
> "match": "*",
> "match_mapping_type": "string"
> }
> }
> ],
> "properties": {
> "author": {
> "type": "multi_field",
> "fields": {
> "author": {
> "type": "string",
> "analyzer": "standard"
> },
> "exact": {
> "type": "string",
> "index": "not_analyzed",
> "store": true,
> "omit_norms": true,
> "index_options": "docs",
> "include_in_all": false
> }
> }
> },
> "statistics": {
> "type": "nested",
> "include_in_parent": true,
> "include_in_root": true,
> "properties": {
> "event_type": {
> "type": "string",
> "index": "not_analyzed",
> "omit_norms": true,
> "index_options": "docs",
> "include_in_all": false
> },
> 
> "time": {
> "type": "date",
> "format": "dateOptionalTime",
> "include_in_all": false
> }
> }
> },
> "subject": {
> "type": "multi_field",
> "fields": {
> "subject": {
> "type": "string",
> "analyzer": "standard"
> },
> "exact": {
> "type": "string",
> "index": "not_analyzed",
> "store": true,
> "omit_norms": true,
> "index_options": "docs",
> "include_in_all": false
> }
> }
> }
> }
> }
> }
> 
> ```
> 
> }
> 
> I am attempting to get a count from the nested statistics objects for a  
> time period with the following query:
> 
> q=statistics.event\_type:"download" AND  
> statistics.time:[2013-09-04T00:00:00.000Z TO 2013-10-30T23:59:00.000Z]
> 
> This appears to return the count of documents that meet the criteria and  
> not the total count (I have taken this query straight from our SOLR  
> application).
> 
> Can anybody point me in the right direction to get the total count?
> 
> Regards,  
> Ben
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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: [July 6, 2017, 2:08am UTC](https://discuss.elastic.co/t/query-nested-objects-gives-incorrect-results/14258/3 "2017-07-06T02:08:44Z")

</div>


