1
|
|
|
<?php |
2
|
|
|
namespace Triadev\Es; |
3
|
|
|
|
4
|
|
|
use Triadev\Es\Business\AbstractElasticsearch; |
5
|
|
|
use Triadev\Es\Contract\ElasticsearchSearchContract; |
6
|
|
|
use Triadev\Es\Models\Hit; |
7
|
|
|
use Triadev\Es\Models\SearchResult; |
8
|
|
|
use Triadev\Es\Models\Shards; |
9
|
|
|
|
10
|
|
|
class ElasticsearchSearch extends AbstractElasticsearch implements ElasticsearchSearchContract |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Search |
14
|
|
|
* |
15
|
|
|
* @param array $index |
16
|
|
|
* @param array $type |
17
|
|
|
* @param array $body |
18
|
|
|
* @param array $params |
19
|
|
|
* @param string|null $version |
20
|
|
|
* @param bool $raw |
21
|
|
|
* @return array|SearchResult |
22
|
|
|
*/ |
23
|
|
|
public function search( |
24
|
|
|
array $index, |
25
|
|
|
array $type, |
26
|
|
|
array $body = [], |
27
|
|
|
array $params = [], |
28
|
|
|
string $version = null, |
29
|
|
|
bool $raw = false |
30
|
|
|
) { |
31
|
|
|
$indices = []; |
32
|
|
|
foreach ($index as $i) { |
33
|
|
|
$indices[] = $this->createIndexWithVersion($i, $version); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$params['index'] = implode(',', $indices); |
37
|
|
|
$params['type'] = implode(',', $type); |
38
|
|
|
$params['body'] = $body; |
39
|
|
|
|
40
|
|
|
$result = $this->getElasticsearchClient()->search($params); |
41
|
|
|
|
42
|
|
|
if (!$raw) { |
43
|
|
|
$searchResult = new SearchResult(); |
44
|
|
|
$searchResult->setTook($result['took']); |
45
|
|
|
$searchResult->setTimedOut($result['timed_out']); |
46
|
|
|
$searchResult->setTotal($result['hits']['total']); |
47
|
|
|
$searchResult->setMaxScore($result['hits']['max_score']); |
48
|
|
|
|
49
|
|
|
if (array_key_exists('_scroll_id', $result)) { |
50
|
|
|
$searchResult->setScrollId($result['_scroll_id']); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$shards = new Shards(); |
54
|
|
|
$shards->setTotal($result['_shards']['total']); |
55
|
|
|
$shards->setSuccessful($result['_shards']['successful']); |
56
|
|
|
$shards->setFailed($result['_shards']['failed']); |
57
|
|
|
$searchResult->setShards($shards); |
58
|
|
|
|
59
|
|
|
if ($result['hits']['total'] > 0) { |
60
|
|
|
foreach ($result['hits']['hits'] as $hit) { |
61
|
|
|
$h = new Hit(); |
62
|
|
|
$h->setIndex($hit['_index']); |
63
|
|
|
$h->setType($hit['_type']); |
64
|
|
|
$h->setId($hit['_id']); |
65
|
|
|
$h->setScore($hit['_score']); |
66
|
|
|
$h->setSource($hit['_source']); |
67
|
|
|
|
68
|
|
|
if (array_key_exists('inner_hits', $hit)) { |
69
|
|
|
$h->setInnerHits($hit['inner_hits']); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (array_key_exists('_routing', $hit)) { |
73
|
|
|
$h->setRouting($hit['_routing']); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (array_key_exists('_parent', $hit)) { |
77
|
|
|
$h->setParent($hit['_parent']); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$searchResult->addHit($h); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if (array_key_exists('aggregations', $result)) { |
85
|
|
|
$searchResult->setAggs($result['aggregations']); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $searchResult; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $result; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Scroll |
96
|
|
|
* |
97
|
|
|
* @param string $scrollId |
98
|
|
|
* @param string $scroll |
99
|
|
|
* @param array $body |
100
|
|
|
* @param array $params |
101
|
|
|
* @param bool $raw |
102
|
|
|
* @return array|SearchResult |
103
|
|
|
*/ |
104
|
|
|
public function scroll( |
105
|
|
|
string $scrollId, |
106
|
|
|
string $scroll, |
107
|
|
|
array $body = [], |
108
|
|
|
array $params = [], |
109
|
|
|
bool $raw = false |
110
|
|
|
) { |
111
|
|
|
$body['scroll_id'] = $scrollId; |
112
|
|
|
$body['scroll'] = $scroll; |
113
|
|
|
|
114
|
|
|
$params['body'] = $body; |
115
|
|
|
|
116
|
|
|
$result = $this->getElasticsearchClient()->scroll($params); |
117
|
|
|
|
118
|
|
|
if (!$raw) { |
119
|
|
|
$searchResult = new SearchResult(); |
120
|
|
|
$searchResult->setTook($result['took']); |
121
|
|
|
$searchResult->setTimedOut($result['timed_out']); |
122
|
|
|
$searchResult->setTotal($result['hits']['total']); |
123
|
|
|
$searchResult->setMaxScore($result['hits']['max_score']); |
124
|
|
|
|
125
|
|
|
if (array_key_exists('_scroll_id', $result)) { |
126
|
|
|
$searchResult->setScrollId($result['_scroll_id']); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$shards = new Shards(); |
130
|
|
|
$shards->setTotal($result['_shards']['total']); |
131
|
|
|
$shards->setSuccessful($result['_shards']['successful']); |
132
|
|
|
$shards->setFailed($result['_shards']['failed']); |
133
|
|
|
$searchResult->setShards($shards); |
134
|
|
|
|
135
|
|
|
if ($result['hits']['total'] > 0) { |
136
|
|
|
foreach ($result['hits']['hits'] as $hit) { |
137
|
|
|
$h = new Hit(); |
138
|
|
|
$h->setIndex($hit['_index']); |
139
|
|
|
$h->setType($hit['_type']); |
140
|
|
|
$h->setId($hit['_id']); |
141
|
|
|
$h->setScore($hit['_score']); |
142
|
|
|
$h->setSource($hit['_source']); |
143
|
|
|
|
144
|
|
|
if (array_key_exists('inner_hits', $hit)) { |
145
|
|
|
$h->setInnerHits($hit['inner_hits']); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if (array_key_exists('_routing', $hit)) { |
149
|
|
|
$h->setRouting($hit['_routing']); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if (array_key_exists('_parent', $hit)) { |
153
|
|
|
$h->setParent($hit['_parent']); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$searchResult->addHit($h); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
if (array_key_exists('aggregations', $result)) { |
161
|
|
|
$searchResult->setAggs($result['aggregations']); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $searchResult; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $result; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|