1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: mina |
5
|
|
|
* Date: 3/17/17 |
6
|
|
|
* Time: 2:20 AM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace MAbadir\ElasticLaravel; |
10
|
|
|
use Elasticsearch\ClientBuilder as Elastic; |
11
|
|
|
|
12
|
|
|
class ElasticClient |
13
|
|
|
{ |
14
|
|
|
protected $index, $params, $client; |
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Connection constructor. |
18
|
|
|
*/ |
19
|
|
|
public function __construct() |
20
|
|
|
{ |
21
|
|
|
$this->params = [ |
22
|
|
|
'index' => config('elastic.index') |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
$hosts = [ |
26
|
|
|
[ |
27
|
|
|
'host' => config('elastic.host'), |
28
|
|
|
'port' => config('elastic.port'), |
29
|
|
|
// 'scheme' => 'https', |
|
|
|
|
30
|
|
|
// 'user' => 'username', |
31
|
|
|
// 'pass' => 'password!#$?*abc' |
32
|
|
|
] |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
$this->client = Elastic::create() // Instantiate a new ClientBuilder |
36
|
|
|
->setHosts($hosts) // Set the hosts |
37
|
|
|
->build(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Merges Parameters received by client with index parameter |
42
|
|
|
* |
43
|
|
|
* @param $params |
44
|
|
|
* |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
|
|
protected function mergeParams($params) |
48
|
|
|
{ |
49
|
|
|
return $this->params + $params; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Returns the Elastic Search Client instance |
54
|
|
|
* @return \Elasticsearch\Client |
55
|
|
|
*/ |
56
|
|
|
public function getClient() |
57
|
|
|
{ |
58
|
|
|
return $this->client; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Creates the index with the given $params settings |
63
|
|
|
* |
64
|
|
|
* @param $params |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
public function createIndex($params = []) |
69
|
|
|
{ |
70
|
|
|
return $this->client->indices()->create($this->mergeParams($params)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Drops the index with the given $params settings |
75
|
|
|
* |
76
|
|
|
* @param $params |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
public function dropIndex($params = []) |
81
|
|
|
{ |
82
|
|
|
return $this->client->indices()->delete($this->mergeParams($params)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Indexes a document of the given parameters |
87
|
|
|
* |
88
|
|
|
* @param $params |
89
|
|
|
* |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
|
|
public function index($params) |
93
|
|
|
{ |
94
|
|
|
return $this->client->index($this->mergeParams($params)); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Retrieves a document of the given parameters |
99
|
|
|
* |
100
|
|
|
* @param $params |
101
|
|
|
* |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
public function get($params) |
105
|
|
|
{ |
106
|
|
|
return $this->client->get($this->mergeParams($params)); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Updates a document of the given parameters |
111
|
|
|
* |
112
|
|
|
* @param $params |
113
|
|
|
* |
114
|
|
|
* @return array |
115
|
|
|
*/ |
116
|
|
|
public function update($params) |
117
|
|
|
{ |
118
|
|
|
return $this->client->update($this->mergeParams($params)); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Deletes a document of the given parameters |
123
|
|
|
* |
124
|
|
|
* @param $params |
125
|
|
|
* |
126
|
|
|
* @return array |
127
|
|
|
*/ |
128
|
|
|
public function delete($params) |
129
|
|
|
{ |
130
|
|
|
return $this->client->delete($this->mergeParams($params)); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Searches the index with the given params |
135
|
|
|
* |
136
|
|
|
* @param $params |
137
|
|
|
* |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
|
|
public function search($params) |
141
|
|
|
{ |
142
|
|
|
return $this->client->search($this->mergeParams($params)); |
143
|
|
|
} |
144
|
|
|
} |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.