# Accessing parent fields from custom facet over child docs (ES 0.20)

**URL:** <https://discuss.elastic.co/t/accessing-parent-fields-from-custom-facet-over-child-docs-es-0-20/13345>\
**Category:** Elasticsearch\
**Created:** [August 27, 2013, 1:39pm UTC](https://discuss.elastic.co/t/accessing-parent-fields-from-custom-facet-over-child-docs-es-0-20/13345 "2013-08-27T13:39:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tikitu\_de\_Jager](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tikitu_de_jager/32/1258_2.png) [@Tikitu\_de\_Jager](https://discuss.elastic.co/u/Tikitu_de_Jager)\
**Post date:** [August 27, 2013, 1:39pm UTC](https://discuss.elastic.co/t/accessing-parent-fields-from-custom-facet-over-child-docs-es-0-20/13345/1 "2013-08-27T13:39:27Z")

</div>

Hi folks,

I have most of a parent/child-doc solution for a problem I'm working on,  
but I ran into a hitch: from inside a facet that iterates over the _child_ docs  
I need to access the value of a _parent_ doc field. I have (or rather, I  
presume I can get) the parent doc ID (from the \_parent field of the child  
doc) but that's an "external" ID, not the node-internal ID that I need to  
load the field value from the field cache. (I'm using default routing so  
the parent doc is definitely in the same shard as the children.)

More concretely, here's what I have in the FacetCollector so far:

protected void doSetNextReader(IndexReader reader, int docBase) throws  
IOException {  
/\* not sure this will work, but I presume there's some way to get the  
\_parent value, which in my case is a long _/  
parentFieldData = (LongFieldData)  
fieldDataCache.cache(FieldDataType.DefaultTypes.LONG, reader, "\_parent");  
parentSpringinessFieldData = (FloatFieldData)  
fieldDataCache.cache(FieldDataType.DefaultTypes.FLOAT, "springiness");  
/_ ... \*/

protected void doCollect(int doc) throws IOException {  
long parentID = parentFieldData.value(doc); // or whatever the correct  
equivalent here is  
// here's the problem:  
parentSpringiness = parentSpringinessFieldData.value(parentID) // type  
error: expected int (node-internal ID), got long (external ID)

Any suggestions? (I can't upgrade to 0.90 yet but would be interested to  
hear if that would help.)

Cheers,  
Tikitu

--  
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:** ![Tikitu\_de\_Jager](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tikitu_de_jager/32/1258_2.png) [@Tikitu\_de\_Jager](https://discuss.elastic.co/u/Tikitu_de_Jager)\
**Post date:** [September 7, 2013, 10:05am UTC](https://discuss.elastic.co/t/accessing-parent-fields-from-custom-facet-over-child-docs-es-0-20/13345/2 "2013-09-07T10:05:41Z")

</div>

In case anyone stumbles on this searching the archives: I found a partial  
solution (see my StackOverflow answer[http://stackoverflow.com/questions/18481083/how-to-access-elasticsearch-parent-doc-fields-from-custom-facet-over-child-docs](http://stackoverflow.com/questions/18481083/how-to-access-elasticsearch-parent-doc-fields-from-custom-facet-over-child-docs))  
but ended up shifting to nested documents instead. A parent/child solution  
is possible but inefficient; nested docs gives (among other things) the  
assurance that the child docs will be in the same _segment_ as the parent,  
which simplifies things rather a lot.

On Tuesday, 27 August 2013 16:39:27 UTC+3, Tikitu de Jager wrote:

> Hi folks,
> 
> I have most of a parent/child-doc solution for a problem I'm working on,  
> but I ran into a hitch: from inside a facet that iterates over the _child_ docs  
> I need to access the value of a _parent_ doc field. I have (or rather, I  
> presume I can get) the parent doc ID (from the \_parent field of the child  
> doc) but that's an "external" ID, not the node-internal ID that I need to  
> load the field value from the field cache. (I'm using default routing so  
> the parent doc is definitely in the same shard as the children.)
> 
> More concretely, here's what I have in the FacetCollector so far:
> 
> protected void doSetNextReader(IndexReader reader, int docBase) throws  
> IOException {  
> /\* not sure this will work, but I presume there's some way to get the  
> \_parent value, which in my case is a long _/  
> parentFieldData = (LongFieldData)  
> fieldDataCache.cache(FieldDataType.DefaultTypes.LONG, reader, "\_parent");  
> parentSpringinessFieldData = (FloatFieldData)  
> fieldDataCache.cache(FieldDataType.DefaultTypes.FLOAT, "springiness");  
> /_ ... \*/
> 
> protected void doCollect(int doc) throws IOException {  
> long parentID = parentFieldData.value(doc); // or whatever the  
> correct equivalent here is  
> // here's the problem:  
> parentSpringiness = parentSpringinessFieldData.value(parentID) // type  
> error: expected int (node-internal ID), got long (external ID)
> 
> Any suggestions? (I can't upgrade to 0.90 yet but would be interested to  
> hear if that would help.)
> 
> Cheers,  
> Tikitu

--  
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:17am UTC](https://discuss.elastic.co/t/accessing-parent-fields-from-custom-facet-over-child-docs-es-0-20/13345/3 "2017-07-06T02:17:38Z")

</div>

Closed on July 6, 2017, 2:17am UTC


