|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Maslosoft\Manganel\Adapters\Finder; |
|
4
|
|
|
|
|
5
|
|
|
use Maslosoft\Mangan\Interfaces\Adapters\FinderAdapterInterface; |
|
6
|
|
|
use Maslosoft\Mangan\Interfaces\CriteriaInterface; |
|
7
|
|
|
use Maslosoft\Manganel\QueryBuilder; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* ElasticSearchAdapter |
|
11
|
|
|
* |
|
12
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
|
13
|
|
|
*/ |
|
14
|
|
|
class ElasticSearchAdapter implements FinderAdapterInterface |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* |
|
19
|
|
|
* @var QueryBuilder |
|
20
|
|
|
*/ |
|
21
|
|
|
private $qb = null; |
|
22
|
|
|
|
|
23
|
28 |
|
public function __construct($models) |
|
24
|
|
|
{ |
|
25
|
28 |
|
$this->qb = new QueryBuilder(); |
|
26
|
28 |
|
$this->qb->setModels($models); |
|
27
|
28 |
|
} |
|
28
|
|
|
|
|
29
|
5 |
|
public function count(CriteriaInterface $criteria) |
|
30
|
|
|
{ |
|
31
|
5 |
|
return $this->qb->setCriteria($criteria)->count(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
20 |
|
public function findMany(CriteriaInterface $criteria, $fields = array()) |
|
35
|
|
|
{ |
|
36
|
20 |
|
$this->prepare($criteria, $fields); |
|
37
|
20 |
|
return new ElasticSearchCursor($this->qb); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
5 |
|
public function findOne(CriteriaInterface $criteria, $fields = array()) |
|
41
|
|
|
{ |
|
42
|
5 |
|
$this->prepare($criteria, $fields); |
|
43
|
5 |
|
$data = (new ElasticSearchCursor(($this->qb)))->current(); |
|
44
|
5 |
|
if (false === $data) |
|
45
|
|
|
{ |
|
46
|
|
|
return null; |
|
47
|
|
|
} |
|
48
|
5 |
|
return $data; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* |
|
53
|
|
|
* @internal Used for debugging purposes, should not be used for any manipulations! |
|
54
|
|
|
* @return QueryBuilder |
|
55
|
|
|
*/ |
|
56
|
1 |
|
public function getQueryBuilder() |
|
57
|
|
|
{ |
|
58
|
1 |
|
return $this->qb; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
25 |
|
private function prepare(CriteriaInterface $criteria, $fields) |
|
62
|
|
|
{ |
|
63
|
25 |
|
$this->qb->setCriteria($criteria); |
|
64
|
25 |
|
if (!empty($fields)) |
|
65
|
|
|
{ |
|
66
|
5 |
|
$selected = array_flip($fields); |
|
67
|
5 |
|
foreach ($selected as $index => $value) |
|
68
|
|
|
{ |
|
69
|
5 |
|
$selected[$index] = true; |
|
70
|
|
|
} |
|
71
|
5 |
|
$this->qb->getCriteria()->select($fields); |
|
72
|
|
|
} |
|
73
|
25 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|