forked from jfarrell/elasticsearch_php_client
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathElasticSearchClient.php
236 lines (209 loc) · 5.18 KB
/
ElasticSearchClient.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
require_once 'lib/ElasticSearchException.php';
require_once 'lib/ElasticSearchDSLStringify.php';
require_once 'lib/builder/ElasticSearchDSLBuilder.php';
require_once 'lib/ElasticSearchBulkQueue.php';
require_once 'lib/transport/ElasticSearchTransport.php';
require_once 'lib/transport/ElasticSearchTransportHTTP.php';
require_once 'lib/transport/ElasticSearchTransportMemcached.php';
if (isset($GLOBALS['THRIFT_ROOT'])) {
require_once 'lib/transport/ElasticSearchTransportThrift.php';
}
class ElasticSearchClient
{
private $transport, $index, $type, $bulk_queue;
/**
* Construct search client
*
* @return ElasticSearch
* @param ElasticSearchTransport $transport
* @param string $index
* @param string $type
*/
public function __construct($transport, $index, $type)
{
$this->index = $index;
$this->type = $type;
$this->transport = $transport;
$this->setIndex($index);
$this->setType($type);
$this->bulk_queue = new ElasticSearchBulkQueue();
}
/**
* Change what index to go against
* @return void
* @param mixed $index
*/
public function setIndex($index)
{
if (is_array($index)) {
$index = implode(",", array_filter($index));
}
$this->index = $index;
$this->transport->setIndex($index);
}
/**
* Change what types to act against
* @return void
* @param mixed $type
*/
public function setType($type)
{
if (is_array($type)) {
$type = implode(",", array_filter($type));
}
$this->type = $type;
$this->transport->setType($type);
}
/**
* Fetch a document by its id
*
* @return array
* @param mixed $id Optional
*/
public function get($id, $verbose=false)
{
if (empty($id)) {
throw new Exception('empty id on get call');
}
$response = $this->transport->request(
array(
$this->type,
$id,
),
"GET");
return ($verbose) ? $response : $response['_source'];
}
/**
* Perform a request
*
* @return array
* @param mixed $id Optional
*/
public function request($path, $method, $payload, $verbose=false)
{
$path = array_merge((array) $this->type, (array) $path);
$response = $this->transport->request($path, $method, $payload);
return ($verbose) ? $response : $response['_source'];
}
/**
* Index a new document or update it if existing
*
* @return array
* @param array $document
* @param mixed $id Optional
* @param array $options Allow sending query parameters to control indexing further
* _refresh_ *bool* If set to true, immediately refresh the shard after indexing
*/
public function index($document, $id=false, array $options = array())
{
return $this->transport->index($document, $id, $options);
}
/**
* Perform search, this is the sweet spot
*
* @return array
* @param array $document
*/
public function search($query)
{
$start = $this->getMicroTime();
$result = $this->transport->search($query);
$result['time'] = $this->getMicroTime() - $start;
return $result;
}
/**
* Flush this index/type combination
*
* @return array
* @param mixed $id If id is supplied, delete that id for this index
* if not wipe the entire index
* @param array $options Parameters to pass to delete action
*/
public function delete($id=false, array $options = array())
{
return $this->transport->delete($id, $options);
}
/**
* Flush this index/type combination
*
* @return array
* @param mixed $query Text or array based query to delete everything that matches
* @param array $options Parameters to pass to delete action
*/
public function deleteByQuery($query, array $options = array())
{
return $this->transport->deleteByQuery($query, $options);
}
/**
* Get the number of items currently in the bulk queue.
*
* @return int The number of items in the bulk queue.
*/
public function getBulkCount()
{
return $this->bulk_queue->queueLength();
}
/**
* Get the bulk queue itself.
*
* @return array The raw bulk queue.
*/
public function getBulkQueue()
{
return $this->bulk_queue->getQueue();
}
/**
* Add an index operation to the bulk queue.
* Use just as you would the regular index,
* but be sure to submit the queue!
*/
public function bulkIndex(
$document,
$id = false,
array $params = array())
{
foreach (explode(',', $this->index) as $index) {
foreach (explode(',', $this->type) as $type) {
$this->bulk_queue->index(
$document,
$index,
$this->type,
$id,
$params);
}
}
}
/**
* Add a delete operation to the bulk queue.
* Use just as you would the regular delete,
* but be sure to submit the queue!
*/
public function bulkDelete($id, array $params = array())
{
foreach (explode(',', $this->index) as $index) {
foreach (explode(',', $this->type) as $type) {
$this->bulk_queue->delete(
$index,
$this->type,
$id,
$params);
}
}
}
/**
* Submit the bulk queue for processing.
*/
public function bulkSubmit(array $params = array())
{
$this->bulk_queue->setParams($params);
$result = $this->transport->bulk($this->bulk_queue);
$this->bulk_queue = new ElasticSearchBulkQueue();
return $result;
}
private function getMicroTime()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
}