Download the PHP package ensi/laravel-elastic-query without Composer
On this page you can find all versions of the php package ensi/laravel-elastic-query. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-elastic-query
Laravel Elastic Query
Working with Elasticsearch in an Eloquent-like fashion.
Installation
You can install the package via composer:
Attention: Synonyms API methods require Elasticsearch 8.10+
Publish config file like this:
Set ELASTICSEARCH_HOSTS in your .env file. , can be used as a delimeter.
Version Compatibility
| Laravel Elastic Query | Laravel | PHP | Elasticsearch |
|---|---|---|---|
| ^0.1.0 | ^8.0 | ^8.0 | 7.* |
| ^0.2.0 | ^8.0 | ^8.0 | 7.* |
| ^0.3.0 | ^8.0 | ^8.0 | 7.* |
| ^0.3.2 | ^8.0 || ^9.0 | ^8.0 | 7.* |
| ^7.x (see details) | ^9.0 || ^10.0 || ^11.0 | ^8.1 | 7.* |
| ^8.0.0 | ^8.0 || ^9.0 | ^8.0 | 8.* |
| ^8.0.13 | ^8.0 || ^9.0 || ^10.0 | ^8.0 | 8.* |
| ^8.0.23 | ^8.0 || ^9.0 || ^10.0 || ^11.0 | ^8.0 | 8.* |
| ^8.1.0 | ^9.0 || ^10.0 || ^11.0 | ^8.1 | 8.* |
| ^8.2.3 | ^9.0 || ^10.0 || ^11.0 || ^12.0 | ^8.1 | 8.* |
| ^8.2.13 | ^9.0 || ^10.0 || ^11.0 || ^12.0 | ^8.1 | 8.10+ |
Basic usage
Let's create and index class. It's someting like Eloquent model.
You should set a unique in document attribute name in $tiebreaker. It is used as an additional sort in search_after
Now we can get some documents
Filtering
nested_field must have nested type.
Subqueries cannot use fields of main document only subdocument.
Full text search
field_one and field_two must be of text type. If no type is given, the MatchType::BEST_FIELDS is used.
Sorting
Second attribute is a direction. It supports asc and desc values. Defaults to asc.
Third attribute - sorting type. List of supporting types: min, max, avg, sum, median. Defaults to min.
There are also dedicated sort methods for each sort type.
Pinned query
Promotes selected documents to rank higher than those matching a given query. This feature is typically used to guide searchers to curated documents that are promoted over and above any "organic" matches for a search. The promoted or "pinned" documents are identified using the document IDs stored in the _id field.
Pagination
Offset Pagination
Offset pagination returns total documents count as total and current position as size/offset.
Cursor pagination
current, next, previous is returned in this case instead of total, size and offset.
You can check Laravel docs for more info about cursor pagination.
Aggregation
Aggregaction queries can be created like this
Type of $aggs->price is MinMax.
Type of $aggs->codes is BucketCollection.
Aggregate names must be unique for whole query.
Aggregate types
Get all variants of attribute values:
Get min and max attribute values. E.g for date:
Get count unique attribute values:
Aggregation plays nice with nested documents.
There is also a special virtual composite aggregate on the root level. You can set special conditions using it.
Suggesting
Suggest queries can be created like this
Global suggest text
User can set global text like this
Suggester types
Term suggester:
Phrase Suggester:
Additional methods
Synonyms API
Synonyms API methods are available through the ElasticQuery facade.
These methods require Elasticsearch 8.10+.
Get all synonym sets
Get a synonym set
Create or update a synonym set
Delete a synonym set
Get a synonym rule
Create or update a synonym rule
Delete a synonym rule
Query Log
Just like Eloquent ElasticQuery has its own query log, but you need to enable it manually
Each message contains indexName, query and timestamp
Environment Variables
Below see the environment variables that you can configure with the default values,
Hosts should be comma seperated string of hosts with protocol prefix and port suffix, e.g. http://localhost:9200,http://localhost:9201
Async Usage
All methods can return a Promise.
To enable this, you will need to add http_async_client to your config and then execute ElasticSearch::getClient()->setAsync(true).
To disable: ElasticQuery::getClient()->setAsync(false).
For example:
laravel-elastic-query.php:
HttpClientOptionsBuilder:
Action:
Elasticsearch 7 and 8 support.
Due to the incompatibility of clients for Elasticsearch 7 and 8, separate releases will be created for these versions. Development for each version is carried out in the corresponding branch.
To make changes to version 7, you need to create a task branch based on v7 and make a pull request to it. For version 8 it is similar, but based on the v8 branch.
Contributing
Please see CONTRIBUTING for details.
Testing
- composer install
- start Elasticsearch in your preferred way
- if you need change
ELASTICSEARCH_HOSTS, copyphpunit.xml.disttophpunit.xmland fill value - composer test
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-elastic-query with dependencies
elasticsearch/elasticsearch Version ^8.10
laravel/framework Version ^9.0 || ^10.0 || ^11.0 || ^12.0
webmozart/assert Version ^1.11