Search code examples
javajsonmavenelasticsearchbuild-dependencies

How to post data to elastic search over java?


How to post data to elasticsearch from java application? What are the necessary Maven dependencies to make queries to elastic search engine?

I have done a lot of research but got confused.

Thanks in Advance !!!!!


Solution

  • The latest version of Elasticsearch will be enough (look for the latest version in maven repository https://mvnrepository.com/artifact/org.elasticsearch) :

    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>5.4.0</version>
    </dependency>
    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>rest</artifactId>
        <version>5.4.0</version>
    </dependency>
    

    In addition, if you are familiar with scala (in java these operations are pretty much the same), you can use this XContentBuilder example to create json objects: https://github.com/sslavian812/needls2/blob/master/src/main/scala/ru/yandex/spark/ElasticSearchHelper.scala#L42

    and this elasticsearch client request example: https://github.com/sslavian812/needls2/blob/master/src/main/scala/ru/yandex/spark/ElasticSearchHelper.scala#L125