Download the PHP package shisun/laravel-elasticsearch-query-builder without Composer
On this page you can find all versions of the php package shisun/laravel-elasticsearch-query-builder. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download shisun/laravel-elasticsearch-query-builder
More information about shisun/laravel-elasticsearch-query-builder
Files in shisun/laravel-elasticsearch-query-builder
Package laravel-elasticsearch-query-builder
Short Description :Query Elasticsearch by using Eloquent
License GPL-3.0-only
Homepage https://github.com/shisun/laravel-elasticsearch-query-builder
Informations about the package laravel-elasticsearch-query-builder
Laravel Elasticsearch Query Builder
Query Elasticsearch by using Eloquent
This Laravel package is developed to simplify the process of querying Elasticsearch. Eloquent is a powerful tool to access and manipulate data in RDB. However, it is not designed to query no-sql DB like Elasticsearch. This package is made to fill the gap between the most popular ORM in Laravel and Elasticsearch.
The package only relies on the official PHP Elasticsearch package(v6).
PHP version
The package is developed and tested under PHP v7.1. It should be also compatible with v7.*. Please email me if you find any compatibility issue.
Elasticsearch version
The package is developed and tested under Elasticsearch v6.*. It should be also compatible with v5.*. Please email me if you find any compatibility issue. It is confirmed that the package does not support versions before v5.*.
Installation
-
Add the following to your
composer.jsonfile: -
Run
composer updatein terminal -
Add the package to your
app.phpfile in the config folder: -
If your Elasticsearch is not accessible at
localhost:9200then you have to publish the config file by running: -
(Optional) Change the
ES_HOSTSin config/laravel-elasticsearch-query-builder.php to the address of your Elasticsearch -
Use with Eloquent Model
-
Add
EsTraitto your model -
Enable validation(Optional)
- Set model-level Index and Type name. Add follow two functions to your model.(Optional)
-
- Use without Eloquent
Usage
Table of Contents
- Init & Configs
- __construct
- setOptions
- getters
- getIndexName
- getTypeName
- getValidation
- getMappingProperties
- setters
- setIndexName
- setTypeName
- setValidation
- setMappingProperties
- Query Clauses
- where
- orWhere
- whereMatch
- whereDoesntMatch
- orWhereMatch
- whereHas
- whereHasNull
- orWhereHas
- whereIn
- orWhereIn
- whereNotIn
- orWhereNotIn
- whereBetween
- orWhereBetween
- Order
- orderBy
- Pagination
- page
- limit
- offset
- Aggregation
- aggregate
- aggregateAll
- aggregateOn
- groupBy
- min
- max
- avg
- sum
- Other Filters
- minScore
- with
- withOut
- Execute Query
- get
- scroll
- count
- first
- find
- delete
- Results Manipulation
- toArray
- toEloquent
- rawResults
- aggregations
- getAggregationBuckets
- paginate
- Direct Query Output
- getQuery
- getBody
- getAggs
Init & Configs
__construct
- You do not need to use this function if you use Eloquent
-
Parameters
Name Required Type Default Description model Eloquent,null|null -
Output
self - Examples
- basic example
setOptions
- You do not need to use this function if you use Eloquent
-
Parameters
Name Required Type Default Description options Y array -
Output
self - Examples
- Basic
Query Clauses
where
-
Parameters
Name Required Type Default Description column Y callable,stringoperator string|null|=,>,<,<=,>=,!=,*value mixed|nullor bool|falseboost bool,int|falseThe weight of the column -
Output
self -
Examples
-
=can be ignored -
*is the wildcard operator columncan be a function
-
whereNull
-
Parameters
Name Required Type Default Description column Y stringColumn or relation -
Output
self -
Examples
-
Find all users with no name
- Find all users who don't have address
-
whereNotNull
-
Parameters
Name Required Type Default Description column Y stringColumn or relation -
Output
self -
Examples
-
Find all users with a name
- Find all users who have at least one address
-
orWhere
-
Parameters
Name Required Type Default Description column Y callable,stringoperator string|null|=,>,<,<=,>=,like,!=,*value mixed|nullboost bool,int|falseThe weight of the column -
Output
self -
Examples
-
=can be ignored columncan be a function
-
whereMatch
- It is used to make fuzzy text search. This function should only be applied on text fields.
-
Parameters
Name Required Type Default Description column Y stringvalue mixed|nulloptions array| [] |matchquery options. Check elasticsearch Docs for references -
Output
self -
Examples
-
without
options - with
options
-
whereDoesntMatch
-
Parameters
Name Required Type Default Description column Y stringvalue mixed|nulloptions array| [] |matchquery options. Check elasticsearch Docs for references -
Output
self -
Examples
-
without
options - with
options
-
orWhereMatch
-
Parameters
Name Required Type Default Description column Y stringvalue mixed|nulloptions array| [] |matchquery options. Check elasticsearch Docs for references -
Output
self -
Examples
-
without
options - with
options
-
whereHas
-
Parameters
Name Required Type Default Description relation Y string,callableMust be capitalized closure callable,null|null|fromandtocannot be bothnullor boolean|nullfor internal use. See orWhereHas boost boolean,integer|falseUsed to adjust the weight of the condition -
Output
self -
Examples
-
basic example
- first parameter can be a function
-
orWhereHas
-
Parameters
Name Required Type Default Description relation Y string,callableMust be capitalized closure callable,null|null|fromandtocannot be bothnullboost boolean,integer|falseUsed to adjust the weight of the condition -
Output
self - Examples
- basic example
whereHasNull
-
Parameters
Name Required Type Default Description relation Y stringMust be capitalized closure null,callable|null -
Output
self -
Examples
-
basic example
- with sub query
-
orWhereHasNull
-
Parameters
Name Required Type Default Description relation Y stringMust be capitalized closure null,callable|null -
Output
self - Examples
- basic example
whereIn
-
Parameters
Name Required Type Default Description column Y stringvalues Y array -
Output
self - Examples
- basic example
orWhereIn
-
Parameters
Name Required Type Default Description column Y stringvalues Y array -
Output
self - Examples
- basic example
whereNotIn
-
Parameters
Name Required Type Default Description column Y stringvalues Y array -
Output
self - Examples
- basic example
orWhereNotIn
-
Parameters
Name Required Type Default Description column Y stringvalues Y array -
Output
self - Examples
- basic example
whereBetween
-
Parameters
Name Required Type Default Description column Y stringfrom numeric|null|fromandtocannot be bothnullto numeric|null -
Output
self - Examples
- basic example
orWhereBetween
-
Parameters
Name Required Type Default Description column Y stringfrom numeric|null|fromandtocannot be bothnullto numeric|null -
Output
self - Examples
- basic example
Order
orderBy
-
Parameters
Name Required Type Default Description column Y stringorder numeric|asc| eitherascordescscript array,boolean|false -
Output
self -
Examples
-
basic example
- script
-
Pagination
page
-
Parameters
Name Required Type Default Description page Y integerper_page Y integer -
Output
self - Examples
- basic example
limit
-
Parameters
Name Required Type Default Description limit Y integer -
Output
self - Examples
- basic example
offset
-
Parameters
Name Required Type Default Description limit Y integer -
Output
self - Examples
- basic example
Aggregations
aggregate
-
Parameters
Name Required Type Default Description name Y stringagg Y callable -
Output
self -
Examples
-
basic example
- with filters
-
aggregateAll
-
Parameters
Name Required Type Default Description name Y stringagg Y callable -
Output
self - Examples
- with filters
aggregateOn
-
Parameters
Name Required Type Default Description relation Y stringMust be capitalized agg Y callablecustom_name string|null -
Output
self - Examples
- basic example
groupBy
-
Parameters
Name Required Type Default Description column Y stringsize integer|10The limit of number of groups. Default to 10 groups -
Output
self - Check aggregate clause for examples
min
-
Parameters
Name Required Type Default Description column Y stringcustom_name string|null -
Output
self - Examples
- basic example
max
-
Parameters
Name Required Type Default Description column Y stringcustom_name string|nullmissing_value numeric|nullThis value will be used to replace null values -
Output
self - Examples
- basic example
avg
-
Parameters
Name Required Type Default Description column Y stringcustom_name string|nullmissing_value numeric|nullThis value will be used to replace null values -
Output
self - Examples
- basic example
sum
-
Parameters
Name Required Type Default Description column string|Emptycustom_name string|nullmissing_value numeric|nullThis value will be used to replace null values script array|nullCheck official Doc as reference -
Output
self - Examples
- basic example
Other Filters
minScore
-
Parameters
Name Required Type Default Description score Y numericOnly get items with _score greater than this value -
Output
self - Examples
- basic example
with
-
Parameters
Name Required Type Default Description relations Y listinclude these relations in results. All relations are included by default. -
Output
self - Examples
- basic example
withOut
-
Parameters
Name Required Type Default Description relations Y listExclude these relations in results. All relations are included by default. -
Output
self - Examples
- basic example
Execute Query
get
scroll
-
Parameters
Name Required Type Default Description scroll_alive string|5mThis specifies the life span of the scroll session. scroll_size integer|500This specifies the number of records retrieved from Elasticsearch per batch. json boolean|falseIf encode the results from each batch in JSON format. Fetching data and put the data in PHP array can consume a lot of memory. This option can greatly reduce the memory usage. -
Output
array - Examples
- basic example
count
-
Output
integer - Examples
- basic example
first
-
Parameters
Name Required Type Default Description eloquent boolean|falseget result in array or Eloquent. Returns array if not provided. -
Output
array|Eloquent - Examples
- basic example
find
-
Parameters
Name Required Type Default Description key Y mixed -
Output
array - Examples
- basic example
delete
- Delete a record from elasticsearch. Returns false if the record does not exist.
-
Parameters
Name Required Type Default Description key Y mixed -
Output
array - Examples
- basic example
Results Manipulation
toArray
-
Output
array
toEloquent
Warning: This function does not work if you don't use the package with Eloquent.
-
Output
Eloquent
rawResults
Get raw results from Elasticsearch
-
Output
array
aggregations
Get aggregation results
-
Output
array
getAggregationBuckets
This is a helper function to get buckets from the aggregation specified by agg_name
-
Parameters
Name Required Type Default Description agg_name Y mixedagg mixed|null -
Output
array - Examples
- basic example
paginate
Returns pagination information
-
Parameters
Name Required Type Default Description per_page mixedThe value can be ignored if per_page is set by limit() or page(). Otherwise, it's required. -
Output
array - Example
- basic
Direct Query Output
getQuery
Returns the query part of the body
getBody
Returns the body
getAggs
Returns the aggregation part of the body
Release History
Meta
Shisun(Leo) Xia - [email protected]
Distributed under the GNU V3 license. See LICENSE for more information.
https://github.com/ShisunXia/Laravel-Elasticsearch-Query-Builder