You can use Elastic\ScoutDriverPlus\Support\Query::bool()
to build a boolean query:
$query = Query::bool()->must(
Query::match()
->field('title')
->query('The Book')
);
$searchResult = Book::searchQuery($query)->execute();
Available methods:
The query defined with filter
must appear in the matching documents,
but won’t contribute to the score:
// you can make a query using builder
$filter = Query::term()
->field('published')
->value('2020-06-07');
// or you can define a raw query
$filter = [
'term' => [
'published' => '2020-06-07'
]
];
$query = Query::bool()->filter($filter);
$searchResult = Book::searchQuery($query)->execute();
The same query with filterRaw
method:
$query = Query::bool()->filterRaw([
'term' => [
'published' => '2020-06-07'
]
]);
$searchResult = Book::searchQuery($query)->execute();
Note that filterRaw
completely replaces the filter
clause of the query.
You can use minimumShouldMatch
to specify the number of should
queries
the documents must match:
$query = Query::bool()
->should(Query::term()->field('published')->value('2018-04-23'))
->should(Query::term()->field('published')->value('2020-03-07'))
->minimumShouldMatch(1);
$searchResult = Book::searchQuery($query)->execute();
The query defined with must
must appear in the matching documents
and will contribute to the score:
// you can make a query using builder
$must = Query::match()
->field('title')
->value('The Book');
// or you can define a raw query
$must = [
'match' => [
'title' => 'The Book'
]
];
$query = Query::bool()->must($must);
$searchResult = Book::searchQuery($query)->execute();
The same query with mustRaw
method:
$query = Query::bool()->mustRaw([
'match' => [
'title' => 'The Book'
]
]);
$searchResult = Book::searchQuery($query)->execute();
Note that mustRaw
completely replaces the must
clause of the query.
The query defined with mustNot
must not appear in the matching documents
and won’t contribute to the score:
// you can make a query using builder
$mustNot = Query::match()
->field('title')
->value('The Book');
// or you can define a raw query
$mustNot = [
'match' => [
'title' => 'The Book'
]
];
$query = Query::bool()->mustNot($mustNot);
$searchResult = Book::searchQuery($query)->execute();
The same query with mustNotRaw
method:
$query = Query::bool()->mustNotRaw([
'match' => [
'title' => 'The Book'
]
]);
$searchResult = Book::searchQuery($query)->execute();
Note that mustNotRaw
completely replaces the must_not
clause of the query.
Use onlyTrashed
method to get only soft deleted results:
$query = Query::bool()
->must($must)
->onlyTrashed();
$searchResult = Book::searchQuery($query)->execute();
The query defined with should
should appear in the matching documents:
// you can make a query using builder
$should = Query::match()
->field('title')
->value('The Book');
// or you can define a raw query
$should = [
'match' => [
'title' => 'The Book'
]
];
$query = Query::bool()->should($should);
$searchResult = Book::searchQuery($query)->execute();
The same query with shouldRaw
method:
$query = Query::bool()->shouldRaw([
'match' => [
'title' => 'The Book'
]
]);
$searchResult = Book::searchQuery($query)->execute();
Note that shouldRaw
completely replaces the should
clause of the query.
You can use withTrashed
to include soft deleted results
in the search result:
$query = Query::bool()
->must($must)
->withTrashed();
$searchResult = Book::searchQuery($query)->execute();