All Projects → matchish → Laravel Scout Elasticsearch

matchish / Laravel Scout Elasticsearch

Licence: mit
Search among multiple models with ElasticSearch and Laravel Scout

Projects that are alternatives of or similar to Laravel Scout Elasticsearch

Laravel Docker Elasticsearch
This is a simple repo for practicing elasticsearch with laravel and docker.
Stars: ✭ 18 (-95.74%)
Mutual labels:  elasticsearch, laravel
Elasticquent
Maps Laravel Eloquent models to Elasticsearch types
Stars: ✭ 1,172 (+177.07%)
Mutual labels:  elasticsearch, laravel
Laravel Scout Elastic
Elastic Driver for Laravel Scout
Stars: ✭ 886 (+109.46%)
Mutual labels:  elasticsearch, laravel
Kbframe
一款基于Laravel框架开发的现代化二次开发框架,是高性能,高效率,高质量的企业级开发框架,具有驱动领域,敏捷开发,轻易上手,高内聚低耦合,开箱即用等特点。
Stars: ✭ 47 (-88.89%)
Mutual labels:  elasticsearch, laravel
Elasticsearch Eloquent
⚡️ Eloquent models for Elasticsearch.
Stars: ✭ 100 (-76.36%)
Mutual labels:  elasticsearch, laravel
Laravel Elasticsearch
An easy way to use the official Elastic Search client in your Laravel applications.
Stars: ✭ 717 (+69.5%)
Mutual labels:  elasticsearch, laravel
Scout Elasticsearch Driver
This package offers advanced functionality for searching and filtering data in Elasticsearch.
Stars: ✭ 1,047 (+147.52%)
Mutual labels:  elasticsearch, laravel
Plastic
Plastic is an Elasticsearch ODM and mapper for Laravel. It renders the developer experience more enjoyable while using Elasticsearch, by providing a fluent syntax for mapping, querying, and storing eloquent models.
Stars: ✭ 494 (+16.78%)
Mutual labels:  elasticsearch, laravel
Elasticsearch
Use SQL statements to query elasticsearch
Stars: ✭ 98 (-76.83%)
Mutual labels:  elasticsearch, laravel
Elastic Scout Driver Plus
Extension for Elastic Scout Driver
Stars: ✭ 90 (-78.72%)
Mutual labels:  elasticsearch, laravel
Elastic Scout Driver
Elasticsearch driver for Laravel Scout
Stars: ✭ 74 (-82.51%)
Mutual labels:  elasticsearch, laravel
Elasticsearch
The missing elasticsearch ORM for Laravel, Lumen and Native php applications
Stars: ✭ 375 (-11.35%)
Mutual labels:  elasticsearch, laravel
Candy Api
GetCandy E-Commerce API
Stars: ✭ 339 (-19.86%)
Mutual labels:  elasticsearch, laravel
Laravel Scout Elastic Demo
笑来搜原型 Laravel Scout & ElasticSearch ik
Stars: ✭ 403 (-4.73%)
Mutual labels:  elasticsearch, laravel
Bladeone
The standalone version Blade Template Engine without Laravel in a single php file and without dependencies
Stars: ✭ 411 (-2.84%)
Mutual labels:  laravel
Laravel Migrations Generator
Laravel Migrations Generator: Automatically generate your migrations from an existing database schema.
Stars: ✭ 417 (-1.42%)
Mutual labels:  laravel
Laravel Model Settings
Model Settings for your Laravel app
Stars: ✭ 409 (-3.31%)
Mutual labels:  laravel
Pusher Http Laravel
[DEPRECATED] A Pusher Channels bridge for Laravel
Stars: ✭ 410 (-3.07%)
Mutual labels:  laravel
Airflix
🍿 An AirPlay friendly web interface to stream your movies and TV shows from a home server.
Stars: ✭ 420 (-0.71%)
Mutual labels:  laravel
Docker Laravel
🐳 Build a simple laravel development environment with docker-compose.
Stars: ✭ 415 (-1.89%)
Mutual labels:  laravel

Scout ElasticSearch Import progress report

Build Status Code quality Coverage Total Downloads Latest Version License

For Laravel Framework < 6.0.0 use 3.x branch

The package provides the perfect starting point to integrate ElasticSearch into your Laravel application. It is carefully crafted to simplify the usage of ElasticSearch within the Laravel Framework.

It’s built on top of the latest release of Laravel Scout, the official Laravel search package. Using this package, you are free to take advantage of all of Laravel Scout’s great features, and at the same time leverage the complete set of ElasticSearch’s search experience.

If you need any help, stack overflow is the preferred and recommended way to ask support questions.

💕 Features

Don't forget to ⭐️ the package if you like it. 🙏

⚠️ Requirements

  • PHP version >= 7.3
  • Laravel Framework version >= 6.0.0
Elasticsearch version ElasticsearchDSL version
>= 7.0 >= 3.0.0
>= 6.0, < 7.0 < 3.0.0

🚀 Installation

Use composer to install the package:

composer require matchish/laravel-scout-elasticsearch

Set env variables

SCOUT_DRIVER=Matchish\ScoutElasticSearch\Engines\ElasticSearchEngine

The package uses \ElasticSearch\Client from official package, but does not try to configure it, so feel free do it in your app service provider. But if you don't want to do it right now, you can use Matchish\ElasticSearchServiceProvider from the package.
Register the provider, adding to config/app.php

'providers' => [
    // Other Service Providers

    \Matchish\ScoutElasticSearch\ElasticSearchServiceProvider::class
],

Set ELASTICSEARCH_HOST env variable

ELASTICSEARCH_HOST=host:port

And publish config example for elasticsearch
php artisan vendor:publish --tag config

💡 Usage

Note: This package adds functionalities to Laravel Scout, and for this reason, we encourage you to read the Scout documentation first. Documentation for Scout can be found on the Laravel website.

Index settings and mappings

It is very important to define the mapping when we create an index — an inappropriate preliminary definition and mapping may result in the wrong search results.

To define mappings or settings for index, set config with right value.

For example if method searchableAs returns products string

Config key for mappings should be
elasticsearch.indices.mappings.products
Or you you can specify default mappings with config key elasticsearch.indices.mappings.default

Same way you can define settings

For index products it will be
elasticsearch.indices.settings.products

And for default settings
elasticsearch.indices.settings.default

Eager load

To speed up import you can eager load relations on import using global scopes.

You should configure ImportSourceFactory in your service provider(register method)

use Matchish\ScoutElasticSearch\Searchable\ImportSourceFactory;
...
public function register(): void
{
$this->app->bind(ImportSourceFactory::class, MyImportSourceFactory::class);

Here is an example of MyImportSourceFactory

namespace Matchish\ScoutElasticSearch\Searchable;

final class MyImportSourceFactory implements ImportSourceFactory
{
    public static function from(string $className): ImportSource
    {
        //Add all required scopes
        return new DefaultImportSource($className, [new WithCommentsScope()]);
    }
}

class WithCommentsScope implements Scope {

    /**
     * Apply the scope to a given Eloquent query builder.
     *
     * @param \Illuminate\Database\Eloquent\Builder $builder
     * @param \Illuminate\Database\Eloquent\Model $model
     * @return void
     */
    public function apply(Builder $builder, Model $model)
    {
        $builder->with('comments');
    }
}

Zero downtime reimport

While working in production, to keep your existing search experience available while reimporting your data, you also can use scout:import Artisan command:

php artisan scout:import

The command create new temporary index, import all models to it, and then switch to the index and remove old index.

Search

To be fully compatible with original scout package, this package does not add new methods.
So how we can build complex queries? There is two ways.
By default, when you pass a query to the search method, the engine builds a query_string query, so you can build queries like this

Product::search('title:this OR description:this) AND (title:that OR description:that')`

If it's not enough in your case you can pass a callback to the query builder

$results = Product::search('zonga', function($client, $body) {

    $minPriceAggregation = new MinAggregation('min_price');
    $minPriceAggregation->setField('price');
    
    $maxPriceAggregation = new MaxAggregation('max_price');
    $maxPriceAggregation->setField('price');
    
    $brandTermAggregation = new TermsAggregation('brand');
    $brandTermAggregation->setField('brand');

    $body->addAggregation($minPriceAggregation);
    $body->addAggregation($brandTermAggregation);
    
    return $client->search(['index' => 'products', 'body' => $body->toArray()]);
})->raw();

$client is \ElasticSearch\Client object from elasticsearch/elasticsearch package
And $body is ONGR\ElasticsearchDSL\Search from ongr/elasticsearch-dsl package

Search amongst multiple models

You can do it with Mixed class, just pass indices names separated by commas to the within method.

Mixed::search('title:Barcelona or to:Barcelona')
    within(implode(',', [
        (new Ticket())->searchableAs(),
        (new Book())->searchableAs(),
    ]))
->get();

In this example you will get collection of Ticket and Book models where ticket's arrival city or book title is Barcelona

Working with results

Often your response isn't collection of models but aggregations or models with higlights an so on. In this case you need to implement your own implementation of HitsIteratorAggregate and bind it in your service provider

Here is a case

🆓 License

Scout ElasticSearch is an open-sourced software licensed under the MIT license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].