# Get count of parent documents only

**URL:** https://discuss.elastic.co/t/get-count-of-parent-documents-only/92494
**Category:** Elasticsearch
**Created:** [July 10, 2017, 3:02pm UTC](https://discuss.elastic.co/t/get-count-of-parent-documents-only/92494 "2017-07-10T15:02:08Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Matt\_Zalen](https://avatars.discourse-cdn.com/v4/letter/m/9d8465/32.png) [@Matt\_Zalen](https://discuss.elastic.co/u/Matt_Zalen)
#### Post date: [July 10, 2017, 3:02pm UTC](https://discuss.elastic.co/t/get-count-of-parent-documents-only/92494/1 "2017-07-10T15:02:08Z")

</div>

Hi all,

I have a document which has some nested documents. When I do something like the following, the count that comes back includes the nested document:

GET loandetails2/loandetail/\_count  
{  
"query": {  
"bool": {  
"should": [  
{  
"match": {  
"visibility": {  
"query": "3"  
}  
}  
}  
]  
}  
}  
}

The above query is just a simplified version of what I'm actually doing. Sometimes I'm really trying to get a count based on fields in the parent document, and fields in the nested documents. But I only want the count that returns to be the number of parent documents.

How do I do that?

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [July 11, 2017, 9:38am UTC](https://discuss.elastic.co/t/get-count-of-parent-documents-only/92494/2 "2017-07-11T09:38:00Z")

</div>

hey

you mixed a bit of different concepts in elasticsearch terminology here. `nested` is something different than `parent/child` than `inner objects`. Can you maybe explain your data model, and also provide the real query, plus sample documents, so that other can reproduce this?

--Alex

---

<div class="post-metadata">

### Author: ![Matt\_Zalen](https://avatars.discourse-cdn.com/v4/letter/m/9d8465/32.png) [@Matt\_Zalen](https://discuss.elastic.co/u/Matt_Zalen)
#### Post date: [July 11, 2017, 3:52pm UTC](https://discuss.elastic.co/t/get-count-of-parent-documents-only/92494/3 "2017-07-11T15:52:06Z")

</div>

You're right, I'm not entirely sure the difference between those two concepts. I've simplified the data model for your purposes, and I've provided the mapping below:

```
  "loandetails": {
    "mappings": {
      "loandetail": {
        "properties": {
          "administrativeAgent": {
            "properties": {
              "id": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "name": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              }
            }
          },
          "properties": {
            "type": "nested",
            "properties": {
              "areaTotalsTotal": {
                "type": "long"
              },
              "markets": {
                "properties": {
                  "id": {
                    "type": "text",
                    "fields": {
                      "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                      }
                    }
                  },
                  "name": {
                    "type": "text",
                    "fields": {
                      "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                      }
                    }
                  }
                }
              },
              "municipality": {
                "properties": {
                  "id": {
                    "type": "text",
                    "fields": {
                      "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                      }
                    }
                  },
                  "name": {
                    "type": "text",
                    "fields": {
                      "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                      }
                    }
                  }
                }
              },
              "name": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "propertySubType": {
                "type": "keyword"
              },
              "propertyType": {
                "type": "keyword"
              }
            }
          },
          "purpose": {
            "type": "keyword"
          },
          "structure": {
            "type": "keyword"
          },
         "visibility": {
            "type": "integer"
          }
        }
      }
    }
  }
}

```

And a real query would be a simple query like the following:

```
GET loandetails/loandetail/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "visibility": 1
          }
        }
      ]
    }
  }
}

```

Is this sufficient, or do you really need the sample documents. If you need that, I'd rather not put them in this forum.

---

<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: [August 8, 2017, 3:52pm UTC](https://discuss.elastic.co/t/get-count-of-parent-documents-only/92494/4 "2017-08-08T15:52:17Z")

</div>

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