# Posting data to elasticsearch index using java API

**URL:** <https://discuss.elastic.co/t/posting-data-to-elasticsearch-index-using-java-api/219714>\
**Category:** Elasticsearch\
**Created:** [February 18, 2020, 6:22am UTC](https://discuss.elastic.co/t/posting-data-to-elasticsearch-index-using-java-api/219714 "2020-02-18T06:22:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![cravinder489](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cravinder489/32/62858_2.png) [@cravinder489](https://discuss.elastic.co/u/cravinder489)\
**Post date:** [February 18, 2020, 6:22am UTC](https://discuss.elastic.co/t/posting-data-to-elasticsearch-index-using-java-api/219714/1 "2020-02-18T06:22:29Z")

</div>

I'm sending data to my elasticsearch index using JestClient in java(using a POJO class).

```
    JSONObject obj1 = new JSONObject();
        	    		obj1.put("name","answer");
                        test1 document = new test1(); //test1 is a POJO class
                        document.setQAJoinField(obj1);
                        document.setBody(str);
                        document.setCreationDate();
                        document.setOwner("Registration");
                        document.setIsValid(true);
                        JestResult result1 = jestClient.execute(new Index.Builder(document).index("idxquesanswer").type("_doc").id(Integer.toString(id)).build());

```

The above code works well. But,

```
JSONObject obj2 = new JSONObject();
    	    		obj2.put("name","question");
    	    		obj2.put("parent",Integer.toString(id-1));
                    test1 document1 = new test1();
                    document1.setQAJoinField(obj2);
                    document1.setBody(str);
                    document1.setTitle(str);
                    document1.setIsValid(true);
                    JestResult result2 = jestClient.execute(new Index.Builder(document1).index("idxquesanswer").type("_doc").id(Integer.toString(id)).build());

```

In the above code, I get an error:  
`"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"failed to parse"}],"type":"mapper_parsing_exception","reason":"failed to parse","caused_by":{"type":"illegal_argument_exception","reason":"[routing] is missing for join field [qajoinfield]"}},"status":400}, isSucceeded: false, response code: 400, error message: {"root_cause":[{"type":"mapper_parsing_exception","reason":"failed to parse"}],"type":"mapper_parsing_exception","reason":"failed to parse","caused_by":{"type":"illegal_argument_exception","reason":"[routing] is missing for join field [qajoinfield]"}`

How can I resolve it?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 18, 2020, 7:58am UTC](https://discuss.elastic.co/t/posting-data-to-elasticsearch-index-using-java-api/219714/2 "2020-02-18T07:58:24Z")

</div>

Welcome!

I think you need to provide the routing key (which is the parent id).

Please format your code, logs or configuration files using `</>` icon as explained in [this guide](https://discuss.elastic.co/t/about-the-elasticsearch-category/21) and not the citation button. It will make your post more readable.

Or use markdown style like:

````
```
CODE
```

````

This is the icon to use if you are not using markdown format:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/7/e/7e6e239431ec2d71cbf1beef741f2e93e7cc762c.jpg)

There's a live preview panel for exactly this reasons.

Lots of people read these forums, and many of them will simply skip over a post that is difficult to read, because it's just too large an investment of their time to try and follow a wall of badly formatted text.  
If your goal is to get an answer to your questions, it's in your interest to make it as easy to read and understand as possible.  
Please update your post.

---

<div class="post-metadata">

**Author:** ![cravinder489](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cravinder489/32/62858_2.png) [@cravinder489](https://discuss.elastic.co/u/cravinder489)\
**Post date:** [February 18, 2020, 9:53am UTC](https://discuss.elastic.co/t/posting-data-to-elasticsearch-index-using-java-api/219714/3 "2020-02-18T09:53:21Z")

</div>

Thanks David  
Yes, you're right. I need to provide the routing key. But I don't know how to do that.  
Also, I'm providing the parent id for the child document in the JSON format (as obj2).  
but still the issue isn't resolved.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 18, 2020, 10:09am UTC](https://discuss.elastic.co/t/posting-data-to-elasticsearch-index-using-java-api/219714/4 "2020-02-18T10:09:40Z")

</div>

I don't know Jest as I'm only using the official Java REST Client but I guess it exposes something like:

```
new Index.Builder(document1)
    .index("idxquesanswer")
    .type("_doc")
    .id(Integer.toString(id))
    .routing("parent_id") // Routing here
    .build();
```

---

<div class="post-metadata">

**Author:** ![cravinder489](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cravinder489/32/62858_2.png) [@cravinder489](https://discuss.elastic.co/u/cravinder489)\
**Post date:** [February 19, 2020, 4:57am UTC](https://discuss.elastic.co/t/posting-data-to-elasticsearch-index-using-java-api/219714/5 "2020-02-19T04:57:15Z")

</div>

Thanks David for help  
I tried this but there is no such option for setting routing value in this way.  
Can you please tell me how to send JSON parent and child documents to our elasticsearch index using java API?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 19, 2020, 9:55am UTC](https://discuss.elastic.co/t/posting-data-to-elasticsearch-index-using-java-api/219714/6 "2020-02-19T09:55:11Z")

</div>

Using Jest? I don't know as I said.

I looked briefly at the code source and found that:

> <https://github.com/searchbox-io/Jest/blob/fe19dc4396e08f0e03c4e749a60b8a0ed398f084/jest-common/src/main/java/io/searchbox/core/Doc.java#L84-L86>

May be that can help?

---

<div class="post-metadata">

**Author:** ![cravinder489](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cravinder489/32/62858_2.png) [@cravinder489](https://discuss.elastic.co/u/cravinder489)\
**Post date:** [February 25, 2020, 10:35am UTC](https://discuss.elastic.co/t/posting-data-to-elasticsearch-index-using-java-api/219714/7 "2020-02-25T10:35:47Z")

</div>

Thank You So Much David for your help

---

<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:** [March 24, 2020, 10:35am UTC](https://discuss.elastic.co/t/posting-data-to-elasticsearch-index-using-java-api/219714/8 "2020-03-24T10:35:51Z")

</div>

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