10

Hi I am trying to do query on elastic search by following the sql query and I want to implement same logic using Java API

select dttime, avg(cpu) from table cpustats where server="X" and dttime="Y" group by dttime,cpu

Now I have the following Java code but it does not return expected output

SearchResponse response = client.prepareSearch("cpuindex")
      .setTypes("cputype")
      .setQuery(QueryBuilders.matchAllQuery())
      .addAggregation(AggregationBuilders.terms("cpu_agg")
           .field("cpu").size(100))
      .execute().actionGet();

Please guide I am new to Elastic search. Thanks in advance.

2 Answers 2

18

I think this will help.

SearchResponse response=
                client.prepareSearch("your_index_name_here").setQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),
                FilterBuilders.andFilter(
                        FilterBuilders.termFilter("server","x"),
                        FilterBuilders.termFilter("dt_time","x")
                ))).addAggregation(
                AggregationBuilders.terms("dt_timeaggs").field("dt_time").size(100).subAggregation(
                        AggregationBuilders.terms("cpu_aggs").field("cpu").size(100)
                )
        ).setSize(0).get();

please verify.

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

5 Comments

Hi thanks for the answer. I have doubt what is requestBuilder here? New to Java APIs of elasticsearch its difficult to find what is it. Please guide.
its, what client.prepareSearch() returns.
See, i've changed my answer. If it solved your problem then accept it ??
I am working on it query fails it is not returning any results. Sure I will accept your answer once it is working I will edit it for others reference.
what's your data look like?
2

since elastic search 2.3, FilterBuilders class has been removed from JavaAPI. you can use

QueryBuilder qb = QueryBuilders.boolQuery()
        .must(QueryBuilders.matchQuery("_all", "JPMORGAN"))
        .must(QueryBuilders.matchQuery(field, value)) ;

instead, and set it to

.setQuery(qb).

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.