1

I have this query in Elasticsearch that is working perfectly if I run it from the command line:

POST http://localhost:9200/YOUR_INDEX_NAME/_search/
{
  "size": 0,
  "aggs": {
    "autocomplete": {
      "terms": {
        "field": "autocomplete",
        "order": {
          "_count": "desc"
        },
        "include": {
          "pattern": "c.*"
        }
      }
    }
  },
  "query": {
    "prefix": {
      "autocomplete": {
        "value": "c"
      }
    }
  }
}

I have tried to rewrite it in java using the native client:

SearchResponse searchResponse2 = newClient.prepareSearch(INDEX_NAME)
            .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
            .setQuery("{\n" +
                "  \"size\": 0,\n" +
                "  \"aggs\": {\n" +
                "    \"autocomplete\": {\n" +
                "      \"terms\": {\n" +
                "        \"field\": \"autocomplete\",\n" +
                "        \"order\": {\n" +
                "          \"_count\": \"desc\"\n" +
                "        },\n" +
                "        \"include\": {\n" +
                "          \"pattern\": \"c.*\"\n" +
                "        }\n" +
                "      }\n" +
                "    }\n" +
                "  },\n" +
                "  \"query\": {\n" +
                "    \"prefix\": {\n" +
                "      \"autocomplete\": {\n" +
                "        \"value\": \"c\"\n" +
                "      }\n" +
                "    }\n" +
                "  }\n" +
                "}").get();
        for (SearchHit res : searchResponse2.getHits()){
            System.out.println(res.getSourceAsString());

        }

Seems, that I'm missing something in this translation process. Thanks in advance

1 Answer 1

1

The Java client setQuery() method doesn't take a String with the JSON query, you need to build the query using the QueryBuilders helper methods and build the aggregation your the AggregationBuilders helper methods.

In your case that would go like this:

// build the aggregation
TermsBuilder agg = AggregationBuilders.terms("autocomplete")
        .field("autocomplete")
        .include("c.*")
        .order(Terms.Order.count(false));

// build the query
SearchResponse searchResponse2 = newClient.prepareSearch(INDEX_NAME)
        .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
        .setSize(0)
        .setQuery(QueryBuilders.prefixQuery("autocomplete", "c"))
        .addAggregation(agg)
        .get();
Sign up to request clarification or add additional context in comments.

4 Comments

Not sure why the downvote... Seems like a valid answer to me.
Thank you, the AggregationBuilder class doesn't have any method terms( String ) in the client 1.7.3 . Do you know any alternative?
My bad, sorry, it's AggregationBuilders and not AggregationBuilder (typed too fast). I fixed the answer.
Yes, Perfect!!! Thank you!!!! I had just to had a .get() at the end: earchResponse searchResponse2 = newClient.prepareSearch(INDEX_NAME) .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) .setSize(0) .setQuery(QueryBuilders.prefixQuery("autocomplete", "c")) .addAggregation(agg).get();

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.