Search code examples
javaelasticsearchjava-client

Parse_exception: collapse failed to parse field inner_hits


Using the javaclient doing inner_hits inside a collapse, I am getting this error: Caused by: co.elastic.clients.elasticsearch._types.ElasticsearchException: [es/search] failed: [x_content_parse_exception] [1:500] [collapse] failed to parse field [inner_hits]

My code:

      FieldCollapse.Builder builder = new FieldCollapse.Builder() //
          .field(collapse.getField());

      if (collapse.getInnerHits() != null) {
        List<InnerHits> innerHits = getInnerHitsBuilderList(collapse.getInnerHits());
        builder.innerHits(innerHits);
      }
      return Optional.of(builder.build());

and:


        InnerHits.Builder builder = new InnerHits.Builder().name(innerHit.getName());

        NullOrEmpty.optional(innerHit.getFetchFields())
            .ifPresent(p -> builder.fields(Lists.mutable.ofAll(p)));
        NullOrEmpty.optional(innerHit.getStoredFields())
            .ifPresent(p -> builder.storedField(Lists.mutable.ofAll(p)));

The collapse portion of the query:

"collapse": {
    "field": "fielda.innerFieldb",
    "inner_hits": [
      {
        "name": "Topics",
        "size": 100,
        "collapse": {
          "field": "topic.topicId"
        },
        "fields": [
          "topic.shortName"
        ],
        "stored_field": [
          "topic.shortName"
        ]
      }
    ]
  }

The interesting thing here is that the documentation for collapse lists inner_hits as an object (not an array): https://www.elastic.co/guide/en/elasticsearch/reference/8.8/collapse-search-results.html

"collapse": {
    "field": "user.id",                       
    "inner_hits": {
      "name": "most_recent",                  
      "size": 5,                              
      "sort": [ { "@timestamp": "desc" } ]    
    },
    "max_concurrent_group_searches": 4        
  }

And yet the code of the FieldCollapse class has a List<InnerHits>

So, what am I doing wrong?


Solution

  • The correct name is indeed stored_fields and not stored_field.

    There's an open issue for this: https://github.com/elastic/elasticsearch-java/issues/409 and while it seems to be fixed, it's not merged yet.