0

Elasticsearch Java client SearchResponse is unable to parse aggregation results. I've come across articles online which suggest adding aggregation types prefixed to the keys. I've added what I thought applies in my use case, such as "sterms# and sum#" but I am unable to figure out which type applies to the main filter (key:'matched' in my case). I am expecting the buckets object to be populated but it is currently being returned as an empty array despite response from elasticsearch containing aggregations.

Note: This is to be able to unit test.

Json response:

{
  "took": 7,
  "timed_out": false,
  "_shards": {
    "total": 75,
    "successful": 75,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 776329,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "sterms#matched": {
      "doc_count": 15,
      "sterms#id": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
          {
            "key": "mykey",
            "doc_count": 15,
            "sterms#manufacturerName": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": [
                {
                  "key": "subkey",
                  "doc_count": 15
                }
              ]
            },
            "sum#totalCars": {
              "value": 214244
            },
            "sum#totalTrucks": {
              "value": 155738
            }
          }
        ]
      }
    }
  }
}

Parser in Java

public static SearchResponse getSearchResponseFromJson(final String jsonFileName) {
        try {
            String jsonResponse = IOUtils.toString(new FileInputStream(jsonFileName));
            NamedXContentRegistry registry = new NamedXContentRegistry(getDefaultNamedXContents());
            XContentParser parser = JsonXContent.jsonXContent.createParser(registry, jsonResponse);
            return SearchResponse.fromXContent(parser);
        } catch (IOException e) {
            throw new RuntimeException("Failed to get resource: " + jsonFileName, e);
        } catch (Exception e) {
            throw new RuntimeException(("exception " + e));
        }
    }

    private static List<NamedXContentRegistry.Entry> getDefaultNamedXContents() {
        Map<String, ContextParser<Object, ? extends Aggregation>> map = new HashMap<>();
        map.put(TopHitsAggregationBuilder.NAME, (parser, content) -> ParsedTopHits.fromXContent(parser, (String) content));
        map.put(StringTerms.NAME, (parser, content) -> ParsedStringTerms.fromXContent(parser, (String) content));
        map.put(SumAggregationBuilder.NAME, (parser, content) -> ParsedSum.fromXContent(parser, (String) content));
        //map.put(ScriptedMetricAggregationBuilder.NAME, (parser, content) -> ParsedScriptedMetric.fromXContent(parser, (String) content));
        List<NamedXContentRegistry.Entry> entries = map.entrySet()
                                                       .stream()
                                                       .map(entry -> new NamedXContentRegistry.Entry(Aggregation.class,
                                                               new ParseField(entry.getKey()),
                                                               entry.getValue()))
                                                       .collect(Collectors.toList());
        return entries;
    }

Parsed response with empty buckets (expectation is for the buckets to have aggregations totalCars and totalTrucks)

{
    "took": 7,
    "timed_out": false,
    "_shards": {
        "total": 75,
        "successful": 75,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 776329,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "sterms#matched": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": []
        }
    }
}

I am not sure if sterms is the right type for the filter

Rishab
  • 143
  • 14
  • The answer for what type needs to be used is 'filter' (filter#matched). Still looking for the counter part Java class – Rishab Apr 23 '19 at 23:32
  • Aggregations are solved! See the answer here: https://stackoverflow.com/questions/49798654/json-string-to-elasticsearch-searchresponse-with-aggregation/57117845#57117845 – technocrat Jul 19 '19 at 18:35

1 Answers1

0

The answer for what type needs to be used is 'filter' (filter#matched). And Java class counterpart is FilterAggregationBuilder

Rishab
  • 143
  • 14