Skip to content

Latest commit

 

History

History
205 lines (150 loc) · 4.48 KB

compound-queries.md

File metadata and controls

205 lines (150 loc) · 4.48 KB

Compound Queries

Boolean

You can use OpenSearch\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:

filter

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();

minimumShouldMatch

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();

must

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();

mustNot

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();

onlyTrashed

Use onlyTrashed method to get only soft deleted results:

$query = Query::bool()
    ->must($must)
    ->onlyTrashed();

$searchResult = Book::searchQuery($query)->execute();

should

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();

withTrashed

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

$query = Query::bool()
    ->must($must)
    ->withTrashed();

$searchResult = Book::searchQuery($query)->execute();