When your search query is ready to be executed, you have several options:
You can get a raw response from Elasticsearch:
$raw = Book::searchQuery($query)->raw();
You can execute the query and get Elastic\ScoutDriverPlus\Decorators\SearchResult
instance in return:
$searchResult = Book::searchQuery($query)->execute();
SearchResult
provides easy access to:
This method returns a collection of aggregations keyed by aggregation name:
$aggregations = $searchResult->aggregations();
$maxPrice = $aggregations->get('max_price');
documents
returns a collection of matching documents:
$documents = $searchResult->documents();
Every document has an id and content:
$document = $documents->first();
$id = $document->id();
$content = $document->content();
This method returns a collection of highlights:
$highlights = $searchResult->highlights();
You can use snippets
to get highlighted snippets for the given field:
$highlight = $highlights->first();
$snippets = $highlight->snippets('title');
You can retrieve a collection of hits:
$hits = $searchResult->hits();
Each hit provides access to the related index name, the score, the model, the document, the highlight and the inner hits:
$hit = $hits->first();
$indexName = $hit->indexName();
$score = $hit->score();
$model = $hit->model();
$document = $hit->document();
$highlight = $hit->highlight();
$innerHits = $hit->innerHits();
Furthermore, you can get a raw representation of the respective hit:
$raw = $hit->raw();
You can use models
to retrieve a collection of matching models:
$models = $searchResult->models();
Note, that models are lazy loaded. They are fetched from the database with a single query and only when you request them.
This method returns a collection of suggestions keyed by suggestion name:
$suggestions = $searchResult->suggestions();
$titleSuggestions = $suggestions->get('title_suggest');
Each suggestion includes a suggestion text, an offset, a length and an arbitrary number of options:
$firstSuggestion = $titleSuggestions->first();
$text = $firstSuggestion->text();
$offset = $firstSuggestion->offset();
$length = $firstSuggestion->length();
$options = $firstSuggestion->options();
You can also resolve the related models when the completion suggester is used:
$models = $firstSuggestion->models();
This method returns the total number of matching documents:
$total = $searchResult->total();
Finally, you can paginate search results:
$paginator = Book::searchQuery($query)->paginate(10);
The paginator provides the same interface as SearchResult
, which means that you can access models, highlights, etc.:
$models = $paginator->models();
However, Elastic Scout Driver Plus by default paginates hits and not models, this behaviour can be changed:
// paginate hits
$paginator = Book::searchQuery($query)
->paginate(10);
foreach ($paginator as $hit) {
$model = $hit->model();
}
// paginate models
$paginator = Book::searchQuery($query)
->paginate(10)
->onlyModels();
foreach ($paginator as $model) {
$id = $model->id;
}
// paginated documents
$paginator = Book::searchQuery($query)
->paginate(10)
->onlyDocuments();
foreach ($paginator as $document) {
$id = $document->id();
}
Note that from and size are ignored when paginating search results.