Skip to content

Latest commit

 

History

History
188 lines (129 loc) · 3.95 KB

search-results.md

File metadata and controls

188 lines (129 loc) · 3.95 KB

Search Results

When your search query is ready to be executed, you have several options:

Raw

You can get a raw response from Elasticsearch:

$raw = Book::searchQuery($query)->raw();

SearchResult

You can execute the query and get Elastic\ScoutDriverPlus\Decorators\SearchResult instance in return:

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

SearchResult provides easy access to:

aggregations

This method returns a collection of aggregations keyed by aggregation name:

$aggregations = $searchResult->aggregations();
$maxPrice = $aggregations->get('max_price');

documents

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

highlights

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

hits

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 more:

$hit = $hits->first();

$indexName = $hit->indexName();
$score = $hit->score();
$model = $hit->model();
$document = $hit->document();
$highlight = $hit->highlight();
$innerHits = $hit->innerHits();
$explanation = $hit->explanation();

Furthermore, you can get a raw representation of the respective hit:

$raw = $hit->raw();

models

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.

suggestions

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

total

This method returns the total number of matching documents:

$total = $searchResult->total();

Pagination

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.