Use nestedSearch
to search in nested fields:
$searchResult = Book::nestedSearch()
->path('author')
->query(['match' => ['author.name' => 'Steven']])
->execute();
Available methods provided by NestedQueryBuilder
:
You can use ignoreUnmapped
to query multiple indices that may not contain the field path
:
$searchResult = Book::nestedSearch()
->path('author')
->query(['match' => ['author.name' => 'Steven']])
->ignoreUnmapped(true)
->execute();
Use path
to set a path to the nested field you wish to search in:
$searchResult = Book::nestedSearch()
->path('author')
->query(['match' => ['author.name' => 'Steven']])
->execute();
query
defines a raw query you wish to run on the nested field:
// option 1: use query type and body
$searchResult = Book::nestedSearch()
->path('author')
->query('match', ['author.name' => 'Steven'])
->execute();
// option 2: use an array
$searchResult = Book::nestedSearch()
->path('author')
->query(['match' => ['author.name' => 'Steven']])
->execute();
// option 3: use a query builder
$searchResult = Book::nestedSearch()
->path('author')
->query((new MatchQueryBuilder())->field('author.name')->query('Steven'))
->execute();
scoreMode
is used to set a scoring mode:
$searchResult = Book::nestedSearch()
->path('author')
->query(['match' => ['author.name' => 'Steven']])
->scoreMode('avg')
->execute();