Skip to content

Commit 4f4d403

Browse files
committed
supplement code notation
1 parent d98f079 commit 4f4d403

File tree

4 files changed

+103
-32
lines changed

4 files changed

+103
-32
lines changed

src/Connection.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,53 @@
55
class Connection
66
{
77
/**
8+
* Elasticsearch client instance
9+
*
810
* @var EsClient
911
*/
1012
protected $client;
1113

14+
/**
15+
* Query builder
16+
*
17+
* @var
18+
*/
1219
protected $queryBuilder;
1320

21+
/**
22+
* The elasticsearch connection configuration options.
23+
*
24+
* @var array
25+
*/
1426
protected $config;
1527

28+
/**
29+
* Index name for elasticsearch connection
30+
*
31+
* @var
32+
*/
1633
protected $index;
1734

35+
/**
36+
* type name for elasticsearch connection
37+
*
38+
* @var
39+
*/
1840
protected $type;
1941

42+
/**
43+
* time zone (usually using in range query)
44+
*
45+
* @var
46+
*/
2047
public $timeZone;
2148

49+
/**
50+
* Connection constructor.
51+
*
52+
* @param EsClient $client
53+
* @param $config
54+
*/
2255
public function __construct(EsClient $client, $config)
2356
{
2457
$this->client = $client;

src/ConnectionFactory.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,40 @@
55

66
class ConnectionFactory
77
{
8+
/**
9+
* Elasticserach client builder
10+
*
11+
* @var ClientBuilder
12+
*/
813
protected $clientBuilder;
914

15+
/**
16+
* ConnectionFactory constructor.
17+
*
18+
* @param ClientBuilder $clientBuilder
19+
*/
1020
public function __construct(ClientBuilder $clientBuilder)
1121
{
1222
$this->clientBuilder = $clientBuilder;
1323
}
1424

25+
/**
26+
* Establish a elasticsearch connection based on the configuration
27+
*
28+
* @param array $config
29+
* @return Connection
30+
*/
1531
public function make(array $config)
1632
{
1733
return $this->createConnection($config);
1834
}
1935

36+
/**
37+
* Create elasticsearch connection
38+
*
39+
* @param array $config
40+
* @return Connection
41+
*/
2042
protected function createConnection(array $config)
2143
{
2244
$host = array_only($config, 'host', 'port', 'scheme', 'user', 'pass');

src/QueryBuilder.php

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,42 @@
44
class QueryBuilder
55
{
66
/**
7-
* select 字段列表
7+
* fields fetched in query
8+
*
89
* @var array
910
*/
1011
protected $selectedFields = [];
1112
/**
12-
* 区间范围条件
13+
* range condition
14+
*
1315
* @var array
1416
*/
1517
protected $range;
1618

1719
/**
18-
* term条件
20+
* term condition
21+
*
1922
* @var array
2023
*/
2124
protected $terms;
2225

2326
/**
24-
* 排序规则
27+
* order rules
28+
*
2529
* @var array
2630
*/
2731
protected $orders;
2832

2933
/**
30-
* 结果集起始文档距搜索结果首个文档的距离
34+
* fetched item's offset apart from the first item in the whole result of a search
35+
*
3136
* @var string
3237
*/
3338
protected $from;
3439

3540
/**
36-
* 返回搜素结果文档的数目
41+
* result size
42+
*
3743
* @var string
3844
*/
3945
protected $size;
@@ -102,7 +108,8 @@ public function initQueryBody()
102108

103109

104110
/**
105-
* 为搜索设置时间范围条件
111+
* set time range condition for query
112+
*
106113
* @param $start
107114
* @param $end
108115
* @return $this
@@ -123,7 +130,7 @@ public function setTimeRange($start, $end)
123130
}
124131

125132
/**
126-
* 为搜索添加term条件
133+
* add term condition for query
127134
* @param $term
128135
* @param $value
129136
* @return $this
@@ -135,7 +142,8 @@ public function term($term, $value)
135142
}
136143

137144
/**
138-
* 设置搜索结果的排序方式
145+
* specify data will be sorted through field in asc or desc
146+
*
139147
* @param $field
140148
* @param $value
141149
* @return $this
@@ -147,17 +155,20 @@ public function order($field, $value)
147155
}
148156

149157
/**
150-
* 设置搜索按照时间降序返回文档
158+
* order documents in descending time
159+
*
151160
* @return $this
152161
*/
153-
public function newest()
162+
public function latest()
154163
{
155164
$this->order('@timestamp', 'desc');
156165
return $this;
157166
}
158167

159168
/**
160-
* 设置搜索按照时间升序返回文档
169+
* order documents in ascending time
170+
*
171+
* @return $this
161172
*/
162173
public function oldest()
163174
{
@@ -166,7 +177,7 @@ public function oldest()
166177
}
167178

168179
/**
169-
* 设置搜索结果_source里只包含给定的字段
180+
* set fields include in _source in query result
170181
* @param array $fields
171182
* @return $this
172183
*/
@@ -178,15 +189,16 @@ public function select(array $fields = [])
178189
}
179190

180191
/**
181-
* 清除selectedFields里的值
192+
* clear $selectedFields
182193
*/
183194
protected function clearSelect()
184195
{
185196
$this->selectedFields = [];
186197
}
187198

188199
/**
189-
* 设置返回的第一条文档距离结果集首个文档的距离(文档数)
200+
* Set the distance of the first document returned from the first document in the whole query result set
201+
*
190202
* @param $offset
191203
* @return $this
192204
*/
@@ -197,7 +209,8 @@ public function from($offset)
197209
}
198210

199211
/**
200-
* 设置要返回的文档数目
212+
* set size of return result
213+
*
201214
* @param $size
202215
* @return $this
203216
*/
@@ -220,14 +233,14 @@ public function scrollSize($scrollSize)
220233
}
221234

222235
/**
223-
* 组成搜索条件的Query
224-
* Query示例:
236+
* compose query based on all options we set before
237+
* Query Example:
225238
*
226239
* [
227-
* 'index' => 'logstash-bussiness_weixin_bridge_page*',
228-
* 'type' => 'bussiness_weixin_bridge_page',
229-
* 'scroll'=> '30s',//scroll镜像生存时间维持30秒, 在调用每次进行scroll搜索时会重新设置镜像的生存时间,
230-
* 'size' => '50',//result size for per shard scroll created
240+
* 'index' => 'index name ',
241+
* 'type' => 'type name',
242+
* 'scroll'=> '30s',// how long between scroll requests. should be small!
243+
* 'size' => '50', //results size for per shard scroll created
231244
* 'body' => [
232245
* 'query' => [
233246
* 'bool' => [
@@ -301,7 +314,8 @@ public function composeQuery()
301314

302315

303316
/**
304-
* 搜索并返回文档
317+
* fetch then return documents
318+
*
305319
* @return array
306320
*/
307321
public function get()
@@ -314,7 +328,8 @@ public function get()
314328
}
315329

316330
/**
317-
* 返回匹配搜索条件的文档数
331+
* count matching documents
332+
*
318333
* @return int
319334
*/
320335
public function count()
@@ -332,9 +347,11 @@ public function count()
332347
}
333348

334349
/**
335-
* 遍历搜索结果
336-
* 分页遍历搜索结果请不要循环用from和size来取数据, 应该用更高效的scroll api, 更多信息查看下面的文档
350+
* iterate documents matching search options
351+
* this is more efficient than specify from and size in query body, more about scroll you can checkout
337352
* @documentaion https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_search_operations.html#_scrolling
353+
*
354+
* @return array
338355
*/
339356
public function scroll()
340357
{

src/config/config.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,19 @@
1616
| Default Elastic Connection Name
1717
|--------------------------------------------------------------------------
1818
|
19-
| Here you may specify which of the database connections below you wish
20-
| to use as your default connection for all database work. Of course
21-
| you may use many connections at once using the Database library.
19+
| Here you may specify which of the connections below you wish
20+
| to use as your default connection for all elasticsearch's work. Of course
21+
| you may use many connections at once using the Elastic library.
2222
|
2323
*/
2424

2525
'default' => env('ELASTIC_CONNECTION', 'default'),
2626

2727
/*
2828
|--------------------------------------------------------------------------
29-
| ElasticSearch 连接的配置信息
29+
| ElasticSearch connection configuration
3030
|--------------------------------------------------------------------------
31-
| index和type是ElasticSearch执行时操作才需要的配置信息, 其它几项配置信息是连接
32-
| ElasticSearch时需要的配置信息
31+
|
3332
*/
3433

3534
'connections' => [

0 commit comments

Comments
 (0)