All Projects → sleimanx2 → Plastic

sleimanx2 / Plastic

Licence: mit
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.

Projects that are alternatives of or similar to Plastic

Laravel Scout Elastic
Elastic Driver for Laravel Scout
Stars: ✭ 886 (+79.35%)
Mutual labels:  elasticsearch, laravel
Elastic Scout Driver
Elasticsearch driver for Laravel Scout
Stars: ✭ 74 (-85.02%)
Mutual labels:  elasticsearch, laravel
Kbframe
一款基于Laravel框架开发的现代化二次开发框架,是高性能,高效率,高质量的企业级开发框架,具有驱动领域,敏捷开发,轻易上手,高内聚低耦合,开箱即用等特点。
Stars: ✭ 47 (-90.49%)
Mutual labels:  elasticsearch, laravel
Scout Elasticsearch Driver
This package offers advanced functionality for searching and filtering data in Elasticsearch.
Stars: ✭ 1,047 (+111.94%)
Mutual labels:  elasticsearch, laravel
Candy Api
GetCandy E-Commerce API
Stars: ✭ 339 (-31.38%)
Mutual labels:  elasticsearch, laravel
Laravel Docker Elasticsearch
This is a simple repo for practicing elasticsearch with laravel and docker.
Stars: ✭ 18 (-96.36%)
Mutual labels:  elasticsearch, laravel
Elasticquent
Maps Laravel Eloquent models to Elasticsearch types
Stars: ✭ 1,172 (+137.25%)
Mutual labels:  elasticsearch, laravel
Laravel Elasticsearch
An easy way to use the official Elastic Search client in your Laravel applications.
Stars: ✭ 717 (+45.14%)
Mutual labels:  elasticsearch, laravel
Elasticsearch Eloquent
⚡️ Eloquent models for Elasticsearch.
Stars: ✭ 100 (-79.76%)
Mutual labels:  elasticsearch, laravel
Elasticsearch
Use SQL statements to query elasticsearch
Stars: ✭ 98 (-80.16%)
Mutual labels:  elasticsearch, laravel
Elastic Scout Driver Plus
Extension for Elastic Scout Driver
Stars: ✭ 90 (-81.78%)
Mutual labels:  elasticsearch, laravel
Laravel Scout Elastic Demo
笑来搜原型 Laravel Scout & ElasticSearch ik
Stars: ✭ 403 (-18.42%)
Mutual labels:  elasticsearch, laravel
Elasticsearch
The missing elasticsearch ORM for Laravel, Lumen and Native php applications
Stars: ✭ 375 (-24.09%)
Mutual labels:  elasticsearch, laravel
Laravel Scout Elasticsearch
Search among multiple models with ElasticSearch and Laravel Scout
Stars: ✭ 423 (-14.37%)
Mutual labels:  elasticsearch, laravel
Docker monitoring logging alerting
Docker host and container monitoring, logging and alerting out of the box using cAdvisor, Prometheus, Grafana for monitoring, Elasticsearch, Kibana and Logstash for logging and elastalert and Alertmanager for alerting.
Stars: ✭ 479 (-3.04%)
Mutual labels:  elasticsearch
Ambientum
Docker native solution for running Laravel projects. From Development to Production
Stars: ✭ 487 (-1.42%)
Mutual labels:  laravel
Open Loyalty
Open Loyalty is technology for loyalty solutions for starting new loyalty projects.
Stars: ✭ 476 (-3.64%)
Mutual labels:  elasticsearch
Laravel Money
Laravel Money.
Stars: ✭ 476 (-3.64%)
Mutual labels:  laravel
Laravel Decomposer
⚙️ A Laravel package to decompose your installed packages, their dependencies, your app & server environment
Stars: ✭ 488 (-1.21%)
Mutual labels:  laravel
Smartwiki
因个人精力有限,不在维护此项目,推荐用MinDoc代替
Stars: ✭ 486 (-1.62%)
Mutual labels:  laravel

Plastic Logo

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.

License Build Status StyleCI

This package is still under active development and may change.

For Elasticsearch v2 please refer to version < 0.4.0.

Installing Plastic

composer require sleimanx2/plastic

If you are using Laravel >=5.5 the service provider will be automatically discovered otherwise we need to add the plastic service provider to config/app.php under the providers key:

Sleimanx2\Plastic\PlasticServiceProvider::class

Finally we need to run:

php artisan vendor:publish

This will create a config file at config/plastic.php and a mapping directory at database/mappings.

Usage

Defining Searchable Models

To get started, enable searching capabilities in your model by adding the Sleimanx2\Plastic\Searchable trait:

use Sleimanx2\Plastic\Searchable;

class Book extends Model
{
    use Searchable;
}

Defining what data to store.

By default, Plastic will store all visible properties of your model, using $model->toArray().

In addition, Plastic provides you with two ways to manually specify which attributes/relations should be stored in Elasticsearch.

1 - Providing a searchable property to our model

public $searchable = ['id', 'name', 'body', 'tags', 'images'];

2 - Providing a buildDocument method

public function buildDocument()
{
    return [
        'id' => $this->id,
        'tags' => $this->tags
    ];
}

Custom elastic type name

By the default Plastic will use the model table name as the model type. You can customize it by adding a $documentType property to your model:

public $documentType = 'custom_type';

Custom elastic index name

By the default Plastic will use the index defined in the configuration file. You can customize in which index your model data will be stored by setting the $documentIndex property to your model:

public $documentIndex = 'custom_index';

Storing Model Content

Plastic automatically syncs model data with elastic when you save or delete your model from our SQL DB, however this feature can be disabled by adding public $syncDocument = false to your model.

Its important to note that manual document update should be performed in multiple scenarios:

1 - When you perform a bulk update or delete, no Eloquent event is triggered, therefore the document data won't be synced.

2 - Plastic doesn't listen to related models events (yet), so when you update a related model's content you should consider updating the parent document.

Saving a document

$book = Book::first()->document()->save();

Partial updating a document

$book = Book::first()->document()->update();

Deleting a document

$book = Book::first()->document()->delete();

Saving documents in bulk

Plastic::persist()->bulkSave(Tag::find(1)->books);

Deleting documents in bulk

$authors = Author::where('age','>',25)->get();

Plastic::persist()->bulkDelete($authors);

Searching Model Content

Plastic provides a fluent syntax to query Elasticsearch which leads to compact readable code. Lets dig into it:

$result = Book::search()->match('title','pulp')->get();

// Returns a collection of Book Models
$books = $result->hits();

// Returns the total number of matched documents
$result->totalHits();

// Returns the highest query score
$result->maxScore();

//Returns the time needed to execute the query
$result->took();

To get the raw DSL query that will be executed you can call toDSL():

$dsl = Book::search()->match('title','pulp')->toDSL();

Pagination

$books = Book::search()
    ->multiMatch(['title', 'description'], 'ham on rye', ['fuzziness' => 'AUTO'])
    ->sortBy('date')
    ->paginate();

You can still access the result object after pagination using the result method:

$books->result();

Bool Query

User::search()
    ->must()
        ->term('name','kimchy')
    ->mustNot()
        ->range('age',['from'=>10,'to'=>20])
    ->should()
        ->match('bio','developer')
        ->match('bio','elastic')
    ->filter()
        ->term('tag','tech')
    ->get();

Nested Query

$contain = 'foo';

Post::search()
    ->multiMatch(['title', 'body'], $contain)
    ->nested('tags', function (SearchBuilder $builder) use ($contain) {
        $builder->match('tags.name', $contain);
    })->get();

Check out this documentation of supported search queries within Plastic and how to apply unsupported queries.

Change index on the fly

To switch to a different index for a single query, simply use the index method.

$result = Book::search()->index('special-books')->match('title','pulp')->get();

Aggregation

$result = User::search()
    ->match('bio', 'elastic')
    ->aggregate(function (AggregationBuilder $builder) {
        $builder->average('average_age', 'age');
    })->get();

$aggregations = $result->aggregations();

Check out this documentation of supported aggregations within plastic and how to apply unsupported aggregations.

Suggestions

Plastic::suggest()->completion('tag_suggest', 'photo')->get();

The suggestions query builder can also be accessed directly from the model as well:

//this be handy if you have a custom index for your model
Tag::suggest()->term('tag_term','admin')->get();

Model Mapping

Mappings are an important aspect of Elasticsearch. You can compare them to indexing in SQL databases. Mapping your models yields better and more efficient search results, and allows us to use some special query functions like nested fields and suggestions.

Generate a Model Mapping

php artisan make:mapping "App\User"

The new mapping will be placed in your database/mappings directory.

Mapping Structure

A mapping class contains a single method map. The map method is used to map the given model fields.

Within the map method you may use the Plastic Map builder to expressively create field maps. For example, let's look at a sample mapping that creates a Tag model map:

use Sleimanx2\Plastic\Map\Blueprint;
use Sleimanx2\Plastic\Mappings\Mapping;

class AppTag extends Mapping
{
    /**
     * Full name of the model that should be mapped
     *
     * @var string
     */
    protected $model = App\Tag::class;

    /**
     * Run the mapping.
     *
     * @return void
     */
    public function map()
    {
        Map::create($this->getModelType(), function (Blueprint $map) {
            $map->string('name')->store('true')->index('analyzed');

            // instead of the fluent syntax we can use the second method argument to fill the attributes
            $map->completion('suggestion', ['analyzer' => 'simple', 'search_analyzer' => 'simple']);
        },$this->getModelIndex());
    }
}

To learn about all of the methods available on the Map builder, check out this documentation.

Run Mappings

Running the created mappings can be done using the Artisan console command:

php artisan mapping:run

Updating Mappings

If your update consists only of adding a new field mapping you can always update our model map with your new field and run:

php artisan mapping:rerun

The mapping for existing fields cannot be updated or deleted, so you'll need to use one of following techniques to update existing fields.

1 - Create a new index

You can always create a new Elasticsearch index and re-run the mappings. After running the mappings you can use the bulkSave method to sync your SQL data with Elasticsearch.

2 - Using aliases

Its recommended to create your Elasticsearch index with an alias to ease the process of updating your model mappings with zero downtime. To learn more check out:

https://www.elastic.co/blog/changing-mapping-with-zero-downtime

Populate An Index

Populating an index with searchable models can be done by running an Artisan console command :

php artisan plastic:populate [--mappings][--index=...][--database=...]
  • --mappings Create the models mappings before populating the index
  • --database=... Database connection to use for mappings instead of the default one
  • --index=... Index to populate instead of the default one

The list of models from which to recreate the documents has to be configured per index in config/plastic.php:

    'populate' => [
        'models' => [
            // Models for the default index
            env('PLASTIC_INDEX', 'plastic') => [
                App\Models\Article::class,
                App\Models\Page::class,
            ],
            // Models for the index "another_index"
            'another_index' => [
                App\Models\User::class,
            ],
        ],
    ],

Access The Client

You can access the Elasticsearch client to manage your indices and aliases as follows:

$client = Plastic::getClient();

//index delete
$client->indices()->delete(['index'=> Plastic::getDefaultIndex()]);
//index create
$client->indices()->create(['index' => Plastic::getDefaultIndex()]);

More about the official elastic client : https://github.com/elastic/elasticsearch-php

Contributing

Thank you for contributing, The contribution guide can be found Here.

License

Plastic is open-sourced software licensed under the MIT license.

To Do

Search Query Builder

  • [ ] implement Boosting query
  • [ ] implement ConstantScore query
  • [ ] implement DisMaxQuery query
  • [ ] implement MoreLikeThis query (with raw eloquent models)
  • [ ] implement GeoShape query

Aggregation Query Builder

  • [ ] implement Nested aggregation
  • [ ] implement ExtendedStats aggregation
  • [ ] implement TopHits aggregation

Mapping

  • [ ] Find a seamless way to update field mappings with zero downtime with aliases

General

  • [ ] Better query builder documentation
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].