Download the PHP package mawebcoder/laravel-elasticsearch without Composer

On this page you can find all versions of the php package mawebcoder/laravel-elasticsearch. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-elasticsearch

laravel-elasticsearch (Elastiquent-PHP)

By using an Eloquent-like query builder, you can tame the beast without having to worry about Elasticsearch's monstrous syntax.

You can use this package without needing to master Elasticsearch deeply and save your time by not having to memorize the higherarchical nested syntax.

Dependencies

Elasticsearch Version Package Version PHP Version Laravel
7.17.9 2.6.3 and lower ^8.1 ^9.0
^8.0 ^3.0 ^8.1 ^9.0

Config file and Elasticsearch migration log

In order to be able to save logs related to migrations and customize your configurations, you need to run command below:

php artisan vendor:publish --tag=elastic

Then migrate your database :

php artisan migrate

Config

After publishing the config file, you will see the following content

ORM

This package has added ORM functionality to make it easier to work with documentation, just like what we see in Laravel.We will get to know more as we go forward.Let's dive into it

Models

to be able to have a more effective relationship with our documents, we need to have a model for each index. Models similar to what we see in Laravel greatly simplify the work of communicating with the database.

In order to create a Model:

php artisan elastic:make-model <model-name>

By default, your models base path is in app/Elasticsearch/Models directory, But you can define your own base path in config/elasticsearch.php file.

All your models must inherit from the BaseElasticsearchModel class. This class is an abstract class that enforce you to implement the getIndex method that returns the index name of model.

We use the return value of this method to create the index you want in migrations.

If you want to get your index name with the prefix that you defined in config file:

Migrations

As you may know, Elasticsearch uses mappings for the structure of its documents, which may seem a little difficult to create in raw form. In order to simplify this process, we use migrations to make this process easier. After defining the model, you have to create a migration to register your desired fields.All your migrations must inherit from the BaseElasticMigration abstract class.

To Create a new Migration:

php artisan elastic:make-migration <migration-name>

By default, your migrations base path is in app/Elasticsearch/Migrations directory, but you can define your own base path in config/elasticsearch.php file.

Unfortunately, the package cannot automatically find the path of your migrations. To introduce the path of migrations,put the sample code below in one of your providers:

To see migrations states :

php artisan elastic:migrate-status

To migrate migrations and create your indices mappings :

php artisan elastic:migrate

To reset all migrations(this command just runs down method in all migrations) :

php artisan elstic:migrate --reset

To drop all indices and register them again:

php artisan elastic:migrate --fresh

To rollback migration:

php artisan elastic:migrate-rollback

By default, this command rollbacks the migrations just one step.if you want to determine steps by yourself:

php artisan elastic:migrate-rollback --step=<number>

Field Types

Integer

String(keyword)

Object

Boolean

SmallInteger(short)

BigInteger(long)

Double

Float

TinyInt(byte)

Text

DateTime(date)

Edit Indices Mappings

Sometimes you need modify your mappings. To do this you have to add a new migration:

php artisan elastic:make-migration <alter migration name>

As you can see, we implemented AlterElasticIndexMigrationInterface interface in our migration. Then in alterDown method we wrote our rollback scenario. Finally, migrate your migration:

php artisan elastic:migrate

Dynamic Mapping

By default, Elasticsearch detects the type of fields that you have not introduced in mapping and defines its type automatically. The package has disabled it by default. To activate it, do the following in your migration:

Query Builder

Just like Laravel, which enabled you to create complex and crude queries by using a series of pre-prepared methods, this package also uses this feature to give you a similar experience.

Store a recorde

Store Several Records (Bulk Insert)

Sometimes, some items may not be saved in the database due to an error. you can check this like below:

Also if you want to rollback transaction if any error happend set the $withTransaction argumet as true:

this action will remove the imported items from database.

Find record

Remove record

Conditions

Equal

Not Equal

Greater Than

Lower Than

Like

whereTerm

Sometimes you want to search for a specific phrase in a text. In this case, you can do the following :

whereIn

whereNotIn

whereBetween

whereNotBetween

whereNull

whereNotNull

Chaining

Fuzzy Search

Note: fuzzy search just works on the text fields

You can change the fuzziness value as you want

Get pure Query

Update record

Bulk update

Bulk delete

Take(limit)

Offset

select

OrderBy

By Adding above line you can use texts as sortable and in aggregations,but fielddata uses significant memory while indexing

Get specific field's value

Nested Search

First of all we need to define object type in our migration:

If you have multi dimension objects like below:

Define your mappings Like below:

Destroy by id

Nested Queries

In order to create complex and nested queries, you can use the nesting function of the builder. There is no limit to the nesting of your queries:

Just pay attention that you need to return the queries inside closure otherwise it will be ignored

chunk

for better managing your memory usage you can use the chunk method :

Aggregations

By default, all related data also will be return, If you want just aggregations be in your result use take(0) to prevent oveloading data in you request

Count

bucket

By default, bucket method returns maximum 2147483647 number of the records,if You want to change it:

Min

Max

Avg

Sum

Unique

Sometimes you need to retrieve only unique records based on an criterion:

groupBy

In order to group data based on a criterion

Pagination

By default paginate methods paginates pages per 15 items,but you can change it:

The result will be something like this:

inRandomOrder

Sometimes you need to get random data from elasticsearch:

Interact With Documentations

Drop indices by name

Check index Exists

Get all indices

Drop index by model

Get all model fields

Get model mappings

Automate Syncing with Primary Database

In NoSQL database systems, merging tables into a single document in Elasticsearch is common. However, when entities are updated in the relational database, keeping Elasticsearch synchronized poses a significant challenge for developers. This often requires writing extensive code to manage dependencies.

To simplify this process, Elasticquent offers a built-in feature called the Bridge, which allows developers to manage dependencies effortlessly.

Example

Consider an entity like an article linked to a category in our relational database.

Articles Table

Column Data Type Description
id INT Unique identifier for the article
title VARCHAR Title of the article
content TEXT Content of the article
category_id INT Foreign key referencing the category of the article
created_at TIMESTAMP Timestamp indicating when the article was created
updated_at TIMESTAMP Timestamp indicating when the article was last updated

Categories Table

Column Data Type Description
id INT Unique identifier for the category
title VARCHAR Title of the category
description TEXT Description of the category
created_at TIMESTAMP Timestamp indicating when the category was created
updated_at TIMESTAMP Timestamp indicating when the category was last updated

When storing this data in Elasticsearch, we join these tables.

Elasticsearch Index (Document Structure)

But what if the title of a category is updated in the relational database?

To handle this, we define a bridge in the Elasticsearch model to connect these segments of data.

Defining the Bridge

To create a bridge, implement the SyncInstructionsBridge or CustomSyncBridge interfaces. In this example, we use the SyncInstructionsBridge interface to specify the type of bridge.

We also have another type of interface for syncing, CustomSyncBridge. This type of syncing is highly custom, allowing you to access the document and update the entity simultaneously.

Connecting the Bridge to Elasticsearch Model

What's Next?

The good news is, there's nothing more to do! Simply define the bridge and introduce it into your Elasticsearch model. Then, set a little configuration once, allowing the Elastic package to detect your Elasticsearch models with the autodiscovery feature built into the Elastic package.

In the example above, when a category is updated, this bridge is triggered to update all related items in Elasticsearch based on your bridge definition. 🚀

Auto Discovery of Elastic Models

By default, the package searches the project in the app/Elasticsearch/Models directory to find your Elasticsearch models. You can customize this behavior in your Elasticsearch configuration file.

Danger: If your model is not discovered, the bridge may not work successfully. Ensure that your Elasticsearch models are located in the specified directories to avoid issues with model discovery and bridge functionality.

Coming soon


All versions of laravel-elasticsearch with dependencies

PHP Build Version
Package Version
Requires guzzlehttp/guzzle Version ^7.0
nizek/manifest-manager Version ^1.0
alighorbani1381/command-manager Version ^1.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package mawebcoder/laravel-elasticsearch contains the following files

Loading the files please wait ....