1
- ### Flexible elasticseach builder to run complex queries with an easier way.
1
+ < p align = " center " >
2
2
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:
4
32
5
33
composer require basemkhirat/elasticsearch
6
34
14
42
15
43
#####4 ) Publishing:
16
44
17
- php artisan vendor: publish --provider="Basemkhirat\Elasticsearch\ElasticsearchServiceProvider"
45
+ php artisan vendor: publish
46
+
18
47
19
- ### Usage:
48
+ ## Configuration
49
+
20
50
21
51
#### Setting your connections
22
52
23
53
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 ) .
26
97
98
+ ## Usage
27
99
28
100
#### Creating a new index
29
101
30
- ES::index ("my_index")->create( );
102
+ ES::create ("my_index");
31
103
32
104
# or
33
105
34
- ES::create ("my_index");
106
+ ES::index ("my_index")->create( );
35
107
36
108
37
- >
38
-
39
- # [optional] you can create index with custom options
109
+ ##### Creating index with custom options (optional)
40
110
41
111
ES::index("my_index")->create(function($index){
42
112
77
147
78
148
#### Dropping index
79
149
80
- ES::index ("my_index")->drop( );
150
+ ES::drop ("my_index");
81
151
82
152
# or
83
153
84
- ES::drop ("my_index");
154
+ ES::index ("my_index")->drop( );
85
155
86
- #### Running queries:
156
+ #### Running queries
87
157
88
158
$documents = ES::connection("default")
89
159
->index("my_index")
90
160
->type("my_type")
91
- ->get(); // return collection of results
161
+ ->get(); # return collection of results
92
162
93
- you can rewite the above query to
163
+ You can rewite the above query to
94
164
95
165
$documents = ES::get(); // return collection of results
96
166
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 ` .
98
168
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.
100
170
101
171
102
- #### Available methods:
172
+ ---
103
173
104
174
##### Getting document by id
105
175
176
+ $documents = ES::id(3)->get();
177
+
178
+ # or
179
+
106
180
$documents = ES::_id(3)->get();
107
181
108
182
##### Sorting
109
183
110
184
$documents = ES::orderBy("created_at", "desc")->get();
185
+
186
+ # Sorting with text search score
187
+
111
188
$documents = ES::orderBy("_score")->get();
112
189
113
190
##### Limit and offset
@@ -219,7 +296,7 @@ Index and type names setted in query will override values the configuration file
219
296
220
297
##### Scan-and-Scroll queries
221
298
222
- These queries are suitable for large amount of data.
299
+ These queries are suitable for large amount of data.
223
300
A scrolled search allows you to do an initial search and to keep pulling batches of results
224
301
from Elasticsearch until there are no more results left. It’s a bit like a cursor in a traditional database
225
302
@@ -228,19 +305,19 @@ Index and type names setted in query will override values the configuration file
228
305
->take(1000)
229
306
->get();
230
307
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
232
309
233
310
$documents = ES::search("my_index")
234
311
->scroll("2m")
235
312
->scrollID("DnF1ZXJ5VGhlbkZldGNoBQAAAAAAAAFMFlJQOEtTdnJIUklhcU1FX2VqS0EwZncAAAAAAAABSxZSUDhLU3ZySFJJYXFNRV9laktBMGZ3AAAAAAAAAU4WUlA4S1N2ckhSSWFxTUVfZWpLQTBmdwAAAAAAAAFPFlJQOEtTdnJIUklhcU1FX2VqS0EwZncAAAAAAAABTRZSUDhLU3ZySFJJYXFNRV9laktBMGZ3")
236
313
->get();
237
314
238
- And so on ...
315
+ And so on ...
239
316
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.
241
318
All you need the ` scroll_id ` and query scroll time.
242
319
243
- To clear scroll id
320
+ To clear ` scroll_id `
244
321
245
322
ES::scrollID("DnF1ZXJ5VGhlbkZldGNoBQAAAAAAAAFMFlJQOEtTdnJIUklhcU1FX2VqS0EwZncAAAAAAAABSxZSUDhLU3ZySFJJYXFNRV9laktBMGZ3AAAAAAAAAU4WUlA4S1N2ckhSSWFxTUVfZWpLQTBmdwAAAAAAAAFPFlJQOEtTdnJIUklhcU1FX2VqS0EwZncAAAAAAAABTRZSUDhLU3ZySFJJYXFNRV9laktBMGZ3")
246
323
->clear();
@@ -258,13 +335,38 @@ Index and type names setted in query will override values the configuration file
258
335
259
336
>
260
337
338
+ ##### Getting the query array without execution
339
+
340
+ $documents = ES::search("foo")->where("views", ">", 150)->query();
341
+
261
342
##### Ignoring bad HTTP response
262
343
263
- $documents = ES::ignore(404, 500)->_id (5)->first();
344
+ $documents = ES::ignore(404, 500)->id (5)->first();
264
345
265
346
266
347
>
267
348
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
+
268
370
##### Executing elasticsearch raw queries
269
371
270
372
ES::raw()->search([
@@ -287,7 +389,7 @@ Index and type names setted in query will override values the configuration file
287
389
288
390
##### Insert a new document
289
391
290
- ES::_id (3)->insert([
392
+ ES::id (3)->insert([
291
393
"title" => "Test document",
292
394
"content" => "Sample content"
293
395
]);
@@ -319,7 +421,7 @@ Index and type names setted in query will override values the configuration file
319
421
320
422
##### Update an existing document
321
423
322
- ES::_id (3)->update([
424
+ ES::id (3)->update([
323
425
"title" => "Test document",
324
426
"content" => "sample content"
325
427
]);
@@ -332,11 +434,11 @@ Index and type names setted in query will override values the configuration file
332
434
333
435
##### Incrementing field
334
436
335
- ES::_id (3)->increment("views");
437
+ ES::id (3)->increment("views");
336
438
337
439
Document has _id = 3 will be incremented by 1.
338
440
339
- ES::_id (3)->increment("views", 3);
441
+ ES::id (3)->increment("views", 3);
340
442
341
443
Document has _id = 3 will be incremented by 3.
342
444
@@ -346,11 +448,11 @@ Index and type names setted in query will override values the configuration file
346
448
347
449
##### Decrementing field
348
450
349
- ES::_id (3)->decrement("views");
451
+ ES::id (3)->decrement("views");
350
452
351
453
Document has _id = 3 will be decremented by 1.
352
454
353
- ES::_id (3)->decrement("views", 3);
455
+ ES::id (3)->decrement("views", 3);
354
456
355
457
Document has _id = 3 will be decremented by 3.
356
458
@@ -363,36 +465,34 @@ Index and type names setted in query will override values the configuration file
363
465
364
466
# icrement field by script
365
467
366
- ES::_id (3)->script(
468
+ ES::id (3)->script(
367
469
"ctx._source.$field -= params.count",
368
470
["count" => 1]
369
471
);
370
472
371
473
# add php tag to tags array list
372
474
373
- ES::_id (3)->script(
475
+ ES::id (3)->script(
374
476
"ctx._source.tags.add(params.tag)",
375
477
["tag" => "php"]
376
478
);
377
479
378
480
# delete the doc if the tags field contain mongodb, otherwise it does nothing (noop)
379
481
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" }",
382
484
["tag" => "mongodb"]
383
485
);
384
486
385
487
>
386
488
387
489
##### Delete a document
388
490
389
- ES::_id (3)->delete();
491
+ ES::id (3)->delete();
390
492
391
493
Document has _id = 3 will be deleted.
392
494
393
495
[id is required]
394
496
395
497
396
- ` Good luck `
397
-
398
- ` Dont forget to send a feedback.. `
498
+ ` Have a happy searching.. `
0 commit comments