# How use JAVA API of Elasticsearch

**URL:** <https://discuss.elastic.co/t/how-use-java-api-of-elasticsearch/50464>\
**Category:** Elasticsearch\
**Created:** [May 19, 2016, 3:46pm UTC](https://discuss.elastic.co/t/how-use-java-api-of-elasticsearch/50464 "2016-05-19T15:46:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vincent\_Vost](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vincent_vost/32/9946_2.png) [@Vincent\_Vost](https://discuss.elastic.co/u/Vincent_Vost)\
**Post date:** [May 19, 2016, 3:46pm UTC](https://discuss.elastic.co/t/how-use-java-api-of-elasticsearch/50464/1 "2016-05-19T15:46:35Z")

</div>

Hello 🙂

I'm trying to use ELS in a JAVA application and do some machine learning after..  
But I have some trouble, do you have some exemple how use ELS with the JAVA API ?  
Do some search in java ? I search in google, try different things, but without result.

Thank if you can assist me a little for start it.  
Vincent

---

<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:** [May 19, 2016, 5:43pm UTC](https://discuss.elastic.co/t/how-use-java-api-of-elasticsearch/50464/2 "2016-05-19T17:43:33Z")

</div>

Have a look at the java guide. [https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/index.html](https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/index.html)

Also you can look at: [https://github.com/dadoonet/legacy-search/tree/05-compute?files=1](https://github.com/dadoonet/legacy-search/tree/05-compute?files=1)

HTH

---

<div class="post-metadata">

**Author:** ![Vincent\_Vost](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vincent_vost/32/9946_2.png) [@Vincent\_Vost](https://discuss.elastic.co/u/Vincent_Vost)\
**Post date:** [May 20, 2016, 8:10am UTC](https://discuss.elastic.co/t/how-use-java-api-of-elasticsearch/50464/3 "2016-05-20T08:10:56Z")

</div>

Need I all branch for try to run this project ? 0 to 5?

I'm going to program a new app, so I'm in the beginning of the project for this part.  
thank

---

<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:** [May 20, 2016, 11:11am UTC](https://discuss.elastic.co/t/how-use-java-api-of-elasticsearch/50464/4 "2016-05-20T11:11:50Z")

</div>

You can use branch 5 directly.

---

<div class="post-metadata">

**Author:** ![Vincent\_Vost](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vincent_vost/32/9946_2.png) [@Vincent\_Vost](https://discuss.elastic.co/u/Vincent_Vost)\
**Post date:** [May 25, 2016, 2:57pm UTC](https://discuss.elastic.co/t/how-use-java-api-of-elasticsearch/50464/5 "2016-05-25T14:57:04Z")

</div>

I have clone branch 5, but maven don't find dependence of `io.restx`

If I understand more or less the code `ElasticsearchDao` is the file of the connexion.  
The data are center in person information.

But for what is `LegacySearchApp` ? My app ?  
Sorry for this question, but a lot of things are totally new for me in this environnement.

---

<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:** [May 25, 2016, 3:46pm UTC](https://discuss.elastic.co/t/how-use-java-api-of-elasticsearch/50464/6 "2016-05-25T15:46:41Z")

</div>

Hey Vincent

I pasted this link as an example of what you can do.

You don't have to use RESTX if you don't want to.  
Just pick part of the code you need.

Again, I'd read the Java guide.

If you want to run my example project, then run:

```auto
mvn clean install
mvn jetty:run

```

`LegacySearchApp` is my main application. It has a [main method](https://github.com/dadoonet/legacy-search/blob/00-legacy/src/main/java/fr/pilato/demo/legacysearch/app/LegacySearchApp.java#L31). So you can run this class from your IDE if you wish.

---

<div class="post-metadata">

**Author:** ![Vincent\_Vost](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vincent_vost/32/9946_2.png) [@Vincent\_Vost](https://discuss.elastic.co/u/Vincent_Vost)\
**Post date:** [June 15, 2016, 8:09am UTC](https://discuss.elastic.co/t/how-use-java-api-of-elasticsearch/50464/7 "2016-06-15T08:09:53Z")

</div>

Hi! Thank you, I tried this solution, but finally I chose Spark which provide an API for Elasticsearch too.

I just put some code of spark here, maybe it can help someone one day.

```
    SparkConf conf = new SparkConf().setAppName("MySparkElas").setMaster("local");
    conf.set("es.index.auto.create", "true");
    conf.set("es.nodes", "172.26.167.204");
    conf.set("es.port", "9200");
    JavaSparkContext sc = new JavaSparkContext(conf);

    String target ="logstash-*/Collector-ticketNEW";
    JavaPairRDD<String, Map<String, Object>> esRDD2 = JavaEsSpark.esRDD(sc, target);

    JavaRDD newRDD = esRDD2.map(x -> x._2);

    JavaRDD<Ticket> tickets = newRDD.map(
            new Function<Map<String, Object>, Ticket>() {
                @Override
                public Ticket call(Map<String, Object> o) throws Exception {
                    Ticket ticket = new Ticket();
                    //exemple of mapping, but this one is not very good:
		    String[] parts = o.toString().split(",");
                    Ticket ticket = new Ticket();
                    ticket.setIpAddress(parts[3]);

                    return ticket;
                }

            });

```

And why not, use SQL after

```
    // Apply a schema to an RDD of JavaBeans and register it as a table.
    SQLContext sqlContext = new SQLContext(sc);
    DataFrame schemaTicket = sqlContext.createDataFrame(tickets,Ticket.class);
    schemaTicket.registerTempTable("tickets");

    DataFrame df = sqlContext.sql("SELECT * FROM tickets");

```

and show the result df.show();

---

<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:** [July 5, 2017, 10:43pm UTC](https://discuss.elastic.co/t/how-use-java-api-of-elasticsearch/50464/8 "2017-07-05T22:43:47Z")

</div>


