How use JAVA API of Elasticsearch

Hello :slight_smile:

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

Have a look at the java guide. 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

HTH

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

You can use branch 5 directly.

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.

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:

mvn clean install
mvn jetty:run

LegacySearchApp is my main application. It has a main method. So you can run this class from your IDE if you wish.

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