Terms Aggregation request returning empty Bucket list in Java TransportClient

Hey there, I am using ES 6.2.4 with the Java TransportClient to perform a search request (through an inline Script). The search request has a Terms aggregation by field. When I run the search request in Kibana, I properly get the aggregated counts that I want. However in Java, the getBuckets object returns an emtpy list. This is the Search script:

{
  "_source": ["_id",  "text"],
          "from" : 0, "size" : 50,
          
    "query": {
        "function_score": {
          
          "query": {
            "multi_match": {
              "type":     "most_fields", 
              "query":    "{{param_text}}",
              "fields": [ "title", "text^2" ]
            }
          },
          "boost": 1,
          
          "script_score" : {
                "script" : {
                  "source": "_score * doc['boost'].value"
                }
            }
          
        }
    },
    
    "aggs" : {
        "document_types" : {
            "terms" : { "field" : "document_type" }
        }
    }
}

This is the Java code:

SearchResponse response = new SearchTemplateRequestBuilder(transportClient).setScript(s).setScriptType(ScriptType.INLINE).setScriptParams(params)
				.setRequest(new SearchRequest()).get().getResponse();

		List<Aggregation> aggregations = response.getAggregations().asList();
	Terms agg = response.getAggregations().get("document_types");
		for (Bucket entry : agg.getBuckets()) {
			String key = entry.getKeyAsString(); // bucket key
			long docCount = entry.getDocCount(); // Doc count
		}

Although the agg object is properly returned, the agg.getBuckets() method returns an empty list.

Your help would be much appreciated.
Kind regards,
Najib

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.