- aggregate
- boostIndex
- collapse
- from
- highlight
- join
- load
- minScore
- postFilter
- size
- sort
- refineModels
- rescore
- source
- suggest
- trackScores
- trackTotalHits
- when
This method can be used to aggregate data based on a search query;
$searchResult = Book::searchQuery($query)
->aggregate('max_price', [
'max' => [
'field' => 'price',
],
])
->execute();
Alternatively you can use the aggregateRaw
method:
$searchResult = Book::searchQuery($query)
->aggregateRaw([
'max_price' => [
'max' => [
'field' => 'price',
],
],
])
->execute();
You can retrieve the aggregated data from the search result as follows:
$aggregations = $searchResult->aggregations();
$maxPrice = $aggregations->get('max_price');
When searching in multiple indices, you can boost results from a specific index:
$searchResult = Author::searchQuery($query)
->join(Book::class)
->boostIndex(Book::class, 2)
->execute();
This method allows to collapse search results based on field values:
$searchResult = Book::searchQuery($query)
->collapse('author_id')
->sort('published', 'desc')
->execute();
There is also the collapseRaw
method at your disposal:
$searchResult = Book::searchQuery($query)
->collapseRaw(['field' => 'author_id'])
->sort('price', 'asc')
->execute();
from
defines the starting document offset:
$searchResult = Book::searchQuery($query)
->from(5)
->execute();
This method allows you to get highlighted snippets from one or more fields in your search results:
$searchResult = Book::searchQuery($query)
->highlight('title')
->execute();
Use highlightRaw
method if you need more control:
$searchResult = Book::searchQuery($query)
->highlightRaw(['fields' => ['title' => ['number_of_fragments' => 3]]])
->execute();
Use highlights
method to retrieve all highlights from the search result:
$highlights = $searchResult->highlights();
You can also get a highlight for every hit:
$hits = $searchResult->hits();
$highlight = $hits->first()->highlight();
The highlighted snippets can be retrieved as follows:
$snippets = $highlight->snippets('title');
It is also possible to get a raw highlight:
$raw = $highlight->raw();
This method enables multi indices search:
$query = Query::bool()
->should(Query::match()->field('name')->query('John'))
->should(Query::match()->field('title')->query('The Book'))
->minimumShouldMatch(1);
$searchResult = Author::searchQuery($query)
->join(Book::class)
->execute();
In the example above, we search for an author with name John
or a book with title The Book
in two different indices.
It does not matter if we start the query from Book
or Author
model. Remember though, that the result collection of models
includes both types in this case:
// every model is either Author or Book
$models = $searchResult->models();
This method allows you to eager load model relations:
$searchResult = Book::searchQuery($query)
->load(['author'])
->execute();
When searching in multiple indices, you need to explicitly define the model you want the relations for:
$searchResult = Book::searchQuery($query)
->join(Author::class)
->load(['author'], Book::class)
->load(['books'], Author::class)
->execute();
This method allows you to set minimum score for matching documents:
$searchResult = Book::searchQuery($query)
->minScore(0.5)
->execute();
postFilter
is used to filter search results:
$postFilter = Query::term()
->field('published')
->value('2020-06-07');
$searchResult = Book::searchQuery($query)
->postFilter($postFilter)
->execute();
You can also provide a raw query in the postFilter
method:
$searchResult = Book::searchQuery($query)
->postFilter(['term' => ['published' => '2020-06-07']])
->execute();
size
method limits the number of hits to return:
$searchResult = Book::searchQuery($query)
->size(2)
->execute();
This method sorts the search results:
$searchResult = Book::searchQuery($query)
->sort('price', 'asc')
->execute();
In case you need more advanced sorting algorithm use sortRaw
:
$searchResult = Book::searchQuery($query)
->sortRaw([['price' => 'asc'], ['published' => 'asc']])
->execute();
This method allows you to rescore
the search results. In addition, you can also use rescoreWeights
and rescoreWindowSize
to set query_weight
,
rescore_query_weight
and window_size
:
$searchResult = Book::searchQuery($query)
->rescore('match_phrase', [
'message' => [
'query' => 'the quick brown',
'slop' => 2,
],
])
->rescoreWeights(0.7, 1.2)
->rescoreWindowSize(10)
->execute();
Alternatively you can use rescoreRaw
:
$searchResult = Book::searchQuery($query)
->rescoreRaw([
'window_size' => 50,
'query' => [
'rescore_query' => [
'match_phrase' => [
'message' => [
'query' => 'the quick brown',
'slop' => 2,
],
],
],
'query_weight' => 0.7,
'rescore_query_weight' => 1.2,
]
])
->execute();
This method allows you to set the callback where you can modify the database query.
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
$models = Book::searchQuery($query)
->refineModels(function (EloquentBuilder $query) {
$query->select(['id', 'title', 'description']);
})
->execute()
->models();
When searching in multiple indices, you need to explicitly define the model for which you want to set the callback:
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
$models = Book::searchQuery($query)
->join(Author::class)
->refineModels(function (EloquentBuilder $query) {
$query->select(['id', 'title', 'description']);
}, Book::class)
->refineModels(function (EloquentBuilder $query) {
$query->select(['id', 'name', 'last_name']);
}, Author::class)
->execute()
->models();
This method allows you to select what document fields of the source are returned:
$searchResult = Book::searchQuery($query)
->source(['title', 'description'])
->execute();
sourceRaw
allows you to use a single wildcard pattern, an array of fields or a boolean value in case you want to
exclude document source from the result:
$searchResult = Book::searchQuery($query)
->sourceRaw(false)
->execute();
This method can be used to get similar looking terms based on the provided text:
$searchResult = Book::searchQuery(Query::matchNone())
->suggest('title_suggest', ['text' => 'book', 'term' => ['field' => 'title']])
->execute();
The same query with suggestRaw
method:
$searchResult = Book::searchQuery(Query::matchNone())
->suggestRaw(['title_suggest' => ['text' => 'book', 'term' => ['field' => 'title']]])
->execute();
You can use the suggestions
method to retrieve suggestions from the search result:
$suggestions = $searchResult->suggestions();
Each key of this collection is a suggestion name, each element is a collection of suggested terms:
$titleSuggestions = $suggestions->get('title_suggest');
Each suggestion contains various information about the term:
$firstSuggestion = $titleSuggestions->first();
// the suggestion text
$text = $firstSuggestion->text();
// the start offset and the length of the suggested text
$offset = $firstSuggestion->offset();
$length = $firstSuggestion->length();
// an arbitrary number of options
$options = $firstSuggestion->options();
// an array representation of the suggestion
$raw = $firstSuggestion->raw();
This method forces scores to be computed and tracked:
$searchResult = Book::searchQuery($query)
->trackScores(true)
->execute();
This method allows you to control how the total number of hits should be tracked:
$searchResult = Book::searchQuery($query)
->trackTotalHits(true)
->execute();
This method can be used to apply certain clauses based on another condition:
$searchResult = Book::searchQuery($query)
->when($orderBy, function ($builder, $orderBy) {
return $builder->sort($orderBy, 'asc');
})
->execute();
You may also pass another closure as a third argument to the when
method. This closure will be only executed
if the first argument evaluates as false
:
$searchResult = Book::searchQuery($query)
->when($orderBy, function ($builder, $orderBy) {
return $builder->sort($orderBy, 'asc');
}, function ($builder) {
return $builder->sort('price', 'asc');
})
->execute();