Skip to content

Commit 5788006

Browse files
authored
#100616 (#12)
* #100616 * Fix styling --------- Co-authored-by: Ms_Natali <golubkova@greensight.ru> Co-authored-by: MsNatali <MsNatali@users.noreply.github.com>
1 parent c8e418b commit 5788006

19 files changed

+907
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.idea
22
/vendor
33
/node_modules
4+
/coverage
45

56
*.cache
67
phpunit.xml

README.md

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,6 @@ $aggs = $aggQuery
139139
'offers',
140140
fn(AggregationsBuilder $builder) => $builder->where('seller_id', 10)->minmax('price', 'price')
141141
);
142-
143-
$aggs
144142

145143
```
146144

@@ -189,6 +187,53 @@ $aggQuery->composite(function (AggregationsBuilder $builder) {
189187
});
190188
```
191189

190+
## Suggesting
191+
192+
Suggest queries can be created like this
193+
194+
```php
195+
$sugQuery = ProductsIndex::suggest();
196+
197+
/** @var \Illuminate\Support\Collection $suggests */
198+
$suggests = $sugQuery->phrase('suggestName', 'name.trigram')
199+
->text('glves')
200+
->size(1)
201+
->shardSize(3)
202+
->get();
203+
204+
```
205+
206+
### Global suggest text
207+
208+
User can set global text like this
209+
210+
```php
211+
$sugQuery = ProductsIndex::suggest()->text('glves');
212+
213+
$sugQuery->phrase('suggestName1', 'name.trigram')->size(1)->shardSize(3);
214+
215+
$sugQuery->phrase('suggestName2', 'name.trigram');
216+
217+
/** @var \Illuminate\Support\Collection $suggests */
218+
$suggests = $sugQuery->get();
219+
220+
```
221+
222+
223+
### Suggester types
224+
225+
Term suggester:
226+
227+
```php
228+
$aggQuery->term('suggestName', 'name.trigram')->text('glves')->...->get();
229+
```
230+
231+
Phrase Suggester:
232+
233+
```php
234+
$aggQuery->phrase('suggestName', 'name.trigram')->text('glves')->...->get();
235+
```
236+
192237
## Additional methods
193238

194239
```php
@@ -200,6 +245,10 @@ $index->bulk(); // Send bulk request
200245
$index->get(); // Send get request
201246
$index->documentDelete(); // Send documentDelete request
202247
$index->deleteByQuery(); // Send deleteByQuery request
248+
249+
$index->catIndices();
250+
$index->indicesDelete();
251+
$index->indicesRefresh();
203252
```
204253

205254
## Query Log

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"scripts": {
4040
"cs": "php-cs-fixer fix --config .php-cs-fixer.php",
4141
"test": "vendor/bin/phpunit",
42-
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
42+
"test-coverage": "XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html coverage"
4343

4444
},
4545
"config": {

src/Concerns/InteractsWithIndex.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Ensi\LaravelElasticQuery\Contracts\SearchIndex;
77
use Ensi\LaravelElasticQuery\ElasticClient;
88
use Ensi\LaravelElasticQuery\Search\SearchQuery;
9+
use Ensi\LaravelElasticQuery\Suggesting\SuggestQuery;
910
use Exception;
1011

1112
trait InteractsWithIndex
@@ -75,6 +76,11 @@ public function indicesDelete(string $index)
7576
return $this->resolveClient()->indicesDelete($index);
7677
}
7778

79+
public function indicesRefresh()
80+
{
81+
return $this->resolveClient()->indicesRefresh($this->indexName());
82+
}
83+
7884
public static function query(): SearchQuery
7985
{
8086
return new SearchQuery(new static());
@@ -85,6 +91,11 @@ public static function aggregate(): AggregationsQuery
8591
return new AggregationsQuery(new static());
8692
}
8793

94+
public static function suggest(): SuggestQuery
95+
{
96+
return new SuggestQuery(new static());
97+
}
98+
8899
protected function resolveClient(): ElasticClient
89100
{
90101
$this->client ??= resolve(ElasticClient::class);

src/ElasticClient.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,14 @@ public function catIndices(string $indexName, ?array $getFields = null)
8282
return $this->client->cat()->indices($params);
8383
}
8484

85-
public function indicesDelete(string $index)
85+
public function indicesDelete(string $indexName)
8686
{
87-
return $this->client->indices()->delete(['index' => $index]);
87+
return $this->client->indices()->delete(['index' => $indexName]);
88+
}
89+
90+
public function indicesRefresh(string $indexName)
91+
{
92+
return $this->client->indices()->refresh(['index' => $indexName]);
8893
}
8994

9095
public function enableQueryLog(): void
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Ensi\LaravelElasticQuery\Suggesting\Enums;
4+
5+
final class SuggestMode
6+
{
7+
public const MISSING = 'missing';
8+
public const POPULAR = 'popular';
9+
public const ALWAYS = 'always';
10+
11+
public static function cases(): array
12+
{
13+
return [
14+
self::MISSING,
15+
self::POPULAR,
16+
self::ALWAYS,
17+
];
18+
}
19+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Ensi\LaravelElasticQuery\Suggesting\Enums;
4+
5+
final class SuggestSort
6+
{
7+
public const SCORE = 'score';
8+
public const FREQUENCY = 'frequency';
9+
10+
public static function cases(): array
11+
{
12+
return [
13+
self::SCORE,
14+
self::FREQUENCY,
15+
];
16+
}
17+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Ensi\LaravelElasticQuery\Suggesting\Enums;
4+
5+
final class SuggestStringDistance
6+
{
7+
public const INTERNAL = 'internal';
8+
public const DAMERAU_LEVENSHTEIN = 'damerau_levenshtein';
9+
public const LEVENSHTEIN = 'levenshtein';
10+
public const JARO_WINKLER = 'jaro_winkler';
11+
public const NGRAM = 'ngram';
12+
13+
public static function cases(): array
14+
{
15+
return [
16+
self::INTERNAL,
17+
self::DAMERAU_LEVENSHTEIN,
18+
self::LEVENSHTEIN,
19+
self::JARO_WINKLER,
20+
self::NGRAM,
21+
];
22+
}
23+
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
3+
namespace Ensi\LaravelElasticQuery\Suggesting\Request;
4+
5+
use Webmozart\Assert\Assert;
6+
7+
class PhraseSuggester implements Suggester
8+
{
9+
// basic phrase suggest api parameters
10+
protected ?string $text = null;
11+
protected ?int $gramSize = null;
12+
protected ?float $realWordErrorLikelihood = null;
13+
protected ?float $confidence = null;
14+
protected ?float $maxErrors = null;
15+
protected ?string $separator = null;
16+
protected ?int $size = null;
17+
protected ?string $analyzer = null;
18+
protected ?int $shardSize = null;
19+
protected ?string $highlightPreTag = null;
20+
protected ?string $highlightPostTag = null;
21+
22+
public function __construct(protected string $name, protected string $field)
23+
{
24+
Assert::stringNotEmpty(trim($name));
25+
Assert::stringNotEmpty(trim($field));
26+
}
27+
28+
public function toDSL(): array
29+
{
30+
return [
31+
"text" => $this->text,
32+
"phrase" => array_filter([
33+
"field" => $this->field,
34+
35+
"gram_size" => $this->gramSize,
36+
"real_word_error_likelihood" => $this->realWordErrorLikelihood,
37+
"confidence" => $this->confidence,
38+
"max_errors" => $this->maxErrors,
39+
"separator" => $this->separator,
40+
"size" => $this->size,
41+
"analyzer" => $this->analyzer,
42+
"shard_size" => $this->shardSize,
43+
"highlight" => array_filter([
44+
"pre_tag" => $this->highlightPreTag,
45+
"post_tag" => $this->highlightPostTag,
46+
]) ?: null,
47+
// todo collate
48+
// todo smoothing models
49+
// todo direct_generator
50+
]),
51+
];
52+
}
53+
54+
public function name(): string
55+
{
56+
return $this->name;
57+
}
58+
59+
public function text(string $text): self
60+
{
61+
Assert::stringNotEmpty(trim($text));
62+
63+
$this->text = $text;
64+
65+
return $this;
66+
}
67+
68+
public function gramSize(int $gramSize): self
69+
{
70+
$this->gramSize = $gramSize;
71+
72+
return $this;
73+
}
74+
75+
public function realWordErrorLikelihood(float $realWordErrorLikelihood): self
76+
{
77+
$this->realWordErrorLikelihood = $realWordErrorLikelihood;
78+
79+
return $this;
80+
}
81+
82+
public function confidence(float $confidence): self
83+
{
84+
$this->confidence = $confidence;
85+
86+
return $this;
87+
}
88+
89+
public function maxErrors(float $maxErrors): self
90+
{
91+
$this->maxErrors = $maxErrors;
92+
93+
return $this;
94+
}
95+
96+
public function separator(string $separator): self
97+
{
98+
$this->separator = $separator;
99+
100+
return $this;
101+
}
102+
103+
public function size(int $size): self
104+
{
105+
$this->size = $size;
106+
107+
return $this;
108+
}
109+
110+
public function analyzer(string $analyzer): self
111+
{
112+
$this->analyzer = $analyzer;
113+
114+
return $this;
115+
}
116+
117+
public function shardSize(int $shardSize): self
118+
{
119+
$this->shardSize = $shardSize;
120+
121+
return $this;
122+
}
123+
124+
public function highlight(string $highlightPreTag, string $highlightPostTag): self
125+
{
126+
$this->highlightPreTag = $highlightPreTag;
127+
$this->highlightPostTag = $highlightPostTag;
128+
129+
return $this;
130+
}
131+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Ensi\LaravelElasticQuery\Suggesting\Request;
4+
5+
use Ensi\LaravelElasticQuery\Contracts\DSLAware;
6+
7+
interface Suggester extends DSLAware
8+
{
9+
public function name(): string;
10+
}

0 commit comments

Comments
 (0)