5

I have created an index and added a document using java Index API.

client.prepareIndex("details", "Key", i).setSource(putJsonDocumentString(Key, Value)).execute().actionGet();

This worked fine. Index was created and document was indexed properly. Now, I need to add another document "Keys2" in this same index. So, I did this,

client.prepareUpdate("details", "Keys2", i).setScriptParams(putJsonDocumentString(Key, Value)).execute().actionGet();

But, it's not getting added to the above index. I don't want to use Bulk API (I kept getting ClusterBlockedException, which never resolved, plus I don't have much data either) I couldn't find any sample program doing the same thing.

The exception is:

ActionRequestValidationException: Validation Failed: 1: script or doc is missing

How do I resolve this?

The method putJsonDocumentString() returns a Map<string, Object> which should work with setScriptParams(), right?

2 Answers 2

2

If I understand well, you want to add another document in the same index.

In this case, this is quite simple : you can use the same API as the first document, but with related parameters:

client.prepareIndex("details", <type_name>, <id>).setSource(putJsonDocumentString(Key, Value)).execute().actionGet();

with and placeholders of the real value of your 2nd document. From what you described, Key2 is the id, and not the type of your document.

If you are confused about the index, type and document concepts, check the basic description from the documentation.

The prepareIndex API add (or update) a document to an index. In your case, if you don't have previously created the index and type, ElasticSearch will create it on-the-fly with default parameters.

The prepareUpdate API is used to update existing documents, not to add new documents to an existing index. From documentation :

The update API allows to update a document based on a script provided.

which explains the error message you have.

Sign up to request clarification or add additional context in comments.

2 Comments

'Key' and 'Key2' are just random name given. Originally, I'm receiving a JSON document when an event is triggered. One object has all the data related to Transaction (which I called Key) and other has all data related to Monitoring (lets name it Key2), and similarly other objects exist. Now, each of these Transaction, Monitor, Log etc (of one particular event) have to be indexed as separate documents in one index. So, is my approach still wrong? I'm new to ES, so if you could point out the mistake it would be really helpful. Thanks
You have multiple ways to handle relationships in ES, but this is a separate subject. There is a dedicated section in the definitive guide which would give you more details.
0

You should index subsequent documents the same way you have the first, as @ThomasC said. I think the problem you're having is that you are trying to index 2 documents with he same ID and different types. you could address this by indexing the first like this:

client.prepareIndex("details", "Key", "ID1").setSource(putJsonDocumentString(Key, Value)).execute().actionGet();

And the second like this:

client.prepareIndex("details", "Key2", "ID2").setSource(putJsonDocumentString(Key, Value)).execute().actionGet();

Ideally IDs will be something easier to ensure are unique, such as UUIDs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.