ElasticSearch - Java Connectivity

Could anyone help us establish elasticsearch-java connectivity through REST api.
It would be of great help if you provide a tutorial from basic.

Did you read https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/index.html ?

We were using 5.x version.
Thanks, we will upgrade and refer the documentation.

The document isnt helping to setup a simple connection.
I just need to execute a search query and display the result in Java.

The document isnt helping to setup a simple connection.

Ha? Initialization | Java REST Client [7.17] | Elastic

I just need to execute a search query and display the result in Java.

Thanks.

The connection is established and i am able to get host name, hits , etc but not the data in the index.

Hard to tell as I can't see your code.

Index -> million
Document Type -> million

public class HighLevel {	
	public static void main(String[] args) throws IOException,InterruptedException {	
		Runtime runtime = Runtime.getRuntime();
        //Pass string in this format to open Batch file
		runtime.exec("cmd /c start Z:\\elasticsearch-6.0.0\\bin\\elasticsearch.bat");
		Thread.sleep(15000);
	
		RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost",9200,"http")));
		
		SearchRequest searchRequest = new SearchRequest();
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
		searchSourceBuilder.query(QueryBuilders.matchAllQuery());
		searchRequest.source(searchSourceBuilder);
		SearchResponse searchResponse = client.search(searchRequest);
		SearchHits hits = searchResponse.getHits();
		long totalhits = hits.getTotalHits();
		System.out.println(totalhits);
		Integer i=1;
		while(i<totalhits)
		{			
			GetRequest getRequest = new GetRequest("million","million",Integer.toString(i));
			GetResponse getResponse = client.get(getRequest);
			
			if (getResponse.isExists()) {
			    String sourceAsString = getResponse.getSourceAsString();  
			    System.out.println(sourceAsString);
			}
			
			else
				System.out.println("Done");			
			i++;
		}			
		client.close();		
	}
}
  1. I need to search data by a specific type e.g. name, age, etc
  2. I need to apply joins to the documents in the index through logstash for all the data that i have and recurrent data as well

Please format your code using </> icon as explained in this guide. It will make your post more readable.

Or use markdown style like:

```
CODE
```

I edited your post.

What does this line prints?

		System.out.println(totalhits);

Could you also print:

		System.out.println(searchResponse.toString());
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 800000,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "million",
        "_type" : "million",
        "_id" : "15260",
        "_score" : 1.0,
        "_source" : {
          "name" : "Rfaon Euaku",
          "@version" : "1",
          "id" : 15260,
          "@timestamp" : "2017-11-16T11:54:58.594Z",
          "salary" : 72393,
          "age" : 23
        }
      },
      {
        "_index" : "million",
        "_type" : "million",
        "_id" : "15262",
        "_score" : 1.0,
        "_source" : {
          "name" : "Afdfx Kplbp",
          "@version" : "1",
          "id" : 15262,
          "@timestamp" : "2017-11-16T11:54:58.594Z",
          "salary" : 83784,
          "age" : 38
        }
      },
      {
        "_index" : "million",
        "_type" : "million",
        "_id" : "15270",
        "_score" : 1.0,
        "_source" : {
          "name" : "Dylmv Hcont",
          "@version" : "1",
          "id" : 15270,
          "@timestamp" : "2017-11-16T11:54:58.594Z",
          "salary" : 45764,
          "age" : 50
        }
      },
      {
        "_index" : "million",
        "_type" : "million",
        "_id" : "15272",
        "_score" : 1.0,
        "_source" : {
          "name" : "Ujsuh Hqrcy",
          "@version" : "1",
          "id" : 15272,
          "@timestamp" : "2017-11-16T11:54:58.594Z",
          "salary" : 49691,
          "age" : 25
        }
      },
      {
        "_index" : "million",
        "_type" : "million",
        "_id" : "15288",
        "_score" : 1.0,
        "_source" : {
          "name" : "Yuauq Efshp",
          "@version" : "1",
          "id" : 15288,
          "@timestamp" : "2017-11-16T11:54:58.594Z",
          "salary" : 71358,
          "age" : 42
        }
      },
      {
        "_index" : "million",
        "_type" : "million",
        "_id" : "15293",
        "_score" : 1.0,
        "_source" : {
          "name" : "Gvkar Brbvr",
          "@version" : "1",
          "id" : 15293,
          "@timestamp" : "2017-11-16T11:54:58.594Z",
          "salary" : 34988,
          "age" : 21
        }
      },
      {
        "_index" : "million",
        "_type" : "million",
        "_id" : "15296",
        "_score" : 1.0,
        "_source" : {
          "name" : "Headv Skopa",
          "@version" : "1",
          "id" : 15296,
          "@timestamp" : "2017-11-16T11:54:58.594Z",
          "salary" : 70742,
          "age" : 40
        }
      },
      {
        "_index" : "million",
        "_type" : "million",
        "_id" : "15297",
        "_score" : 1.0,
        "_source" : {
          "name" : "Jjkis Cbaci",
          "@version" : "1",
          "id" : 15297,
          "@timestamp" : "2017-11-16T11:54:58.594Z",
          "salary" : 30088,
          "age" : 52
        }
      },
      {
        "_index" : "million",
        "_type" : "million",
        "_id" : "15299",
        "_score" : 1.0,
        "_source" : {
          "name" : "Fanpi Eauvy",
          "@version" : "1",
          "id" : 15299,
          "@timestamp" : "2017-11-16T11:54:58.594Z",
          "salary" : 22330,
          "age" : 28
        }
      },
      {
        "_index" : "million",
        "_type" : "million",
        "_id" : "15304",
        "_score" : 1.0,
        "_source" : {
          "name" : "Wcppa Vocpa",
          "@version" : "1",
          "id" : 15304,
          "@timestamp" : "2017-11-16T11:54:58.594Z",
          "salary" : 57461,
          "age" : 35
        }
      }
    ]
  }
}

So what is wrong?

You said:

The connection is established and i am able to get host name, hits , etc but not the data in the index.

You have the data, right?

This is done.

Now i need to create parent-child relation in my documents.

There are 2 tables in MySQL.

Employee
    +------+-------------+--------+------+
    | id   | name        | salary | age  |
    +------+-------------+--------+------+
    |    0 | Dxgow Xkiuq |  40414 |   33 |
    |    1 | Kouni Pecre |  44814 |   41 |
    |    2 | Jtwfl Peiyw |  76611 |   37 |
    |    3 | Mnpxt Cpprc |  32538 |   31 |
    |    4 | Ukhey Uosqg |  90272 |   40 |
    |    5 | Eavcg Gqfoo |  43957 |   48 |
    |    6 | Bcllu Vhara |  42928 |   32 |
    |    7 | Bafna Qeymj |  91398 |   23 |
    |    8 | Qxwni Tgwdw |  60973 |   31 |
    |    9 | Jpcqc Qtyum |  54696 |   41 |
    +------+-------------+--------+------+


Age
    +--------------+
    | employee_age |
    +--------------+
    |           20 |
    |           21 |
    |           22 |
    |           23 |
    |           24 |
    |           25 |
    |           26 |
    |           27 |
    |           28 |
    |           29 |
    +--------------+

Now while importing this data to elasticsearch via logstash i need to create parent child relationship between these two tables as AGE being the parent and Employee being child.

Similarly to have parent-child relationship to the data which is already in the index.

These are the 2 situation where I am stuck.

That's another non related question.

Please open a new question about this.

Thanks

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.