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.
dadoonet
(David Pilato)
January 19, 2018, 1:04pm
2
Which java client are you using?
How do you make that call?
I want to use both
High Level Rest Client
Low Level Rest Client.
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();
searchSourceBuilder.query(QueryBuilders.matchAllQuery()).aggregation(AggregationBuilders.avg("expenditure_avg").field("expenditure")).aggregation(AggregationBuilders.avg("num_prg_expenditure_avg").field("num_prg_expanditure"));
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest);
System.out.println(searchResponse.toString());
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);
System.out.println(EntityUtils.toString(response.getEntity()));
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?
dadoonet
(David Pilato)
January 22, 2018, 11:21am
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?
dadoonet
(David Pilato)
January 22, 2018, 12:55pm
6
The SearchResponse
object gives you access to the aggregations.
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.
dadoonet
(David Pilato)
January 22, 2018, 1:45pm
8
With the RestHighLevelClient
as you already shown.
How should i alter my Response to SearchResponse??
dadoonet
(David Pilato)
January 22, 2018, 1:59pm
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
sr.source(ssb);
SearchResponse response = client.search(sr);
dadoonet:
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost",9200,"http")));
SearchRequest sr = new SearchRequest("index");
SearchSourceBuilder ssb = new SearchSourceBuilder();
// Add whatever here
sr.source(ssb);
SearchResponse response = client.search(sr);
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.
dadoonet
(David Pilato)
January 22, 2018, 2:44pm
12
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
TermsAggregationBuilder aggregation = AggregationBuilders.terms("by_company")
.field("company.keyword");
aggregation.subAggregation(AggregationBuilders.avg("average_age")
.field("age"));
searchSourceBuilder.aggregation(aggregation);
can you please explain what is "by_company"and "company.keyword"
dadoonet
(David Pilato)
January 23, 2018, 10:09am
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();
ssb.aggregation(AggregationBuilders.avg("expenditure_avg").field("expenditure"));
ssb.aggregation(AggregationBuilders.avg("num_prg_expenditure_avg").field("num_prg_expanditure"));
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()
dadoonet
(David Pilato)
January 23, 2018, 10:48am
16
I think I gave an example.
May be you don't understand what aggregations are in general ?
May be read this then? https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html
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.
dadoonet
(David Pilato)
January 23, 2018, 11:12am
18
.index("person")
.withJson(ElasticsearchDao.class.getResourceAsStream("/person.json"))
);
logger.info("New index person has been created");
} catch (ElasticsearchException e) {
if (e.status() != 400) {
logger.warn("can not create index and mappings", e);
} else {
logger.debug("Index person was already existing. Skipping creating it again.");
}
}
}
public void saveAll(Iterable<Person> persons) throws IOException {
esClient.bulk(br -> {
br.index("person");
persons.forEach(person -> br.operations(ops -> ops.index(i -> i.document(person))));
return br;
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: Search API | Java REST Client [7.17] | Elastic
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
(system)
Closed
February 20, 2018, 11:12am
19
This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.