# How to facet on parent property - what's wrong with this code?

**URL:** <https://discuss.elastic.co/t/how-to-facet-on-parent-property-whats-wrong-with-this-code/67172>\
**Category:** Elasticsearch\
**Created:** [November 25, 2016, 10:11am UTC](https://discuss.elastic.co/t/how-to-facet-on-parent-property-whats-wrong-with-this-code/67172 "2016-11-25T10:11:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![alfonso.mateos](https://avatars.discourse-cdn.com/v4/letter/a/bcef8e/32.png) [@alfonso.mateos](https://discuss.elastic.co/u/alfonso.mateos)\
**Post date:** [November 25, 2016, 10:11am UTC](https://discuss.elastic.co/t/how-to-facet-on-parent-property-whats-wrong-with-this-code/67172/1 "2016-11-25T10:11:29Z")

</div>

I've got this mapping in a parent-child relation between myParent and myChild:

```
{
"myParent": {
	"properties": {
		"parentCode": {"type": "string"},
		"parentDescriptor": {"type": "string"}
	}
},
"myChild": {
  "_parent": {
   "type": "myParent"
  },
	"properties": {
		"childCode": {"type": "string"},
		"childDescriptor": {"type": "string"}
	}
}

```

}  
}

I use this query to get facets both on child and parent fields, against the url [http://localhost:9200/myIndex/myChild](http://localhost:9200/myIndex/myChild)

```
{
  "query": {
    "has_parent": {
      "type": "myChild", 
      "inner_hits": {},
      "query": {
        "match_all": {
        }
      }
    }
  },
    "facets" : {
        "childCode" : {
            "terms" : {
                "field" : "childCode",
                "size" : 2147483647
            }
        },
	"parentCode": {
          "terms": {
              "field": "parentCode",
                "size" : 2147483647
          }
	}
    }
}

```

I get correct results from the query (every child includes its parent inside inner\_hits), but the facet for parentCode is always ZERO. I've tried to name the terms.field for that facet as myParent.parentCode, inner\_hits.myParent.parentCode but I cannot get that facet.

Can you tell me how can I get it?  
Thanks in advance.

---

<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:** [December 23, 2016, 10:11am UTC](https://discuss.elastic.co/t/how-to-facet-on-parent-property-whats-wrong-with-this-code/67172/2 "2016-12-23T10:11:42Z")

</div>

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