Save elasticsearch document field value in java variable

(Teddy) #1

How can I save the the field value in a variable in Java.

  "took": 6,
  "timed_out": false,
  "_shards": {
    "total": 10,
    "successful": 10,
    "skipped": 0,
    "failed": 0
  "hits": {
    "total": 1776430,
    "max_score": 0,
    "hits": []
  "aggregations": {
    "num_prg_expenditure_avg": {
      "value": 721421.2185617918
    "expenditure_avg": {
      "value": 3876729.261930876

I need to access expenditure_avg and num_prg_expenditure_avg from above search result to store them in Java variable.

(David Pilato) #2

Which java client are you using?
How do you make that call?

(Teddy) #3

I want to use both

  1. High Level Rest Client

  2. Low Level Rest Client.

  3. By High Level

    RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost",9200,"http")));
     	SearchRequest searchRequest = new SearchRequest("mis_monthly_allotment_expenditure","mis_work_prg_exp");
     	SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
     	SearchResponse searchResponse =;
  4. For Low Level Client

     String jasonString = "{\r\n" + 
     				"  \"aggs\": {\r\n" + 
     				"    \"expenditure_avg\": {\r\n" + 
     				"      \"avg\": {\r\n" + 
     				"        \"field\": \"expenditure\"\r\n" + 
     				"      }\r\n" + 
     				"    },\r\n" + 
     				"    \"num_prg_expenditure_avg\": {\r\n" + 
     				"      \"avg\": {\r\n" + 
     				"        \"field\": \"num_prg_expanditure\" \r\n" + 
     				"      }\r\n" + 
     				"    }\r\n" + 
     				"  }\r\n" + 
     	HttpEntity entity = new NStringEntity(jasonString, ContentType.APPLICATION_JSON);
     	RestClient client = RestClient.builder(new HttpHost("localhost",9200,"http")).build();
     	Response response = client.performRequest("POST","mis_monthly_allotment_expenditure,mis_work_prg_exp/_search",paramMap,entity);

And the output is:

  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 10,
    "successful" : 10,
    "skipped" : 0,
    "failed" : 0
  "hits" : {
    "total" : 1776430,
    "max_score" : 0.0,
    "hits" : [ ]
  "aggregations" : {
    "num_prg_expenditure_avg" : {
      "value" : 721421.2185617918
    "expenditure_avg" : {
      "value" : 3876729.261930876

and how can I use pretty in High Level Client?

(David Pilato) #4

I'm not sure to understand what your problem is.

The only question I can see is:

and how can I use pretty in High Level Client?

The High Level client gives you back Java objects, not Strings. What do you want to do exactly?

(Teddy) #5

(David Pilato) #6

The SearchResponse object gives you access to the aggregations.

(Teddy) #7

How can i use SearchResponse instead of Response

Response response = client.performRequest("POST","mis_monthly_allotment_expenditure,mis_work_prg_exp/_search",paramMap,entity);

Here, Entity has my query in jsonString.

(David Pilato) #8

With the RestHighLevelClient as you already shown.

(Teddy) #9

How should i alter my Response to SearchResponse??

(David Pilato) #10

I did not mean that but to use:

RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost",9200,"http")));
SearchRequest sr = new SearchRequest("index");
SearchSourceBuilder ssb = new SearchSourceBuilder();

// Add whatever here

SearchResponse response =;

(Teddy) #11

I have already implemented these!
But the question remains as how do i access the field values by any methods??

Sorry I am a student and new to elasticsearch.:grin:

(David Pilato) #12

So this gives you back a SearchResponse object right?

So then just do that:

(Teddy) #13
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
TermsAggregationBuilder aggregation = AggregationBuilders.terms("by_company")

can you please explain what is "by_company"and "company.keyword"

(David Pilato) #14

by_company is the aggregation name you want to use. Can be whatever like expenditure_avg.
company.keyword is the field name you want to run the aggregation on. In your case, it can be expenditure.

So you can write something like:

SearchSourceBuilder ssb = new SearchSourceBuilder();

(Teddy) #15
Aggregations aggregations = searchResponse.getAggregations();
    Terms byCompanyAggregation = aggregations.get("by_company"); 
    Bucket elasticBucket = byCompanyAggregation.getBucketByKey("Elastic"); 
    Avg averageAge = elasticBucket.getAggregations().get("average_age"); 
    double avg = averageAge.getValue();

so similarly here
by_company is expenditure_avg
Then what is Elastic ?? what does getBucketByKey specify?
and what would be average_key for my case.

As I cannot get .getValue()

(David Pilato) #16

I think I gave an example.

May be you don't understand what aggregations are in general ?

May be read this then?

(Teddy) #17

I do.

Can you share your sample code as how to store the field value from elasticsearch query to a java variable.
Maybe that can help.

(David Pilato) #18

how to store the field value from elasticsearch query to a java variable.

If you meant reading a SearchResponse object, I already pasted a link previously:

If you can't make it work, create a simple Java JUnit test method which does everything (delete index, create index, put document, search) and then we can start from there. But I don't understand what you don't understand.

(system) #19

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