0

This is my code in Marvel Sense:

GET /sweet/cake/_search
{
  "query": {
    "bool": {
      "must": [
        {"term": {
          "code":"18"
        }}
      ]
    }
  },
  "size": 0,
  "aggs": {
    "group_by_state": {
      "terms": {
        "field": "id"
      }
    }
  }
}

And I want to write it in Java but I dont't know how.

1
  • What version of Elasticsearch are you running? Commented Nov 26, 2015 at 14:04

1 Answer 1

3

You can find some examples in the official documentation for the Java client.

But in your case, you need to create one bool/must query using the QueryBuilders and one terms aggregation using the AggregationBuilders. It goes like this:

// build the query
BoolQueryBuilder query = QueryBuilders.boolFilter()
    .must(QueryBuilders.termFilter("code", "18"));

// build the terms sub-aggregation
TermsAggregation stateAgg = AggregationBuilders.terms("group_by_state")
    .field("id");

SearchResponse resp = client.prepareSearch("sweet")
        .setType("cake")
        .setQuery(query)
        .setSize(0)
        .addAggregation(stateAgg)
        .execute()
        .actionGet();
Sign up to request clarification or add additional context in comments.

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.