1
1
## Introduction
2
2
This is a simple package that is used to integrate the Elasticsearch API with a Laravel project.
3
- The package is based on the Client class, which is not a builder.
4
- The package is still in development.
3
+
4
+ _ The package is still in development._
5
+
6
+ - [ Installation] ( #installation )
7
+ - [ Custom Indices] ( #custom-indices )
8
+ - [ General usage] ( #general-usage )
9
+ - [ Examples] ( #examples )
10
+ - [ Client class reference] ( #client-class-reference )
11
+ - [ Running Tests] ( #running-tests )
5
12
6
13
## Installation
7
14
@@ -17,34 +24,81 @@ Set Elasticsearch base URL in `.env` file
17
24
ELASTICSEARCH_URL=http://localhost:9200
18
25
```
19
26
27
+ ## Custom Indices
28
+ Custom Indices work similarly to Laravel models.
29
+ First, you need to define your own class that extends AbstractIndex:
20
30
21
- ## Client class reference
31
+ ``` php
32
+ <?php
33
+
34
+ namespace App\Indices;
35
+
36
+ use Olekjs\Elasticsearch\Contracts\AbstractIndex;
37
+
38
+ class LogIndex extends AbstractIndex
39
+ {
40
+ protected string $index = 'logs';
41
+ }
42
+ ```
43
+
44
+ You can then use Custom Index to perform a search operation without specifying an index name:
22
45
23
46
``` php
24
- public function search(string $index, array $data): SearchResponseDto ;
47
+ use App\Indices\LogIndex ;
25
48
26
- public function find(string $index, string|int $id): ?FindResponseDto;
49
+ ...
27
50
28
- public function findOrFail(string $index, string|int $id): FindResponseDto;
51
+ LogIndex::query()->where('type', 'error')->get();
52
+ ```
29
53
30
- public function create(string $index, string|int $id, array $data): IndexResponseDto;
54
+ ### General usage
55
+ There are three options for making requests:
31
56
32
- public function update(string $index, string|int $id, array $data): IndexResponseDto;
57
+ 1 . Using builder
58
+ ``` php
59
+ use Olekjs\Elasticsearch\Builder\Builder;
33
60
34
- public function delete(string $index, string|int $id): IndexResponseDto;
61
+ ...
35
62
36
- public function searchWhereIn(string $index, string $field, array $values): SearchResponseDto;
63
+ $results = Builder::query()
64
+ ->index('shops')
65
+ ->where('slug', 'test-slug')
66
+ ->where('email', 'test@test.com')
67
+ ->whereLike('name', 'test')
68
+ ->whereIn('_id', [123, 321])
69
+ ->get(); // Resturns SearchResponseDto
70
+ ```
71
+ 2 . Using Custom Index
72
+ How to use Custom Indices? See: Custom Indices
73
+ ``` php
74
+ use App\Indices\MyCustomIndex;
37
75
38
- public function searchWhereKeyword(string $index, string $field, string $value): SearchResponseDto;
76
+ ...
39
77
40
- public function searchWhereLike(string $index, string $field, string|int|float $value): SearchResponseDto;
78
+ $results = MyCustomIndex::query()
79
+ ->where('slug', 'test-slug')
80
+ ->where('email', 'test@test.com')
81
+ ->whereLike('name', 'test')
82
+ ->whereIn('_id', [123, 321])
83
+ ->get(); // Resturns SearchResponseDto
84
+ ```
41
85
42
- public function increment(string $index, string|int $id, string $field, int $value = 1): IndexResponseDto;
86
+ 3 . Directly using the Client class
87
+ ``` php
88
+ use Olekjs\Elasticsearch\Client;
43
89
44
- public function decrement(string $index, string|int $id, string $field, int $value = 1): IndexResponseDto;
90
+ ...
91
+
92
+ $client = new Client();
93
+ $client->search('logs', [
94
+ 'query' => [
95
+ 'bool' => ['filter' => ['term' => ['email' => 'test@test.com']]]
96
+ ]
97
+ ]);
45
98
```
46
99
47
100
### Examples
101
+
48
102
1 . Index the new document
49
103
``` php
50
104
use Olekjs\Elasticsearch\Client;
@@ -80,3 +134,37 @@ $client->findOrFail(
80
134
);
81
135
82
136
```
137
+
138
+ ## Client class reference
139
+
140
+ ``` php
141
+ public function search(string $index, array $data): SearchResponseDto;
142
+
143
+ public function find(string $index, string|int $id): ?FindResponseDto;
144
+
145
+ public function findOrFail(string $index, string|int $id): FindResponseDto;
146
+
147
+ public function create(string $index, string|int $id, array $data): IndexResponseDto;
148
+
149
+ public function update(string $index, string|int $id, array $data): IndexResponseDto;
150
+
151
+ public function delete(string $index, string|int $id): IndexResponseDto;
152
+
153
+ public function searchWhereIn(string $index, string $field, array $values): SearchResponseDto;
154
+
155
+ public function searchWhereKeyword(string $index, string $field, string $value): SearchResponseDto;
156
+
157
+ public function searchWhereLike(string $index, string $field, string|int|float $value): SearchResponseDto;
158
+
159
+ public function increment(string $index, string|int $id, string $field, int $value = 1): IndexResponseDto;
160
+
161
+ public function decrement(string $index, string|int $id, string $field, int $value = 1): IndexResponseDto;
162
+ ```
163
+
164
+ ## Running Tests
165
+
166
+ To run tests, run the following command
167
+
168
+ ``` bash
169
+ php vendor/bin/testbench package:test
170
+ ```
0 commit comments