Skip to content

Commit ae83afa

Browse files
authored
Merge pull request #6 from ensi-platform/dev-match
Add match filter
2 parents 8f0e9ce + 57b37ef commit ae83afa

File tree

5 files changed

+65
-1
lines changed

5 files changed

+65
-1
lines changed

src/Filtering/AllowedFilter.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ public static function lessOrEqual(string $name, ?string $field = null): self
117117
return new static($name, new RangeFilterAction('<='), $field);
118118
}
119119

120+
public static function match(string $name, ?string $field): self
121+
{
122+
return new static($name, new MatchFilterAction(), $field);
123+
}
124+
120125
private function refineValue(mixed $value): mixed
121126
{
122127
if ($value === null) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Ensi\LaravelElasticQuerySpecification\Filtering;
4+
5+
use Ensi\LaravelElasticQuery\Contracts\BoolQuery;
6+
use Ensi\LaravelElasticQuerySpecification\Contracts\FilterAction;
7+
use Ensi\LaravelElasticQuerySpecification\Exceptions\InvalidQueryException;
8+
9+
class MatchFilterAction implements FilterAction
10+
{
11+
public function __invoke(BoolQuery $query, mixed $value, string $field): void
12+
{
13+
FilterValue::make($value)
14+
->whenSingle(fn (mixed $value) => $query->whereMatch($field, (string)$value))
15+
->whenMultiple(fn () => throw InvalidQueryException::notSupportMultipleValues($field));
16+
}
17+
}

tests/Data/ProductIndexSeeder.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ProductIndexSeeder extends IndexSeeder
1414
'active' => ['type' => 'boolean'],
1515

1616
'name' => ['type' => 'keyword', 'copy_to' => 'search_name'],
17-
'search_name' => ['type' => 'text'],
17+
'search_name' => ['type' => 'text', 'analyzer' => 'default'],
1818
'description' => ['type' => 'text'],
1919

2020
'code' => ['type' => 'keyword'],
@@ -40,4 +40,31 @@ class ProductIndexSeeder extends IndexSeeder
4040
],
4141
],
4242
];
43+
44+
protected array $settings = [
45+
'analysis' => [
46+
'filter' => [
47+
'english_stop' => [
48+
'type' => 'stop',
49+
'stopwords' => '_english_',
50+
],
51+
'english_stemmer' => [
52+
'type' => 'stemmer',
53+
'language' => 'english',
54+
],
55+
],
56+
'analyzer' => [
57+
'default' => [
58+
'type' => 'custom',
59+
'char_filter' => ['html_strip'],
60+
'tokenizer' => 'standard',
61+
'filter' => [
62+
'lowercase',
63+
'english_stop',
64+
'english_stemmer',
65+
],
66+
],
67+
],
68+
],
69+
];
4370
}

tests/Integration/FilteringTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,15 @@
7878
'less or equal' => [['rating__lte' => 5], [319, 328, 471]],
7979
'between' => [['rating__lte' => 7, 'rating__gte' => 5], [1, 328]],
8080
]);
81+
82+
test('match filter', function (string $query, array $expectedIds) {
83+
$spec = CompositeSpecification::new()
84+
->allowedFilters([
85+
AllowedFilter::match('name', 'search_name'),
86+
]);
87+
88+
searchQuery($spec, ['filter' => ['name' => $query]])->assertDocumentIds($expectedIds);
89+
})->with([
90+
'single' => ['water', [150]],
91+
'multiple' => ['gloves', [319, 471]],
92+
]);

tests/TestCase.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
use Ensi\LaravelElasticQuery\ElasticQuery;
66
use Ensi\LaravelElasticQuery\ElasticQueryServiceProvider;
77
use Ensi\LaravelElasticQuerySpecification\ElasticQuerySpecificationServiceProvider;
8+
use Ensi\LaravelElasticQuerySpecification\Tests\Data\ProductIndexSeeder;
89
use Orchestra\Testbench\TestCase as Orchestra;
910

1011
class TestCase extends Orchestra
1112
{
1213
protected function setUp(): void
1314
{
1415
parent::setUp();
16+
17+
ProductIndexSeeder::run();
1518
}
1619

1720
protected function tearDown(): void

0 commit comments

Comments
 (0)