Reputation: 21
I am new to EL. My req would be I need fetch the data from ELS through Java API using spring boot. I have written search query along with collapse and sort. Its working perfectly fine. But I am getting how to re-write this code in java spring boot. Could you please help me out.
Below my ELS query:
GET /test/_search
{
"query": {
"function_score": {
"query": {
"constant_score": {
"filter": {
"bool": {
"must": [
{
"match" : {
"job_status" : "SUCCESS"
}
},
{
"range": {
"input_count": {
"gte": 0
}
}
},
{
"range": {
"output_count": {
"gte": 0
}
}
},
{
"range": {
"@timestamp": {
"from" : "20/04/2020",
"to" : "26/04/2020",
"format" : "dd/MM/yyyy"
}
}
},
{
"script": {
"script": {
"source": "doc['output_count'].value < doc['input_count'].value",
"params": {}
}
}
}
]
}
}
}
}
}
},
"collapse": {
"field": "run_id.keyword"
},
"sort": [
{
"@timestamp": {
"order": "desc"
}
}
]
}
This is my Java code: Its is working fine. Here I need your help to add collapse & sort API code.
MultiSearchRequest multiRequest = new MultiSearchRequest();
SearchRequest rowCountMatchRequest = new SearchRequest();
SearchSourceBuilder rowCountMatchSearchSourceBuilder = new SearchSourceBuilder();
MultiSearchResponse response = null;
BoolQueryBuilder rowCountMatchQuery = QueryBuilders.boolQuery()
.must(QueryBuilders.matchQuery("job_status", Constants.SUCCESS))
.must(QueryBuilders.rangeQuery("input_record_count").gte(0))
.must(QueryBuilders.rangeQuery("output_record_count").gte(0))
.must(QueryBuilders.rangeQuery("@timestamp").format("dd/MM/yyyy").gte(fromDate).lte(toDate))
.must(QueryBuilders.scriptQuery(
new Script("doc['output_count'].value >= doc['input_count'].value")));
rowCountMatchSearchSourceBuilder.query(rowCountMatchQuery);
rowCountMatchRequest.indices(stblstreamsetindex);
rowCountMatchRequest.source(rowCountMatchSearchSourceBuilder);
multiRequest.add(rowCountMatchRequest);
response = restHighLevelClient.msearch(multiRequest, RequestOptions.DEFAULT);
Hope I am clear with my question.
Upvotes: 0
Views: 3140
Reputation: 41
Just add a SortBuilder to the SearchSourceBuilder:
rowCountMatchSearchSourceBuilder.sort(SortBuilders.fieldSort("@timestamp").order(SortOrder.DESC));
For "collapse" it could work like this:
rowCountMatchSearchSourceBuilder.collapse(new CollapseBuilder("run_id.keyword"));
Upvotes: 1