Download the PHP package nordsoftware/lumen-elasticsearch without Composer
On this page you can find all versions of the php package nordsoftware/lumen-elasticsearch. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download nordsoftware/lumen-elasticsearch
More information about nordsoftware/lumen-elasticsearch
Files in nordsoftware/lumen-elasticsearch
Package lumen-elasticsearch
Short Description Simple wrapper of https://github.com/elastic/elasticsearch-php for the Lumen PHP framework.
License MIT
Informations about the package lumen-elasticsearch
Lumen Elasticsearch
Simple wrapper of Elasticsearch-PHP for the Lumen PHP framework.
Version support
Lumen | Elasticsearch | PHP | Library |
---|---|---|---|
>= 6.0 | 5.x | >= 7.1 | 3.8 - 3.x |
>= 5.4, <= 6.0 | 5.x | >= 7.1 | 3.0 - 3.7 |
5.4.x | 5.x | >= 5.6 | 2.x |
5.4.x | 2.x | >= 5.6 | 1.x |
5.2.x | 2.x | >= 5.6 | 0.7.x |
Requirements
- 3.x requires PHP 7.1 or newer
- 2.x requires PHP 5.6 or newer
Usage
Installation
Run the following command to install the package through Composer:
Copy the configuration template in config/elasticsearch.php
to your application's config
directory and modify it
to suit your needs.
Add the following line to :
You can now get the service instance using or inject the where needed.
Configuring your indices
You can create and delete indices using the provided console commands. Start by adding the commands to your console kernel:
To create an index you need a configuration file that describes how the index should look. To create an index called
my-index
, create a file named my-index.php
in the config/elasticsearch
directory (create the directory if it
doesn't exist) with the following contents:
Please refer to the official Elasticsearch documentation for more information on how to define indices.
Now that you have a configuration file for your index, create it by running
php artisan elastic:index:create config/elasticsearch/my-index.php
.
To delete the index, run php artisan elastic:index:delete my-index
.
Indexing your data
To index data into your newly created indices you need to create a new console command that extends
Nord\Lumen\Elasticsearch\Console\IndexCommand
, then register that command in your console kernel. A sample
implementation can look like this:
Now, run php artisan app:index:persons
to index the data. You can now create additional commands for your other data
types that need to be indexed.
In addition to IndexCommand
there is an AbstractMultiIndexCommand
that can be used if you need to index the same
data into multiple indices. This can be useful if you're migrating Elasticsearch 5.x indices to Elasticsearch 6.x,
which doesn't support indices with multiple different document types.
Indexing single items
The console commands are useful when you want to index all items of a particular type, e.g. all persons in your database. However, if you update a single person you probably want to reindex just that person.
Here's an example:
Running queries
Queries against the search index are run by creating a query, then creating a search using the query and finally executing the query using the provided service.
Here's an example:
You can also perform raw queries:
Creating and applying index migrations
Sometimes you need to make a change to an index mapping that requires data to be re-indexed. However, most of the time you don't need to actually index everything into Elasticsearch again, instead it is enough to just copy the data internally from your original index to a new one. This process can be performed seamlessly without downtime.
Requirements
- You must add the
CreateMigrationCommand
andApplyMigrationCommand
commands to your console kernel - Your Elasticsearch instance must support the
/_reindex
API. This is normally the case, however, Amazon Elasticsearch Service doesn't support it if you're using Elasticsearch 2.3 or older.
Creating a migration
- Change your index definition (e.g.
config/search/your-index.php
) according to your needs - Run
php artisan elastic:migrations:create config/search/your-index.php
)
This will create a directory named versions
as well as a timestamped copy of your index definition file.
Applying a migration
- Run
php artisan elastic:migrations:migrate config/search/your-index.php
If you haven't run migrations before, your index will be replaced by an alias of the same name once the new index has been created. The next time you apply a migration, the alias will simply be updated.
If your documents are very large you may want to decrease the batch size used during re-indexing to prevent
Elasticsearch from running out of memory. You can do so by passing --batchSize=X
to the elastic:migrations:migrate
command. If the option is omitted, the default value of 1000 is used.
Updating dynamic index settings
There is a console command (elastic:index:settings:update
) that you can use to update certain dynamic index settings
such as the refresh interval or the number of replicas. Simply register it in your console kernel to start using it.
Using index prefixes
This library supports specifying a prefix for index names, similarly to how many frameworks support cache prefixes in order to allow multiple applications to share the same cache. This means you can use a single Elasticsearch cluster for multiple projects (or for example a shared one for the "dev" and "staging" environment).
The prefix used is specified by the configuration file (config/elasticsearch.php
). The default behavior is to read the
prefix from the ELASTICSEARCH_INDEX_PREFIX
environment variable.
If you have an index named
content
and you specify foo
as a prefix, the index will be named foo_content
. If you need custom logic you
can override getPrefixedIndexName()
in ElasticsearchService
.
Prefixing is supported for index migrations too, in which case the both the indices and the aliases created are prefixed.
Pagerfanta integration
There is a Pagerfanta adapter included for easy pagination. However, it is optional, so if you intend to use it you
must require the pagerfanta/pagerfanta
package explicitly.
Contributing
Please read the guidelines.
Running tests
Clone the project and install its dependencies by running:
Run the following command to run the test suite:
License
See LICENSE.
All versions of lumen-elasticsearch with dependencies
elasticsearch/elasticsearch Version ^5.3
illuminate/support Version ^5.4 | ^6.0
illuminate/console Version ^5.4 | ^6.0
league/pipeline Version ^0.3.0