Skip to content

Latest commit

 

History

History
177 lines (134 loc) · 4.83 KB

compound-queries.md

File metadata and controls

177 lines (134 loc) · 4.83 KB

Compound Queries

Boolean

Use boolSearch to make a boolean query:

$searchResult = Book::boolSearch()
    ->must('match', ['title' => 'The Book'])
    ->execute();

Available methods provided by BoolQueryBuilder:

filter

The query defined with filter must appear in the matching documents, but won’t contribute to the score:

// option 1: use query type and body
$searchResult = Book::boolSearch()
    ->filter('term', ['published' => '2020-06-07'])
    ->execute();

// option 2: use an array
$searchResult = Book::boolSearch()
    ->filter(['term' => ['published' => '2020-06-07']])
    ->execute();

// option 3: use a query builder
$searchResult = Book::boolSearch()
    ->filter((new TermQueryBuilder())->field('published')->value('2020-06-07'))
    ->execute();

The same query with filterRaw method:

$searchResult = Book::boolSearch()
    ->filterRaw(['term' => ['published' => '2020-06-07']])
    ->execute();

minimumShouldMatch

You can use minimumShouldMatch to specify the number of should queries the documents must match:

$searchResult = Book::boolSearch()
    ->should('term', ['published' => '2018-04-23'])
    ->should('term', ['published' => '2020-03-07'])
    ->minimumShouldMatch(1)
    ->execute();

must

The query defined with must must appear in the matching documents and will contribute to the score:

// option 1: use query type and body
$searchResult = Book::boolSearch()
    ->must('match', ['title' => 'The Book'])
    ->execute();

// option 2: use an array
$searchResult = Book::boolSearch()
    ->must(['match' => ['title' => 'The Book']])
    ->execute();

// option 3: use a query builder
$searchResult = Book::boolSearch()
    ->must((new MatchQueryBuilder())->field('title')->query('The Book'))
    ->execute();

There is also a raw version of this method:

$searchResult = Book::boolSearch()
    ->mustRaw(['match' => ['title' => 'The Book']])
    ->execute();

mustNot

The query defined with mustNot must not appear in the matching documents and won’t contribute to the score:

// option 1: use query type and body
$searchResult = Book::boolSearch()
    ->mustNot('match', ['title' => 'The Book'])
    ->execute();

// option 2: use an array
$searchResult = Book::boolSearch()
    ->mustNot(['match' => ['title' => 'The Book']])
    ->execute();

// option 3: use a query builder
$searchResult = Book::boolSearch()
    ->mustNot((new MatchQueryBuilder())->field('title')->query('The Book'))
    ->execute();

or using mustNotRaw:

$searchResult = Book::boolSearch()
    ->mustNotRaw(['match' => ['title' => 'The Book']])
    ->execute();

onlyTrashed

Use onlyTrashed method to get only soft deleted results:

$searchResult = Book::boolSearch()
    ->onlyTrashed()
    ->execute();

should

The query defined with should should appear in the matching documents:

// option 1: use query type and body
$searchResult = Book::boolSearch()
    ->should('match', ['title' => 'The Book'])
    ->execute();

// option 2: use an array
$searchResult = Book::boolSearch()
    ->should(['match' => ['title' => 'The Book']])
    ->execute();

// option 3: use a query builder
$searchResult = Book::boolSearch()
    ->should((new MatchQueryBuilder())->field('title')->query('The Book'))
    ->execute();

You can also take advantage of shouldRaw method:

$searchResult = Book::boolSearch()
    ->shouldRaw(['match' => ['title' => 'The Book']])
    ->execute();

withTrashed

You can use withTrashed to include soft deleted results in the search result:

$searchResult = Book::boolSearch()
    ->must('match_all')
    ->withTrashed()
    ->execute();