dpwilhelmsen / elastic-scout-driver-plus

Extension for Elastic Scout Driver

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool


Extension for Elastic Scout Driver.

Contents

Compatibility

The current version of Elastic Scout Driver Plus has been tested with the following configuration:

  • PHP 7.2-7.4
  • Elasticsearch 7.0-7.6
  • Laravel 6.x-7.x
  • Laravel Scout 7.x-8.x
  • Elastic Scout Driver 1.x

Installation

The library can be installed via Composer:

composer require babenkoivan/elastic-scout-driver-plus

Note, that the library doesn't work without Elastic Scout Driver. If it's not installed yet, please follow the installation steps described here.

Usage

Elastic Scout Driver Plus doesn't replace or modify default search method provided by Laravel Scout. Instead, it introduces a list of advanced query builders, which allow you to make complex search requests.

To get started with the builders you need to add CustomSearch trait to your model:

class MyModel
{
    use \ElasticScoutDriverPlus\CustomSearch;
}

Bool Search

Use boolSearch method to make a bool request:

MyModel::boolSearch()
    // must clause 
    ->must('match', ['title' => 'incredible'])
    ->mustRaw(['match' => ['title' => 'incredible']])
    // must not clause
    ->mustNot('match', ['title' => 'incredible'])
    ->mustNotRaw(['match' => ['title' => 'incredible']])
    // should clause
    ->should('match', ['title' => 'incredible'])
    ->shouldRaw(['match' => ['title' => 'incredible']])
    ->minimumShouldMatch(1)
    // filter clause
    ->filter('term', ['year' => 2020])
    ->filterRaw(['term' => ['year' => 2020]])
    // soft deletes
    ->onlyTrashed()
    ->withTrashed()
    // sorting
    ->sort('year', 'asc')
    ->sortRaw([['year' => 'asc']])
    // highlighting
    ->highlight('title', ['number_of_fragments' => 3])
    ->highlightRaw(['fields' => ['title' => ['number_of_fragments' => 3]]])
    // pagination
    ->from(0)
    ->size(20)
    // execute request and return array result
    ->raw()
    // execute request and return SearchResult instance (see below for more details)
    ->execute();

Raw Search

Use rawSearch method to make a custom search request:

MyModel::rawSearch()
    // raw query
    ->query(['match' => ['title' => 'incredible']])
    // sorting
    ->sort('year', 'asc')
    ->sortRaw([['year' => 'asc']])
    // highlighting
    ->highlight('title', ['number_of_fragments' => 3])
    ->highlightRaw(['fields' => ['title' => ['number_of_fragments' => 3]]])
    // pagination
    ->from(0)
    ->size(20)
    // execute request and return array result
    ->raw()
    // execute request and return SearchResult instance (see below for more details)
    ->execute();

Search Result

Whenever you preform a search request, SearchResult instance is returned:

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

With SearchResult you can quickly get access to collection of models, documents or highlights:

$searchResult->models();
$searchResult->documents();    
$searchResult->highlights();

And of course the total amount of matches:

$searchResult->total();

You can also retrieve a collection of matches, which group related model, document and highlight:

$matches = $searchResult->matches();
$firstMatch = $matches->first();

// model
$model = $firstMatch->model();

// document
$document = $firstMatch->document();
$documentId = $document->getId();
$documentContent = $document->getContent();

// highlight
$highlight = $firstMatch->highlight();
$highlightedSnippets = $highlight->getSnippets('title');
$rawHighlight = $highlight->getRaw();

Note, that models are lazy loaded, which means, that they will be fetched from the database with a single query and only when it's needed.

About

Extension for Elastic Scout Driver


Languages

Language:PHP 95.9%Language:Makefile 4.1%