|
1
|
|
|
<?php namespace Nord\Lumen\Elasticsearch; |
|
2
|
|
|
|
|
3
|
|
|
use Elasticsearch\Client; |
|
4
|
|
|
use Nord\Lumen\Elasticsearch\Contracts\ElasticsearchServiceContract; |
|
5
|
|
|
use Nord\Lumen\Elasticsearch\Search\Search; |
|
6
|
|
|
use Nord\Lumen\Elasticsearch\Search\Sort; |
|
7
|
|
|
|
|
8
|
|
|
class ElasticsearchService implements ElasticsearchServiceContract |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @var Client |
|
13
|
|
|
*/ |
|
14
|
|
|
private $client; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* ElasticsearchService constructor. |
|
18
|
|
|
* |
|
19
|
|
|
* @param Client $client |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct(Client $client) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->client = $client; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @inheritdoc |
|
28
|
|
|
*/ |
|
29
|
|
|
public function search(array $params = []) |
|
30
|
|
|
{ |
|
31
|
|
|
$params = IndexNamePrefixer::getPrefixedIndexParameters($params); |
|
32
|
|
|
|
|
33
|
|
|
return $this->client->search($params); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @inheritdoc |
|
38
|
|
|
*/ |
|
39
|
|
|
public function index(array $params = []) |
|
40
|
|
|
{ |
|
41
|
|
|
$params = IndexNamePrefixer::getPrefixedIndexParameters($params); |
|
42
|
|
|
|
|
43
|
|
|
return $this->client->index($params); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @inheritdoc |
|
48
|
|
|
*/ |
|
49
|
|
|
public function reindex(array $params = []) |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->client->reindex($params); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @inheritdoc |
|
56
|
|
|
*/ |
|
57
|
|
|
public function updateByQuery(array $params = []) |
|
58
|
|
|
{ |
|
59
|
|
|
$params = IndexNamePrefixer::getPrefixedIndexParameters($params); |
|
60
|
|
|
|
|
61
|
|
|
return $this->client->updateByQuery($params); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @inheritdoc |
|
66
|
|
|
*/ |
|
67
|
|
|
public function bulk(array $params = []) |
|
68
|
|
|
{ |
|
69
|
|
|
$params = IndexNamePrefixer::getPrefixedIndexParameters($params); |
|
70
|
|
|
|
|
71
|
|
|
return $this->client->bulk($params); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @inheritdoc |
|
76
|
|
|
*/ |
|
77
|
|
|
public function delete(array $params = []) |
|
78
|
|
|
{ |
|
79
|
|
|
$params = IndexNamePrefixer::getPrefixedIndexParameters($params); |
|
80
|
|
|
|
|
81
|
|
|
return $this->client->delete($params); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @inheritdoc |
|
86
|
|
|
*/ |
|
87
|
|
|
public function deleteByQuery(array $params = []) |
|
88
|
|
|
{ |
|
89
|
|
|
$params = IndexNamePrefixer::getPrefixedIndexParameters($params); |
|
90
|
|
|
|
|
91
|
|
|
return $this->client->deleteByQuery($params); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @inheritdoc |
|
96
|
|
|
*/ |
|
97
|
|
|
public function tasks() |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->client->tasks(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @inheritdoc |
|
104
|
|
|
*/ |
|
105
|
|
|
public function create(array $params = []) |
|
106
|
|
|
{ |
|
107
|
|
|
$params = IndexNamePrefixer::getPrefixedIndexParameters($params); |
|
108
|
|
|
|
|
109
|
|
|
return $this->client->create($params); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @inheritdoc |
|
114
|
|
|
*/ |
|
115
|
|
|
public function exists(array $params = []) |
|
116
|
|
|
{ |
|
117
|
|
|
$params = IndexNamePrefixer::getPrefixedIndexParameters($params); |
|
118
|
|
|
|
|
119
|
|
|
return $this->client->exists($params); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @inheritdoc |
|
124
|
|
|
*/ |
|
125
|
|
|
public function indices() |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->client->indices(); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @inheritdoc |
|
132
|
|
|
*/ |
|
133
|
|
|
public function createSearch() |
|
134
|
|
|
{ |
|
135
|
|
|
return new Search(); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @inheritdoc |
|
140
|
|
|
*/ |
|
141
|
|
|
public function createSort() |
|
142
|
|
|
{ |
|
143
|
|
|
return new Sort(); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @inheritdoc |
|
148
|
|
|
*/ |
|
149
|
|
|
public function execute(Search $search) |
|
150
|
|
|
{ |
|
151
|
|
|
$index = IndexNamePrefixer::getPrefixedIndexName($search->getIndex()); |
|
152
|
|
|
|
|
153
|
|
|
return $this->search([ |
|
154
|
|
|
'index' => $index, |
|
155
|
|
|
'type' => $search->getType(), |
|
156
|
|
|
'body' => $search->buildBody(), |
|
157
|
|
|
]); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @inheritdoc |
|
162
|
|
|
*/ |
|
163
|
|
|
public function count(Search $search): int |
|
164
|
|
|
{ |
|
165
|
|
|
$index = IndexNamePrefixer::getPrefixedIndexName($search->getIndex()); |
|
166
|
|
|
|
|
167
|
|
|
return $this->client->count([ |
|
168
|
|
|
'index' => $index, |
|
169
|
|
|
'type' => $search->getType(), |
|
170
|
|
|
'body' => $search->buildBody(), |
|
171
|
|
|
])['count']; |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|