4
4
5
5
namespace CodelyTv \Shared \Infrastructure \Persistence \Elasticsearch ;
6
6
7
- use CodelyTv \Shared \Domain \ValueObject \ Uuid ;
7
+ use CodelyTv \Shared \Domain \Criteria \ Criteria ;
8
8
use CodelyTv \Shared \Infrastructure \Elasticsearch \ElasticsearchClient ;
9
9
use Elasticsearch \Common \Exceptions \Missing404Exception ;
10
- use function Lambdish \Phunctional \get ;
11
10
use function Lambdish \Phunctional \get_in ;
12
11
use function Lambdish \Phunctional \map ;
13
12
@@ -27,39 +26,36 @@ protected function persist(string $id, array $plainBody): void
27
26
$ this ->client ->persist ($ this ->aggregateName (), $ id , $ plainBody );
28
27
}
29
28
30
- protected function searchInElasticById (Uuid $ id ): ?array
29
+ protected function searchAllInElastic (): array
30
+ {
31
+ return $ this ->searchRawElasticsearchQuery ([]);
32
+ }
33
+
34
+ protected function searchRawElasticsearchQuery (array $ params ): array
31
35
{
32
36
try {
33
- $ result = $ this ->client ->get (
34
- [
35
- 'index ' => $ this ->indexName (),
36
- 'type ' => $ this ->typeName (),
37
- 'id ' => $ id ->value (),
38
- ]
39
- );
40
-
41
- return get ('_source ' , $ result );
37
+ $ result = $ this ->client ->client ()->search (array_merge (['index ' => $ this ->indexName ()], $ params ));
38
+
39
+ $ hits = get_in (['hits ' , 'hits ' ], $ result , []);
40
+
41
+ return map ($ this ->elasticValuesExtractor (), $ hits );
42
42
} catch (Missing404Exception $ unused ) {
43
- return null ;
43
+ return [] ;
44
44
}
45
45
}
46
46
47
- protected function indexName ( ): string
47
+ public function searchByCriteria ( Criteria $ criteria ): array
48
48
{
49
- return sprintf ('%s_%s ' , $ this ->client ->indexPrefix (), $ this ->aggregateName ());
50
- }
49
+ $ converter = new ElasticsearchCriteriaConverter ();
51
50
52
- protected function searchAllInElastic (): array
53
- {
54
- $ result = $ this ->client ->client ()->search (
55
- [
56
- 'index ' => $ this ->indexName (),
57
- ]
58
- );
51
+ $ query = $ converter ->convert ($ criteria );
59
52
60
- $ hits = get_in (['hits ' , 'hits ' ], $ result , []);
53
+ return $ this ->searchRawElasticsearchQuery ($ query );
54
+ }
61
55
62
- return map ($ this ->elasticValuesExtractor (), $ hits );
56
+ protected function indexName (): string
57
+ {
58
+ return sprintf ('%s_%s ' , $ this ->client ->indexPrefix (), $ this ->aggregateName ());
63
59
}
64
60
65
61
private function elasticValuesExtractor (): callable
0 commit comments