Skip to content

Commit 6cce7a1

Browse files
authored
Update README.md
v1.1.0
1 parent d24199f commit 6cce7a1

File tree

1 file changed

+102
-14
lines changed

1 file changed

+102
-14
lines changed

README.md

Lines changed: 102 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
## Introduction
22
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)
512

613
## Installation
714

@@ -17,34 +24,81 @@ Set Elasticsearch base URL in `.env` file
1724
ELASTICSEARCH_URL=http://localhost:9200
1825
```
1926

27+
## Custom Indices
28+
Custom Indices work similarly to Laravel models.
29+
First, you need to define your own class that extends AbstractIndex:
2030

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:
2245

2346
```php
24-
public function search(string $index, array $data): SearchResponseDto;
47+
use App\Indices\LogIndex;
2548

26-
public function find(string $index, string|int $id): ?FindResponseDto;
49+
...
2750

28-
public function findOrFail(string $index, string|int $id): FindResponseDto;
51+
LogIndex::query()->where('type', 'error')->get();
52+
```
2953

30-
public function create(string $index, string|int $id, array $data): IndexResponseDto;
54+
### General usage
55+
There are three options for making requests:
3156

32-
public function update(string $index, string|int $id, array $data): IndexResponseDto;
57+
1. Using builder
58+
```php
59+
use Olekjs\Elasticsearch\Builder\Builder;
3360

34-
public function delete(string $index, string|int $id): IndexResponseDto;
61+
...
3562

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;
3775

38-
public function searchWhereKeyword(string $index, string $field, string $value): SearchResponseDto;
76+
...
3977

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+
```
4185

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;
4389

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+
]);
4598
```
4699

47100
### Examples
101+
48102
1. Index the new document
49103
```php
50104
use Olekjs\Elasticsearch\Client;
@@ -80,3 +134,37 @@ $client->findOrFail(
80134
);
81135

82136
```
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

Comments
 (0)