0

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?

John B
  • 32,493
  • 6
  • 77
  • 98
  • Which version of ES are you running? – Val May 26 '23 at 03:51
  • Running version of ES is 8.7.0 – John B May 26 '23 at 09:49
  • Can you try without the `fields` and `stored_field` sections? – Val May 26 '23 at 10:36
  • So, looks like the issues is that `stored_field` should be `stored_fields`. Is that a bug in the ES client library? It is interesting that I got more details out of Kibana then the java client stack trace. I will have to remember that. – John B May 26 '23 at 10:59
  • https://artifacts.elastic.co/javadoc/co/elastic/clients/elasticsearch-java/8.7.0/co/elastic/clients/elasticsearch/core/search/InnerHits.html#storedField() – John B May 26 '23 at 11:22

1 Answers1

0

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.

Val
  • 207,596
  • 13
  • 358
  • 360