# Setting parent attribute for child document with java api

**URL:** https://discuss.elastic.co/t/setting-parent-attribute-for-child-document-with-java-api/17118
**Category:** Elasticsearch
**Created:** [April 22, 2014, 3:59am UTC](https://discuss.elastic.co/t/setting-parent-attribute-for-child-document-with-java-api/17118 "2014-04-22T03:59:07Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![echin1999\_2](https://avatars.discourse-cdn.com/v4/letter/e/f4b2a3/32.png) [@echin1999\_2](https://discuss.elastic.co/u/echin1999_2)
#### Post date: [April 22, 2014, 3:59am UTC](https://discuss.elastic.co/t/setting-parent-attribute-for-child-document-with-java-api/17118/1 "2014-04-22T03:59:07Z")

</div>

Hi. I am having trouble trying to set the parent attribute for a child  
document and was wondering if someone could suggest to me what i'm doing  
wrong.

I have a type called "feedback" which I want to assign a parent attribute -  
i want "feedback" to be a child of "content" - so i do the following:

_curl -XPUT [http://localhost:9200/myindex/feedback/\_mapping](http://localhost:9200/myindex/feedback/_mapping) -d '_

_{_

_"feedback" : {_

- 

```
      "_parent" : {*

```

- 

```
                "type" : "content"*

```

- 

```
                }*

```

- 

```
     }*

```

_}_'

My java code to insert a document of type "feedback" look like this:

_IndexResponse response = \_client.prepareIndex(INDEX, TYPE\_FEEDBACK,  
feedback.getFeedbackId()).setParent(""+feedback.getContentId()).setSource(feedbackJSON.toString())  
.execute().actionGet();_

I got back the response fine - and the response had the correct index,  
type, and id.

However, If i try to fetch what i just inserted:

_curl -XGET [http://localhost:9200/myindex/feedback/${1}?pretty=true](http://localhost:9200/myindex/feedback/%24%7B1%7D?pretty=true)_

I get nothing:

_{_

- "\_index" : "myindex",\*

- "\_type" : "feedback",\*

- "\_id" : "3860",\*

- "exists" : false\*

_}_

If I examine the mapping, I _think_ it looks ok:

_curl -XGET [http://localhost:9200/myindex/feedback/\_mapping](http://localhost:9200/myindex/feedback/_mapping)_

_{"feedback":{"\_parent":{"type":"content"},"\_routing":{"required":true},"properties":{"cid":{"type":"long"},"cmi":{"type":"long"},"cp":{"type":"long"},"ct":{"type":"long"},"cwn":{"type":"long"},"dur":{"type":"long"},"fee":{"type":"long"},"ia":{"type":"long"},"ito":{"type":"long"},"mir":{"type":"long"},"mvd":{"type":"long"},"mvt":{"type":"long"},"n":{"type":"string"},"own":{"type":"long"},"pdt":{"type":"long"},"sav":{"type":"string"},"td":{"type":"long"}}}}_

If just search for all id's for that type, I do see the entry:

_curl [http://localhost:9200/myindex/feedback/\_search?pretty=true](http://localhost:9200/myindex/feedback/_search?pretty=true) -d '_

\*{ \*

- "query" : { \*

- 

```
   "match_all" : {} *

```

- },\*

- "fields": []\*

_}_

_'_

_{_

- "took" : 13,\*

- "timed\_out" : false,\*

- "\_shards" : {\*

- "total" : 5,\*

- "successful" : 5,\*

- "failed" : 0\*

- },\*

- "hits" : {\*

- "total" : 3,\*

- "max\_score" : 1.0,\*

- "hits" : [ {\*

- 

```
 "_index" : "myindex",*

```

- 

```
 "_type" : "feedback",*

```

- 

```
 "_id" : "1_3858",*

```

- 

```
 "_score" : 1.0*

```

- }, {\*

- 

```
 "_index" : "myindex",*

```

- 

```
 "_type" : "feedback",*

```

- 

```
 "_id" : "1_3859",*

```

- 

```
 "_score" : 1.0*

```

- }, {\*

- 

```
 "_index" : "myindex",*

```

- 

```
 "_type" : "feedback",*

```

- 

```
 "_id" : "3860",*

```

- 

```
 "_score" : 1.0*

```

- } ]\*

- }\*

_}_

Could someone tell me what I could be missing? This is version 0.90

Many thanks,  
Ed

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/8fd5d014-73f9-4f7b-b7d5-a8a73e3c2151%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8fd5d014-73f9-4f7b-b7d5-a8a73e3c2151%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![echin1999\_2](https://avatars.discourse-cdn.com/v4/letter/e/f4b2a3/32.png) [@echin1999\_2](https://discuss.elastic.co/u/echin1999_2)
#### Post date: [April 22, 2014, 5:10pm UTC](https://discuss.elastic.co/t/setting-parent-attribute-for-child-document-with-java-api/17118/2 "2014-04-22T17:10:19Z")

</div>

Parly resolved! My problem was that I need to specify the routing. With  
Parent-Child, routing becomes required.

However, even though i'm able to see my documents again, the "\_parent"  
attribute doesn't seem to show up when I query a doc. Do I need to  
configure the parent type somehow for it to work? I simply modified the  
mapping of the child type to include \_parent. When querying for the  
mapping, i see \_parent in the result:

_curl -XGET [http://localhost:9200/myindex/feedback/\_mapping](http://localhost:9200/myindex/feedback/_mapping)  
 [http://localhost:9200/myindex/feedback/\_mapping](http://localhost:9200/myindex/feedback/_mapping)_

_{"feedback":{"\_parent":{"type":"content"},"\_routing":{"required":true},"properties":{"cid":{"type":"long"},"cmi":{"type":"long"},"cp":{"type":"long"},"ct":{"type":"long"},"cwn":{"type":"long"},"dur":{"type":"long"},"fee":{"type":"long"},"ia":{"type":"long"},"ito":{"type":"long"},"mir":{"type":"long"},"mvd":{"type":"long"},"mvt":{"type":"long"},"n":{"type":"string"},"own":{"type":"long"},"pdt":{"type":"long"},"sav":{"type":"string"},"td":{"type":"long"}}}}_

Thanks!

On Monday, April 21, 2014 11:59:07 PM UTC-4, echin1999 wrote:

> Hi. I am having trouble trying to set the parent attribute for a child  
> document and was wondering if someone could suggest to me what i'm doing  
> wrong.
> 
> I have a type called "feedback" which I want to assign a parent attribute
> 
> - i want "feedback" to be a child of "content" - so i do the following:
> 
> _curl -XPUT [http://localhost:9200/myindex/feedback/\_mapping](http://localhost:9200/myindex/feedback/_mapping)  
> [http://localhost:9200/myindex/feedback/\_mapping](http://localhost:9200/myindex/feedback/_mapping) -d '_
> 
> _{_
> 
> _"feedback" : {_
> 
> - 
> 
> ```
> "_parent" : {*
> 
> ```
> 
> - 
> 
> ```
> "type" : "content"*
> 
> ```
> 
> - 
> 
> ```
> }*
> 
> ```
> 
> - 
> 
> ```
> }*
> 
> ```
> 
> _}_'
> 
> My java code to insert a document of type "feedback" look like this:
> 
> _IndexResponse response = \_client.prepareIndex(INDEX, TYPE\_FEEDBACK,  
> feedback.getFeedbackId()).setParent(""+feedback.getContentId()).setSource(feedbackJSON.toString())  
> .execute().actionGet();_
> 
> I got back the response fine - and the response had the correct index,  
> type, and id.
> 
> However, If i try to fetch what i just inserted:
> 
> _curl -XGET [http://localhost:9200/myindex/feedback/${1}?pretty=true](http://localhost:9200/myindex/feedback/$%7B1%7D?pretty=true)  
> [http://localhost:9200/myindex/feedback/${1}?pretty=true](http://localhost:9200/myindex/feedback/$%7B1%7D?pretty=true)_
> 
> I get nothing:
> 
> _{_
> 
> - "\_index" : "myindex",\*
> 
> - "\_type" : "feedback",\*
> 
> - "\_id" : "3860",\*
> 
> - "exists" : false\*
> 
> _}_
> 
> If I examine the mapping, I _think_ it looks ok:
> 
> _curl -XGET [http://localhost:9200/myindex/feedback/\_mapping](http://localhost:9200/myindex/feedback/_mapping)  
> [http://localhost:9200/myindex/feedback/\_mapping](http://localhost:9200/myindex/feedback/_mapping)_
> 
> _{"feedback":{"\_parent":{"type":"content"},"\_routing":{"required":true},"properties":{"cid":{"type":"long"},"cmi":{"type":"long"},"cp":{"type":"long"},"ct":{"type":"long"},"cwn":{"type":"long"},"dur":{"type":"long"},"fee":{"type":"long"},"ia":{"type":"long"},"ito":{"type":"long"},"mir":{"type":"long"},"mvd":{"type":"long"},"mvt":{"type":"long"},"n":{"type":"string"},"own":{"type":"long"},"pdt":{"type":"long"},"sav":{"type":"string"},"td":{"type":"long"}}}}_
> 
> If just search for all id's for that type, I do see the entry:
> 
> _curl [http://localhost:9200/myindex/feedback/\_search?pretty=true](http://localhost:9200/myindex/feedback/_search?pretty=true)  
> [http://localhost:9200/myindex/feedback/\_search?pretty=true](http://localhost:9200/myindex/feedback/_search?pretty=true) -d '_
> 
> \*{ \*
> 
> - "query" : { \*
> 
> - 
> 
> ```
> "match_all" : {} *
> 
> ```
> 
> - },\*
> 
> - "fields": \*
> 
> _}_
> 
> _'_
> 
> _{_
> 
> - "took" : 13,\*
> 
> - "timed\_out" : false,\*
> 
> - "\_shards" : {\*
> 
> - "total" : 5,\*
> 
> - "successful" : 5,\*
> 
> - "failed" : 0\*
> 
> - },\*
> 
> - "hits" : {\*
> 
> - "total" : 3,\*
> 
> - "max\_score" : 1.0,\*
> 
> - "hits" : [ {\*
> 
> - 
> 
> ```
> "_index" : "myindex",*
> 
> ```
> 
> - 
> 
> ```
> "_type" : "feedback",*
> 
> ```
> 
> - 
> 
> ```
> "_id" : "1_3858",*
> 
> ```
> 
> - 
> 
> ```
> "_score" : 1.0*
> 
> ```
> 
> - }, {\*
> 
> - 
> 
> ```
> "_index" : "myindex",*
> 
> ```
> 
> - 
> 
> ```
> "_type" : "feedback",*
> 
> ```
> 
> - 
> 
> ```
> "_id" : "1_3859",*
> 
> ```
> 
> - 
> 
> ```
> "_score" : 1.0*
> 
> ```
> 
> - }, {\*
> 
> - 
> 
> ```
> "_index" : "myindex",*
> 
> ```
> 
> - 
> 
> ```
> "_type" : "feedback",*
> 
> ```
> 
> - 
> 
> ```
> "_id" : "3860",*
> 
> ```
> 
> - 
> 
> ```
> "_score" : 1.0*
> 
> ```
> 
> - } ]\*
> 
> - }\*
> 
> _}_
> 
> Could someone tell me what I could be missing? This is version 0.90
> 
> Many thanks,  
> Ed

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/58fa7370-9d52-4ce0-ab45-6c60716965e3%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/58fa7370-9d52-4ce0-ab45-6c60716965e3%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![echin1999\_2](https://avatars.discourse-cdn.com/v4/letter/e/f4b2a3/32.png) [@echin1999\_2](https://discuss.elastic.co/u/echin1999_2)
#### Post date: [April 22, 2014, 7:34pm UTC](https://discuss.elastic.co/t/setting-parent-attribute-for-child-document-with-java-api/17118/3 "2014-04-22T19:34:42Z")

</div>

ok. even though i don't see the "\_parent" in the result after doing the  
curl request for the document, the parent mapping does indeed exist, as i  
was able to run a query with the has\_parent - and it returned appropriately  
the correct children. So.. is it normal that the "\_parent" doesn't come  
back in the document requested?

thanks!

On Monday, April 21, 2014 11:59:07 PM UTC-4, echin1999 wrote:

> Hi. I am having trouble trying to set the parent attribute for a child  
> document and was wondering if someone could suggest to me what i'm doing  
> wrong.
> 
> I have a type called "feedback" which I want to assign a parent attribute
> 
> - i want "feedback" to be a child of "content" - so i do the following:
> 
> _curl -XPUT [http://localhost:9200/myindex/feedback/\_mapping](http://localhost:9200/myindex/feedback/_mapping)  
> [http://localhost:9200/myindex/feedback/\_mapping](http://localhost:9200/myindex/feedback/_mapping) -d '_
> 
> _{_
> 
> _"feedback" : {_
> 
> - 
> 
> ```
> "_parent" : {*
> 
> ```
> 
> - 
> 
> ```
> "type" : "content"*
> 
> ```
> 
> - 
> 
> ```
> }*
> 
> ```
> 
> - 
> 
> ```
> }*
> 
> ```
> 
> _}_'
> 
> My java code to insert a document of type "feedback" look like this:
> 
> _IndexResponse response = \_client.prepareIndex(INDEX, TYPE\_FEEDBACK,  
> feedback.getFeedbackId()).setParent(""+feedback.getContentId()).setSource(feedbackJSON.toString())  
> .execute().actionGet();_
> 
> I got back the response fine - and the response had the correct index,  
> type, and id.
> 
> However, If i try to fetch what i just inserted:
> 
> _curl -XGET [http://localhost:9200/myindex/feedback/${1}?pretty=true](http://localhost:9200/myindex/feedback/$%7B1%7D?pretty=true)  
> [http://localhost:9200/myindex/feedback/${1}?pretty=true](http://localhost:9200/myindex/feedback/$%7B1%7D?pretty=true)_
> 
> I get nothing:
> 
> _{_
> 
> - "\_index" : "myindex",\*
> 
> - "\_type" : "feedback",\*
> 
> - "\_id" : "3860",\*
> 
> - "exists" : false\*
> 
> _}_
> 
> If I examine the mapping, I _think_ it looks ok:
> 
> _curl -XGET [http://localhost:9200/myindex/feedback/\_mapping](http://localhost:9200/myindex/feedback/_mapping)  
> [http://localhost:9200/myindex/feedback/\_mapping](http://localhost:9200/myindex/feedback/_mapping)_
> 
> _{"feedback":{"\_parent":{"type":"content"},"\_routing":{"required":true},"properties":{"cid":{"type":"long"},"cmi":{"type":"long"},"cp":{"type":"long"},"ct":{"type":"long"},"cwn":{"type":"long"},"dur":{"type":"long"},"fee":{"type":"long"},"ia":{"type":"long"},"ito":{"type":"long"},"mir":{"type":"long"},"mvd":{"type":"long"},"mvt":{"type":"long"},"n":{"type":"string"},"own":{"type":"long"},"pdt":{"type":"long"},"sav":{"type":"string"},"td":{"type":"long"}}}}_
> 
> If just search for all id's for that type, I do see the entry:
> 
> _curl [http://localhost:9200/myindex/feedback/\_search?pretty=true](http://localhost:9200/myindex/feedback/_search?pretty=true)  
> [http://localhost:9200/myindex/feedback/\_search?pretty=true](http://localhost:9200/myindex/feedback/_search?pretty=true) -d '_
> 
> \*{ \*
> 
> - "query" : { \*
> 
> - 
> 
> ```
> "match_all" : {} *
> 
> ```
> 
> - },\*
> 
> - "fields": \*
> 
> _}_
> 
> _'_
> 
> _{_
> 
> - "took" : 13,\*
> 
> - "timed\_out" : false,\*
> 
> - "\_shards" : {\*
> 
> - "total" : 5,\*
> 
> - "successful" : 5,\*
> 
> - "failed" : 0\*
> 
> - },\*
> 
> - "hits" : {\*
> 
> - "total" : 3,\*
> 
> - "max\_score" : 1.0,\*
> 
> - "hits" : [ {\*
> 
> - 
> 
> ```
> "_index" : "myindex",*
> 
> ```
> 
> - 
> 
> ```
> "_type" : "feedback",*
> 
> ```
> 
> - 
> 
> ```
> "_id" : "1_3858",*
> 
> ```
> 
> - 
> 
> ```
> "_score" : 1.0*
> 
> ```
> 
> - }, {\*
> 
> - 
> 
> ```
> "_index" : "myindex",*
> 
> ```
> 
> - 
> 
> ```
> "_type" : "feedback",*
> 
> ```
> 
> - 
> 
> ```
> "_id" : "1_3859",*
> 
> ```
> 
> - 
> 
> ```
> "_score" : 1.0*
> 
> ```
> 
> - }, {\*
> 
> - 
> 
> ```
> "_index" : "myindex",*
> 
> ```
> 
> - 
> 
> ```
> "_type" : "feedback",*
> 
> ```
> 
> - 
> 
> ```
> "_id" : "3860",*
> 
> ```
> 
> - 
> 
> ```
> "_score" : 1.0*
> 
> ```
> 
> - } ]\*
> 
> - }\*
> 
> _}_
> 
> Could someone tell me what I could be missing? This is version 0.90
> 
> Many thanks,  
> Ed

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/45d0c1b6-d306-46e8-966a-7e522de08cac%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/45d0c1b6-d306-46e8-966a-7e522de08cac%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 1:34am UTC](https://discuss.elastic.co/t/setting-parent-attribute-for-child-document-with-java-api/17118/4 "2017-07-06T01:34:16Z")

</div>


