Skip to content

Commit a127260

Browse files
committed
add laravel scout support
1 parent 8290c95 commit a127260

File tree

5 files changed

+527
-67
lines changed

5 files changed

+527
-67
lines changed

composer.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
{
22
"name": "basemkhirat/elasticsearch",
3-
"description": "Elasticsearch query builder",
4-
"keywords": ["elasticsearch", "full text", "builder", "laravel"],
3+
4+
"description": "laravel elasticsearch query builder",
5+
6+
"keywords": ["elasticsearch", "laravel", "scout", "fulltext", "indexing", "builder"],
7+
58
"license": "MIT",
9+
610
"type": "package",
11+
712
"authors": [
813
{
914
"name": "basemkhirat",
@@ -19,6 +24,7 @@
1924

2025
"require": {
2126
"elasticsearch/elasticsearch": "5.0.0",
27+
"laravel/scout": "3.0.0",
2228
"illuminate/pagination": "*",
2329
"illuminate/support": "*"
2430
}

readme.md

Lines changed: 139 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,34 @@
1-
### Flexible elasticseach builder to run complex queries with an easier way.
1+
<p align="center">
22

3-
#####1) Install package via composer:
3+
<a href="https://packagist.org/packages/basemkhirat/elasticsearch"><img src="https://poser.pugx.org/basemkhirat/elasticsearch/v/stable.svg" alt="Latest Stable Version"></a>
4+
5+
<a href="https://packagist.org/packages/basemkhirat/elasticsearch"><img src="https://poser.pugx.org/basemkhirat/elasticsearch/d/total.svg" alt="Total Downloads"></a>
6+
7+
<a href="https://packagist.org/packages/basemkhirat/elasticsearch"><img src="https://poser.pugx.org/basemkhirat/elasticsearch/license.svg" alt="License"></a>
8+
9+
</p>
10+
11+
<p align="center"><img src="http://basemkhirat.com/images/basemkhirat-elasticsearch.png"></p>
12+
13+
14+
## Laravel elasticseach query builder to run complex queries with an elegant syntax.
15+
16+
17+
18+
- Laravel [elasticsearch](https://www.elastic.co/products/elasticsearch) query builder inspired from Eloquent.
19+
- Keep away from wasting your time by replacing array queries with simple and elegant syntax you will love.
20+
- Comes with [laravel 5.4](https://laravel.com/docs/5.4) and [laravel scout 3](https://laravel.com/docs/5.4/scout) support.
21+
- Dealing with multiple elasticsearch connections at the same time.
22+
- Support scan and scroll queries for dealing big data.
23+
- Awesome Pagination based on [LengthAwarePagination](https://github.com/illuminate/pagination).
24+
- Feeling free to create, drop and mapping index fields.
25+
- Caching queries using a caching layer over query builder built on [laravel cache](https://laravel.com/docs/5.4/cache).
26+
27+
28+
29+
## Installation
30+
31+
#####1) Install package using composer:
432

533
composer require basemkhirat/elasticsearch
634

@@ -14,29 +42,71 @@
1442

1543
#####4) Publishing:
1644

17-
php artisan vendor:publish --provider="Basemkhirat\Elasticsearch\ElasticsearchServiceProvider"
45+
php artisan vendor:publish
46+
1847

19-
### Usage:
48+
## Configuration
49+
2050

2151
#### Setting your connections
2252

2353

24-
After publishing, the config file is placed here `config/es.php`
25-
where you can add more than one elasticsearch node.
54+
After publishing, two configuration files will be created.
55+
56+
- `config/es.php` where you can add more than one elasticsearch server.
57+
58+
```
59+
60+
'default' => env('ELASTIC_CONNECTION', 'default'),
61+
62+
'connections' => [
63+
'default' => [
64+
'servers' => [
65+
[
66+
"host" => env("ELASTIC_HOST", "127.0.0.1"),
67+
"port" => env("ELASTIC_PORT", 9200),
68+
'user' => env('ELASTIC_USER', ''),
69+
'pass' => env('ELASTIC_PASS', ''),
70+
'scheme' => env('ELASTIC_SCHEME', 'http'),
71+
]
72+
],
73+
'index' => env('ELASTIC_INDEX', 'my_index'),
74+
'type' => env('ELASTIC_TYPE', 'my_type'),
75+
]
76+
]
77+
78+
```
79+
80+
- `config/scout.php` where you can use package as a laravel scout driver.
81+
82+
83+
All you have to do is updating these lines in `config/scout.php` configuration file.
84+
85+
86+
# change the default driver to `es`
87+
88+
'driver' => env('SCOUT_DRIVER', 'es'),
89+
90+
# link `es` driver with default elasticsearch connection in config/es.php
91+
92+
'es' => [
93+
'connection' => env('ELASTIC_CONNECTION', 'default'),
94+
],
95+
96+
Have a look at [laravel Scout documentation](https://laravel.com/docs/5.4/scout#configuration).
2697

98+
## Usage
2799

28100
#### Creating a new index
29101

30-
ES::index("my_index")->create();
102+
ES::create("my_index");
31103

32104
# or
33105

34-
ES::create("my_index");
106+
ES::index("my_index")->create();
35107

36108

37-
>
38-
39-
# [optional] you can create index with custom options
109+
##### Creating index with custom options (optional)
40110

41111
ES::index("my_index")->create(function($index){
42112
@@ -77,37 +147,44 @@
77147

78148
#### Dropping index
79149

80-
ES::index("my_index")->drop();
150+
ES::drop("my_index");
81151
82152
# or
83153

84-
ES::drop("my_index");
154+
ES::index("my_index")->drop();
85155

86-
#### Running queries:
156+
#### Running queries
87157

88158
$documents = ES::connection("default")
89159
->index("my_index")
90160
->type("my_type")
91-
->get(); // return collection of results
161+
->get(); # return collection of results
92162

93-
you can rewite the above query to
163+
You can rewite the above query to
94164

95165
$documents = ES::get(); // return collection of results
96166

97-
the query builder will use the default connection, index, and type names setted in configuration file `es.php`.
167+
The query builder will use the default connection, index, and type names setted in configuration file `es.php`.
98168

99-
Index and type names setted in query will override values the configuration file
169+
Index and type names setted in query will override values the configuration file.
100170

101171

102-
#### Available methods:
172+
---
103173

104174
##### Getting document by id
105175

176+
$documents = ES::id(3)->get();
177+
178+
# or
179+
106180
$documents = ES::_id(3)->get();
107181

108182
##### Sorting
109183

110184
$documents = ES::orderBy("created_at", "desc")->get();
185+
186+
# Sorting with text search score
187+
111188
$documents = ES::orderBy("_score")->get();
112189

113190
##### Limit and offset
@@ -219,7 +296,7 @@ Index and type names setted in query will override values the configuration file
219296

220297
##### Scan-and-Scroll queries
221298

222-
These queries are suitable for large amount of data.
299+
These queries are suitable for large amount of data.
223300
A scrolled search allows you to do an initial search and to keep pulling batches of results
224301
from Elasticsearch until there are no more results left. It’s a bit like a cursor in a traditional database
225302

@@ -228,19 +305,19 @@ Index and type names setted in query will override values the configuration file
228305
->take(1000)
229306
->get();
230307

231-
Response will contain a hashed code `scroll_id` will be used to get the next result by running
308+
Response will contain a hashed code `scroll_id` will be used to get the next result by running
232309

233310
$documents = ES::search("my_index")
234311
->scroll("2m")
235312
->scrollID("DnF1ZXJ5VGhlbkZldGNoBQAAAAAAAAFMFlJQOEtTdnJIUklhcU1FX2VqS0EwZncAAAAAAAABSxZSUDhLU3ZySFJJYXFNRV9laktBMGZ3AAAAAAAAAU4WUlA4S1N2ckhSSWFxTUVfZWpLQTBmdwAAAAAAAAFPFlJQOEtTdnJIUklhcU1FX2VqS0EwZncAAAAAAAABTRZSUDhLU3ZySFJJYXFNRV9laktBMGZ3")
236313
->get();
237314

238-
And so on ...
315+
And so on ...
239316

240-
NOTE: you don't need to write the query parameters in every scroll.
317+
Note that you don't need to write the query parameters in every scroll.
241318
All you need the `scroll_id` and query scroll time.
242319

243-
To clear scroll id
320+
To clear `scroll_id`
244321

245322
ES::scrollID("DnF1ZXJ5VGhlbkZldGNoBQAAAAAAAAFMFlJQOEtTdnJIUklhcU1FX2VqS0EwZncAAAAAAAABSxZSUDhLU3ZySFJJYXFNRV9laktBMGZ3AAAAAAAAAU4WUlA4S1N2ckhSSWFxTUVfZWpLQTBmdwAAAAAAAAFPFlJQOEtTdnJIUklhcU1FX2VqS0EwZncAAAAAAAABTRZSUDhLU3ZySFJJYXFNRV9laktBMGZ3")
246323
->clear();
@@ -258,13 +335,38 @@ Index and type names setted in query will override values the configuration file
258335

259336
>
260337
338+
##### Getting the query array without execution
339+
340+
$documents = ES::search("foo")->where("views", ">", 150)->query();
341+
261342
##### Ignoring bad HTTP response
262343
263-
$documents = ES::ignore(404, 500)->_id(5)->first();
344+
$documents = ES::ignore(404, 500)->id(5)->first();
264345

265346

266347
>
267348
349+
350+
##### Query Caching
351+
352+
Package comes with a built-in caching layer based on laravel cache.
353+
354+
ES::search("foo")->remember(10)->get();
355+
356+
# you can specify a custom cache key
357+
358+
ES::search("foo")->remember(10, "last_documents")->get();
359+
360+
# Caching using other available driver
361+
362+
ES::search("foo")->cacheDriver("redis")->remember(10, "last_documents")->get();
363+
364+
# Caching with cache key prefix
365+
366+
ES::search("foo")->cacheDriver("redis")->cachePrefix("docs")->remember(10, "last_documents")->get();
367+
368+
369+
268370
##### Executing elasticsearch raw queries
269371

270372
ES::raw()->search([
@@ -287,7 +389,7 @@ Index and type names setted in query will override values the configuration file
287389
288390
##### Insert a new document
289391

290-
ES::_id(3)->insert([
392+
ES::id(3)->insert([
291393
"title" => "Test document",
292394
"content" => "Sample content"
293395
]);
@@ -319,7 +421,7 @@ Index and type names setted in query will override values the configuration file
319421
320422
##### Update an existing document
321423
322-
ES::_id(3)->update([
424+
ES::id(3)->update([
323425
"title" => "Test document",
324426
"content" => "sample content"
325427
]);
@@ -332,11 +434,11 @@ Index and type names setted in query will override values the configuration file
332434
333435
##### Incrementing field
334436
335-
ES::_id(3)->increment("views");
437+
ES::id(3)->increment("views");
336438
337439
Document has _id = 3 will be incremented by 1.
338440

339-
ES::_id(3)->increment("views", 3);
441+
ES::id(3)->increment("views", 3);
340442

341443
Document has _id = 3 will be incremented by 3.
342444

@@ -346,11 +448,11 @@ Index and type names setted in query will override values the configuration file
346448
347449
##### Decrementing field
348450
349-
ES::_id(3)->decrement("views");
451+
ES::id(3)->decrement("views");
350452
351453
Document has _id = 3 will be decremented by 1.
352454

353-
ES::_id(3)->decrement("views", 3);
455+
ES::id(3)->decrement("views", 3);
354456

355457
Document has _id = 3 will be decremented by 3.
356458

@@ -363,36 +465,34 @@ Index and type names setted in query will override values the configuration file
363465

364466
# icrement field by script
365467

366-
ES::_id(3)->script(
468+
ES::id(3)->script(
367469
"ctx._source.$field -= params.count",
368470
["count" => 1]
369471
);
370472

371473
# add php tag to tags array list
372474

373-
ES::_id(3)->script(
475+
ES::id(3)->script(
374476
"ctx._source.tags.add(params.tag)",
375477
["tag" => "php"]
376478
);
377479

378480
# delete the doc if the tags field contain mongodb, otherwise it does nothing (noop)
379481

380-
ES::_id(3)->script(
381-
"if (ctx._source.tags.contains(params.tag)) { ctx.op = \"delete\" } else { ctx.op = \"none\" }",
482+
ES::id(3)->script(
483+
"if (ctx._source.tags.contains(params.tag)) { ctx.op = "delete" } else { ctx.op = "none" }",
382484
["tag" => "mongodb"]
383485
);
384486

385487
>
386488
387489
##### Delete a document
388490
389-
ES::_id(3)->delete();
491+
ES::id(3)->delete();
390492
391493
Document has _id = 3 will be deleted.
392494

393495
[id is required]
394496

395497

396-
`Good luck`
397-
398-
`Dont forget to send a feedback..`
498+
`Have a happy searching.. `

0 commit comments

Comments
 (0)