Search code examples
springelasticsearchspring-dataspring-data-elasticsearch

Spring Data Elasticsearch - Is Inner Hit supported at root level on query?


I'm trying to use ElasticsearchOperations to write a query like this one:

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "dialogLines",
            "query": {
              "match_phrase": {
                "dialogLines.text": {
                  "query": "No problem"
                }
              }
            }
          }
        }
      ]
    }
  },
  "collapse": {
    "field": "movieId",
    "inner_hits" : {
        "name": "by_movie",
        "collapse" : {"field" : "boundaryGroup"},
        "sort": [{ "boundaryStartInMillis": "desc" }],
        "size": 6
    }
  }
}

I know NestedQuery supports innerHit(InnerHitBuilder innerHitBuilder), but the root level query of type NativeSearchQuery doesn't have that method.

Is there a way to write this query using ElasticsearchOperations?

I'm on version org.springframework.data:spring-data-elasticsearch:3.2.9.RELEASE

Running Elasticsearch v6.8.2


Solution

  • When you create a query using NativeSearchQuery, you normally use

    Query query = new NativeSearchQueryBuilder()
      .withQuery(queryBuilder)
      .build();
    

    The queryBuilder argument is any implementation of org.elasticsearch.index.query.QueryBuilder, so you can use any of the Elasticsearch query builders that you like.

    Edit 29.09.2020:

    NativeSsearchQuery supports a collapse field with the NativeSearchQueryBuilder.withCollapseField(String) method. But there is no support for the inner hits of a collapse field yet. I created https://jira.spring.io/browse/DATAES-939 to address that.