Spring Data Elasticsearch nested field multimatch query

Viewed 4358

I'm using elasticsearch with spring-data-elastic. And try to use multi search. The problem is while search wprking with class fields, its not working with nested fields. My mapping is like below

{
    "archieve": {
        "mappings": {
            "author": {
                "properties": {
                    "books": {
                        "type": "nested",
                        "properties": {
                            "id": {
                                "type": "long"
                            },
                            "name": {
                                "type": "string",
                                "analyzer": "standard"
                            }
                        }
                    },
                    "id": {
                        "type": "long"
                    },
                    "firstName": {
                        "type": "string",
                        "analyzer": "standard"
                    },
                    "lastName": {
                        "type": "string",
                        "analyzer": "standard"
                    }
                }
            }
        }
    }
}

and I have an endpoint with searchQuery like:

  @GetMapping(value = "/es/archieve/multi/{keyword}")
  public Page<Author> getBrandMulti(@PathVariable String keyword, Pageable pageable) {

      SearchQuery searchQuery = new NativeSearchQueryBuilder()
                      .withQuery(QueryBuilders.multiMatchQuery(keyword)
                              .field("firstName", 1.2f)
                              .field("books.name", 1.1f)
                              .type(MultiMatchQueryBuilder.Type.CROSS_FIELDS)
                              .fuzziness(Fuzziness.TWO)
                      )
                      .withIndices("archieve")
                      .withTypes("author")
                      .withPageable(pageable)
                      .build();

      return elasticsearchTemplate.queryForPage(searchQuery, Author.class);
  }

The problem is query is not working for nested field. Is there any suggestion, regards?

Update

In fact nested objects can be queried as

NativeSearchQueryBuilder()
    .withQuery(QueryBuilders.nestedQuery("books", 
         QueryBuilders.termQuery("books.name", searchKey)))

Is there anyway to concat two queries like

NativeSearchQueryBuilder()
    .withQuery(Query1)
    .withQuery(Query1)
    .build();
1 Answers
Related