Laravel 8.* Elasticsearch Eloquent

Related tags

Search Elasticsearch
Overview

Elasticsearch

Installation

composer require etsetra/elasticsearch
  1. Create config file

    $ php artisan vendor:publish --tag="etsetra-elasticsearch-config"

  2. Update .env file

    ELASTICSEARCH_SERVERS=127.0.0.1:9200,127.0.0.1:9201,127.0.0.1:9202

    ELASTICSEARCH_RETRIES=2

    ELASTICSEARCH_PASSWORD=1234 //This password is unique to you. Used to delete index

    ELASTICSEARCH_PREFIX=app //prefix of indexes

Model & Migration

$ php artisan elasticsearch:model MyModel

// Model created successfully.

or with migration

$ php artisan elasticsearch:model MyModel --m

// Model created successfully.

// Created Migration: 2021_09_25_151308_create_my_model_table

$ php artisan migrate

You can enter standard elasticsearch mapping parameters into the created migration file.

Delete Index

$ php artisan elasticsearch:index:delete

resim

The password is the ELASTICSEARCH_PASSWORD value in the env file.

Put Mapping

use App\Models\MyModel;

(new MyModel)->putIndexMapping(
    [
        'new_column_name' => [
            'type' => 'keyword'
        ]
    ]
);

Search Document

use App\Models\MyModel;

$data = (new MyModel)->find(
    [
        'bool' => [
            'filter' => [
                'terms' => [
                    'user_id' => [ 1 ]
                ]
            ]
        ]
    ],
    [
        'from' => 0,
        'size' => 10,
        'sort' => [
            [
                'created_at' => [
                    'order' => 'desc'
                ]
            ]
        ]
    ]
);

// You can use all parameters of Elasticsearch.

// Results
stdClass Object
(
    [success] => ok
    [source] => Array
        (
            [0] => Array
                (
                    [id] => J6SzTtlUFpTQ
                    [user_id] => 1
                    [description] => 3 - Dolor egestas velit ligula nunc tortor ultricies quam consequat hac inceptos congue ullamcorper nisl.
                    [created_at] => 2021-09-25T15:23:25+00:00
                    [lang] => en
                )

            [1] => Array
                (
                    [id] => tky4EtIvlp1b
                    [user_id] => 1
                    [description] => Suspendisse ante commodo duis dignissim, elit mi orci vulputate hac curabitur duis dignissim
                    [created_at] => 2021-09-25T15:23:25+00:00
                    [lang] => en
                )
        )

    [aggregations] => Array
        (
        )

    [stats] => Array
        (
            [total] => 2
        )
)

Create & Update Document

use App\Models\MyModel;

// Create document
$create = (new MyModel)->create(
    [
        'id' => 'abcd1234',
        'user_id' => 1,
        'description' => 'Lorem ipsum...',
        'created_at' => '2021-09-25T15:23:25+00:00',
        'lang' => 'en',
    ],
    false, // upsert (bool, default = false)
    false, // should queue (bool, default = false)
);

// Update document
$update = (new MyModel)->update(
    'my_doc_id',
    [
        'description' => 'Lorem ipsum text...',
    ],
    false, // should queue (bool, default = false)
);

// Update by script
$update = (new MyModel)->script(
    'my_doc_id',
    [
        'ctx._source.views = 0;',
    ],
    false, // should queue (bool, default = false)
);

Delete Document

use App\Models\MyModel;

$delete = (new MyModel)->delete(
    'my_doc_id',
    false, // should queue (bool, default = false)
);

// Delete by Query
$items = (new MyModel)->deleteByQuery(
    [
      'bool' => [
        'must' => [
          [
            'match' => [ 'user_id' => 1 ]
          ]
        ]
      ]
    ],
    true // shouldQueue
);

Get Document

use App\Models\MyModel;

$item = (new MyModel)->get('my_doc_id');

// Results
stdClass Object
(
    [success] => ok
    [source] => stdClass Object
        (
            [id] => J6SzTtlUFpTQFUm5LABP
            [user_id] => 1
            [description] => 'Lorem text...',
            [likes] => 0
            [created_at] => 2021-09-25T15:23:25+00:00
        )

)

Bulk Actions

use Etsetra\Elasticsearch\Console\BulkApi;

BulkApi::chunk(
  'my_model', // index name
  'J6SzTtlUFpTQ', // doc id
  [ 'ctx._source.views += 1' ], // doc body
  'script' // action type
);

// Action Types;
// script: elasticsearch java scripts,
// index: upsert document,
// create: create document,

// Alternate
BulkApi::chunk(
  'my_model',
  'J6SzTtlUFpTQ',
  [
    'user_id' => 2,
    'video_id' => 'dummy1234'
  ],
  'index' // action type
);
You might also like...
This modules provides a Search API Backend for Elasticsearch.

Search API ElasticSearch This modules provides a Search API Backend for Elasticsearch. This module uses the official Elasticsearch PHP Client. Feature

Search products, categories, brands or tags with ElasticSearch

ElasticSearch for Shopaholic This plugin allows you to use ElasticSearch as search engine for Shopaholic. Benefits Easy to install, easy to use Opened

Query Builder for Elasticsearch

Query Builder for Elasticsearch

Laravel Scout provides a driver based solution to searching your Eloquent models.

Introduction Laravel Scout provides a simple, driver-based solution for adding full-text search to your Eloquent models. Once Scout is installed and c

Laravel package to search through multiple Eloquent models. Supports sorting, pagination, scoped queries, eager load relationships and searching through single or multiple columns.

Laravel Cross Eloquent Search This Laravel package allows you to search through multiple Eloquent models. It supports sorting, pagination, scoped quer

[Deprecated] We now recommend using Laravel Scout, see =

[DEPRECATED] Algolia Search API Client for Laravel Algolia Search is a hosted full-text, numerical, and faceted search engine capable of delivering re

A search package for Laravel 5.

Search Package for Laravel 5 This package provides a unified API across a variety of different full text search services. It currently supports driver

A php trait to search laravel models

Searchable, a search trait for Laravel Searchable is a trait for Laravel 4.2+ and Laravel 5.0 that adds a simple search function to Eloquent Models. S

Driver for Laravel Scout search package based on https://github.com/teamtnt/tntsearch
Driver for Laravel Scout search package based on https://github.com/teamtnt/tntsearch

TNTSearch Driver for Laravel Scout - Laravel 5.3 - 8.0 This package makes it easy to add full text search support to your models with Laravel 5.3 to 8

Releases(1.0.7)
Owner
Etsetra
Etsetra
Laravel 8.* Elasticsearch Eloquent

Elasticsearch Installation composer require etsetra/elasticsearch Create config file $ php artisan vendor:publish --tag="etsetra-elasticsearch-config

Etsetra 2 Jan 14, 2022
Elasticsearch driver for Laravel Scout

Elasticsearch driver for Laravel Scout. Contents Compatibility Installation Configuration Basic Usage Advanced Search Migrations Pitfalls Compatibilit

Ivan Babenko 197 Dec 19, 2022
Elasticsearch migrations for Laravel

Elastic Migrations Elasticsearch migrations for Laravel allow you to easily modify and share indices schema across the application's environments. Con

Ivan Babenko 151 Dec 20, 2022
Search among multiple models with ElasticSearch and Laravel Scout

For PHP8 support use php8 branch For Laravel Framework < 6.0.0 use 3.x branch The package provides the perfect starting point to integrate ElasticSear

Sergey Shlyakhov 592 Dec 25, 2022
Official PHP low-level client for Elasticsearch.

elasticsearch-php Official low-level client for Elasticsearch. Its goal is to provide common ground for all Elasticsearch-related code in PHP; because

elastic 5k Dec 31, 2022
Elastica is a PHP client for elasticsearch

Elastica: elasticsearch PHP Client All documentation for Elastica can be found under Elastica.io. If you have questions, don't hesitate to ask them on

Nicolas Ruflin 2.2k Dec 23, 2022
Store and retrieve objects from Algolia or Elasticsearch

Store and retrieve objects from a search index This is an opinionated Laravel 5.1 package to store and retrieve objects from a search index. Currently

Spatie 440 Dec 30, 2022
Build and execute an Elasticsearch search query using a fluent PHP API

PACKAGE IN DEVELOPMENT, DO NOT USE YET Build and execute ElasticSearch queries using a fluent PHP API This package is a lightweight query builder for

Spatie 94 Dec 14, 2022
This package offers advanced functionality for searching and filtering data in Elasticsearch.

Scout Elasticsearch Driver ?? Introducing a new Elasticsearch ecosystem for Laravel. ?? This package offers advanced functionality for searching and f

Ivan Babenko 1.2k Dec 20, 2022
Official PHP low-level client for Elasticsearch.

elasticsearch-php Official low-level client for Elasticsearch. Its goal is to provide common ground for all Elasticsearch-related code in PHP; because

elastic 5k Jan 1, 2023