Download the PHP package carlosocarvalho/elasticsearch without Composer
On this page you can find all versions of the php package carlosocarvalho/elasticsearch. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download carlosocarvalho/elasticsearch
More information about carlosocarvalho/elasticsearch
Files in carlosocarvalho/elasticsearch
Package elasticsearch
Short Description Laravel, Lumen and Native php elasticseach query builder to build complex queries using an elegant syntax. This is fork from carlosocarvalho
License MIT
Informations about the package elasticsearch
Laravel, Lumen and Native php elasticseach query builder to build complex queries using an elegant syntax
- Keeps you away from wasting your time by replacing array queries with a simple and elegant syntax you will love.
- Elasticsearch data model for types and indices inspired from laravel eloquent.
- Feeling free to create, drop, mapping and reindexing through easy artisan console commands.
- Lumen framework support.
- Native php and composer based applications support.
- Can be used as a laravel scout driver.
- Dealing with multiple elasticsearch connections at the same time.
- Awesome pagination based on LengthAwarePagination.
- Caching queries using a caching layer over query builder built on laravel cache.
Requirements
-
php
>= 5.6.6See Travis CI Builds.
laravel/laravel
>= 5. orlaravel/lumen
>= 5. orcomposer application
Documentation
See Full Documentation.
Installation
Laravel Installation
1) Install package using composer.
2) Add package service provider (< laravel 5.5).
3) Add package alias (< laravel 5.5).
4) Publishing.
Lumen Installation
1) Install package using composer.
2) Add package service provider in bootstrap/app.php
.
3) Copy package config directory vendor/carlosocarvalho/elasticsearch/src/config
to root folder alongside with app
directory.
4) Making Lumen work with facades by uncommenting this line in bootstrap/app.php
.
If you don't want to enable working with Lumen facades you can access the query builder using app("es")
.
Composer Installation
You can install package with any composer-based applications
1) Install package using composer.
2) Creating a connection.
Configuration (Laravel & Lumen)
After publishing, two configuration files will be created.
-
config/es.php
where you can add more than one elasticsearch server. config/scout.php
where you can use package as a laravel scout driver.
Working with console environment (Laravel & Lumen)
With some artisan commands you can do some tasks such as creating or updating settings, mappings and aliases.
Note that all commands are running with --connection=default
option, you can change it through the command.
These are all available commands:
List All indices on server
Create indices defined in es.php
config file
Note that creating operation skips the index if exists.
Update indices defined in es.php
config file
Note that updating operation updates indices setting, aliases and mapping and doesn't delete the indexed data.
Drop index
Be careful when using this command, you will lose your index data!
Running drop command with --force
option will skip all confirmation messages.
Reindexing data (with zero downtime)
First, why reindexing?
Changing index mapping doesn't reflect without data reindexing, otherwise your search results will not work on the right way.
To avoid down time, your application should work with index alias
not index name
.
The index alias
is a constant name that application should work with to avoid change index names.
Assume that we want to change mapping for my_index
, this is how to do that:
1) Add alias
as example my_index_alias
to my_index
configuration and make sure that application is working with.
2) Update index with command:
3) Create a new index as example my_new_index
with your new mapping in configuration file.
4) Reindex data from my_index
into my_new_index
with command:
5) Remove my_index_alias
alias from my_index
and add it to my_new_index
in configuration file and update with command:
Usage as a Laravel Scout driver
First, follow Laravel Scout installation.
All you have to do is updating these lines in config/scout.php
configuration file.
Have a look at laravel Scout documentation.
Elasticsearch data model
Each index type has a corresponding "Model" which is used to interact with that type. Models allow you to query for data in your types or indices, as well as insert new documents into the type.
Basic usage
The above example will use the default connection and default index in es.php
. You can override both in the next example.
Retrieving Models
Once you have created a model and its associated index type, you are ready to start retrieving data from your index. For example:
Adding Additional Constraints
The all
method will return all of the results in the model's type. Each elasticsearch model serves as a query builder, you may also add constraints to queries, and then use the get()
method to retrieve the results:
Retrieving Single Models
Inserting Models
To create a new document, simply create a new model instance, set attributes on the model, then call the save()
method:
Updating Models
The save()
method may also be used to update models that already exist. To update a model, you should retrieve it, set any attributes you wish to update, and then call the save method.
Deleting Models
To delete a model, call the delete()
method on a model instance:
Query Scopes
Scopes allow you to define common sets of constraints that you may easily re-use throughout your application. For example, you may need to frequently retrieve all posts that are considered "popular". To define a scope, simply prefix an Eloquent model method with scope.
Scopes should always return a Query instance.
Once the scope has been defined, you may call the scope methods when querying the model. However, you do not need to include the scope prefix when calling the method. You can even chain calls to various scopes, for example:
Accessors & Mutators
Defining An Accessor
To define an accessor
, create a getFooAttribute method on your model where Foo
is the "studly" cased name of the column you wish to access. In this example, we'll define an accessor for the title
attribute. The accessor will automatically be called by model when attempting to retrieve the value of the title
attribute:
As you can see, the original value of the column is passed to the accessor, allowing you to manipulate and return the value. To access the value of the accessor, you may simply access the title
attribute on a model instance:
Occasionally, you may need to add array attributes that do not have a corresponding field in your index. To do so, simply define an accessor for the value:
Once you have created the accessor, just add the value to the appends
property on the model:
Once the attribute has been added to the appends list, it will be included in model's array.
Defining A Mutator
To define a mutator, define a setFooAttribute
method on your model where Foo
is the "studly" cased name of the column you wish to access. So, again, let's define a mutator for the title
attribute. This mutator will be automatically called when we attempt to set the value of the title
attribute on the model:
The mutator will receive the value that is being set on the attribute, allowing you to manipulate the value and set the manipulated value on the model's internal $attributes
property. So, for example, if we attempt to set the title attribute to Awesome post to read
:
In this example, the setTitleAttribute function will be called with the value Awesome post to read
. The mutator will then apply the strtolower function to the name and set its resulting value in the internal $attributes array.
Attribute Casting
The $casts
property on your model provides a convenient method of converting attributes to common data types. The $casts
property should be an array where the key is the name of the attribute being cast and the value is the type you wish to cast the column to. The supported cast types are: integer
, float
, double
, string
, boolean
, object
and array
.
For example, let's cast the is_published
attribute, which is stored in our index as an integer (0 or 1) to a boolean
value:
Now the is_published
attribute will always be cast to a boolean
when you access it, even if the underlying value is stored in the index as an integer:
Usage as a query builder
Creating a new index
Creating index with custom options (optional)
Dropping index
Running queries
You can rewrite the above query to
The query builder will use the default connection, index name in configuration file es.php
.
Connection and index names in query overrides connection and index names in configuration file es.php
.
Getting document by id
Sorting
Limit and offset
Select only specific fields
Where clause
Where greater than
Where greater than or equal
Where less than
Where less than or equal
Where like
Where field exists
Where in clause
Where between clause
Where not clause
Where not greater than
Where not greater than or equal
Where not less than
Where not less than or equal
Where not like
Where not field exists
Where not in clause
Where not between clause
Search by a distance from a geo point
Search using array queries
Search the entire document
Return only first record
Return only count
Scan-and-Scroll queries
Paginate results with 5 records per page
These are all pagination methods you may use:
Getting the query array without execution
Getting the original elasticsearch response
Ignoring bad HTTP response
Query Caching (Laravel & Lumen)
Package comes with a built-in caching layer based on laravel cache.
Executing elasticsearch raw queries
Insert a new document
Bulk insert a multiple of documents at once.
Update an existing document
Incrementing field
Decrementing field
Update using script
Delete a document
Releases
See Change Log.
Author
Basem Khirat - @carlosocarvalho
Bugs, Suggestions and Contributions
Thanks to everyone who has contributed to this project!
Please use Github for reporting bugs, and making comments or suggestions.
License
MIT
Have a happy searching..
All versions of elasticsearch with dependencies
elasticsearch/elasticsearch Version ^5.0|~6.0
illuminate/pagination Version ^5.5|^6.0
illuminate/support Version ^5.5|^6.0
symfony/var-dumper Version *
monolog/monolog Version ^1.23