1

I'm using elasticsearch version 6.4.2

I succeeded to create a GET query using the REST API, now I would like to perform the same query using the JAVA api.

This is the query:

GET _search
{
   "query":{
      "bool":{
         "must":{
            "match":{
               "tags":"kpi"
            }
         },
         "filter":{
            "range":{
               "@timestamp":{
                  "gt":"now-5m"
               }
            }
         }
      }
   }
}

I read this documentation: https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-compound-queries.html but it is still not clear to me if this is I'm looking for and how to use it. Consider that the query could return a lot of results that have to be processed by my java application.

Also, since I have to perform this query every 5 minutes, how can I optimize it (if possible)?

3
  • @Spara can you explain what more you need? Commented Nov 13, 2018 at 16:24
  • @Val, I think the accepted answer is not good as the other one! so I want to give him the bounty! Commented Nov 13, 2018 at 16:28
  • Gotcha, so you have your answer already then, great ;-) Commented Nov 13, 2018 at 16:29

2 Answers 2

2
+50

If your project is maven based, you can use elasticsearch client dependency:

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>transport</artifactId>
    <version>5.6.2</version>
</dependency>

your needed query would be:

val query = QueryBuilders.boolQuery()
    .must(
        QueryBuilders.rangeQuery("timestamp").from(startDate)
    )
    .must(
        QueryBuilders.termQuery("tags", "kpi")
    )

and finally to execute your query you can use client.search(query). client is a RestHighLevelClient type.

Sign up to request clarification or add additional context in comments.

Comments

0

This is the exact code that works:

Date dateFrom = new Date(System.currentTimeMillis() - 300 * 1000);
Date dateTo = new Date(System.currentTimeMillis());

BoolQueryBuilder query = QueryBuilders.boolQuery()
    .must(QueryBuilders.rangeQuery("@timestamp").from(dateFrom).to(dateTo))
    .must(QueryBuilders.matchQuery("tags", "kpi"));

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(query);
SearchRequest searchRequest = new SearchRequest();
searchRequest.source(searchSourceBuilder);
System.out.println(query.toString());
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

logger.log(Level.INFO, "Status: {0}", new Object[] { searchResponse.status() });
logger.log(Level.INFO, "Took: {0}", new Object[] { searchResponse.getTook() });
logger.log(Level.INFO, "IsTerminatedEarly: {0}", new Object[] { searchResponse.isTerminatedEarly() });
logger.log(Level.INFO, "TimedOut: {0}", new Object[] { searchResponse.isTimedOut() });
logger.log(Level.INFO, "TotalShards: {0}", new Object[] { searchResponse.getTotalShards() });
logger.log(Level.INFO, "SuccessfulShards: {0}", new Object[] { searchResponse.getSuccessfulShards() });
logger.log(Level.INFO, "FailedShards: {0}", new Object[] { searchResponse.getFailedShards() });
logger.log(Level.INFO, "Total Hits: {0}", new Object[] { searchResponse.getHits().getTotalHits() });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.