Posting data to elasticsearch index using java API

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?

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 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:

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.

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.

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();

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?

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

I looked briefly at the code source and found that:

May be that can help?

Thank You So Much David for your help

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