1

I am following this source:

Elastic Search Example

and I created the piece of code:

import static org.elasticsearch.node.NodeBuilder.nodeBuilder;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.elasticsearch.client.Client;
import org.elasticsearch.node.Node;

public class ElasticSearchAPI {

    public static void main(String[] args) {

        Node node = nodeBuilder().clusterName("yourclustername").node();
        Client client = node.client();

        client.prepareIndex("kodcucom", "article", "1")
                .setSource(
                        putJsonDocument(
                                "ElasticSearch: Java API",
                                "ElasticSearch provides the Java API, all operations "
                                        + "can be executed asynchronously using a client object.",
                                new Date(), new String[] { "elasticsearch" },
                                "Huseyin Akdogan")).execute().actionGet();

        node.close();
    }

    public static Map<String, Object> putJsonDocument(String title,
            String content, Date postDate, String[] tags, String author) {

        Map<String, Object> jsonDocument = new HashMap<String, Object>();

        jsonDocument.put("title", title);
        jsonDocument.put("conten", content);
        jsonDocument.put("postDate", postDate);
        jsonDocument.put("tags", tags);
        jsonDocument.put("author", author);

        return jsonDocument;
    }

}

I run ElasticSearch with command line:

elasticsearch.bat

and it runs correctly:

server

After that, I run my Java code and here is a log from Eclipse and server:

errors

Should I configure something? I saw few tutorials like this and everytime is really similar code which never works for me.

Thanks

2 Answers 2

1

Your jsonDocument has a typo:

 jsonDocument.put("conten", content);

Should be

 jsonDocument.put("content", content);

I presume

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

1 Comment

This is just a name of a key in my HashMap
1

Ok, I solved this problem. In fact, the problem was with the versions of ElasticSearch Client and ES Java API.

Upgrade ES Java API to the same version as ES Client solved this problem.

More info here:

Java API 1.x Client

Important:

Please note that you are encouraged to use the same version on client and cluster sides. You may hit some incompatibilities issues when mixing major versions.

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.