1
|
|
|
<?php |
2
|
|
|
namespace Triadev\Es\Dsl\Dsl; |
3
|
|
|
|
4
|
|
|
use Illuminate\Support\Facades\Log; |
5
|
|
|
use ONGR\ElasticsearchDSL\BuilderInterface; |
6
|
|
|
use ONGR\ElasticsearchDSL\Search as OngrSearch; |
7
|
|
|
use Prometheus\Exception\MetricsRegistrationException; |
8
|
|
|
use Triadev\Es\Dsl\Dsl\Query\Compound; |
9
|
|
|
use Triadev\Es\Dsl\Dsl\Query\Fulltext; |
10
|
|
|
use Triadev\Es\Dsl\Dsl\Query\Geo; |
11
|
|
|
use Triadev\Es\Dsl\Dsl\Query\InnerHit; |
12
|
|
|
use Triadev\Es\Dsl\Dsl\Query\Joining; |
13
|
|
|
use Triadev\Es\Dsl\Dsl\Query\Specialized; |
14
|
|
|
use Triadev\Es\Dsl\Dsl\Query\TermLevel; |
15
|
|
|
use Triadev\PrometheusExporter\Contract\PrometheusExporterContract; |
16
|
|
|
|
17
|
|
|
abstract class AbstractSearch |
18
|
|
|
{ |
19
|
|
|
/** @var OngrSearch */ |
20
|
|
|
protected $search; |
21
|
|
|
|
22
|
|
|
/** @var string|null */ |
23
|
|
|
private $esIndex; |
24
|
|
|
|
25
|
|
|
/** @var string|null */ |
26
|
|
|
private $esType; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* AbstractDsl constructor. |
30
|
|
|
* @param OngrSearch|null $search |
31
|
|
|
* @param string|null $esIndex |
32
|
|
|
* @param string|null $esType |
33
|
|
|
*/ |
34
|
23 |
|
public function __construct( |
35
|
|
|
?OngrSearch $search = null, |
36
|
|
|
?string $esIndex = null, |
37
|
|
|
?string $esType = null |
38
|
|
|
) { |
39
|
23 |
|
$this->search = $search ?: new OngrSearch(); |
40
|
|
|
|
41
|
23 |
|
$this->esIndex = $esIndex; |
42
|
23 |
|
$this->esType = $esType; |
43
|
23 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Overwrite the default elasticsearch index |
47
|
|
|
* |
48
|
|
|
* @param string $index |
49
|
|
|
* @return AbstractDsl|Search|TermLevel|Compound|Fulltext|Geo|InnerHit|Joining|Specialized |
50
|
|
|
*/ |
51
|
1 |
|
public function esIndex(string $index): AbstractSearch |
52
|
|
|
{ |
53
|
1 |
|
$this->esIndex = $index; |
54
|
1 |
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get elasticsearch index |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
14 |
|
public function getEsIndex(): string |
63
|
|
|
{ |
64
|
14 |
|
return $this->esIndex ?: config('laravel-elasticsearch-dsl.index'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Overwrite the default elasticsearch type |
69
|
|
|
* |
70
|
|
|
* @param string $type |
71
|
|
|
* @return AbstractDsl|Search|TermLevel|Compound|Fulltext|Geo|InnerHit|Joining|Specialized |
72
|
|
|
*/ |
73
|
1 |
|
public function esType(string $type): AbstractSearch |
74
|
|
|
{ |
75
|
1 |
|
$this->esType = $type; |
76
|
1 |
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get elasticsearch type |
81
|
|
|
* |
82
|
|
|
* @return string|null |
83
|
|
|
*/ |
84
|
14 |
|
public function getEsType(): ?string |
85
|
|
|
{ |
86
|
14 |
|
return $this->esType; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get current search |
91
|
|
|
* |
92
|
|
|
* @return OngrSearch |
93
|
|
|
*/ |
94
|
14 |
|
protected function getCurrentSearch(): OngrSearch |
95
|
|
|
{ |
96
|
14 |
|
return $this->search; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* To dsl |
101
|
|
|
* |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
23 |
|
public function toDsl(): array |
105
|
|
|
{ |
106
|
23 |
|
return $this->search->toArray(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get search |
111
|
|
|
* |
112
|
|
|
* @return OngrSearch |
113
|
|
|
*/ |
114
|
2 |
|
public function getSearch(): OngrSearch |
115
|
|
|
{ |
116
|
2 |
|
return $this->search; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get query |
121
|
|
|
* |
122
|
|
|
* @return BuilderInterface |
123
|
|
|
*/ |
124
|
11 |
|
public function getQuery(): BuilderInterface |
125
|
|
|
{ |
126
|
11 |
|
return $this->search->getQueries(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Init metric start time |
131
|
|
|
* |
132
|
|
|
* @return float|null |
133
|
|
|
*/ |
134
|
1 |
|
protected function initMetricStartTime(): ?float |
135
|
|
|
{ |
136
|
1 |
|
return config('laravel-elasticsearch-dsl.metrics.enabled', false) ? |
137
|
1 |
|
microtime(true) : |
138
|
1 |
|
null; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Set histogram metric |
143
|
|
|
* |
144
|
|
|
* @param float $startTime |
145
|
|
|
* @param string $handler |
146
|
|
|
*/ |
147
|
1 |
|
protected function setHistogramMetric(float $startTime, string $handler) |
148
|
|
|
{ |
149
|
|
|
try { |
150
|
1 |
|
$this->getPrometheusExporter()->setHistogram( |
151
|
1 |
|
'query_duration_milliseconds', |
152
|
1 |
|
'Get the query duration.', |
153
|
1 |
|
round((microtime(true) - $startTime) * 1000.0), |
154
|
1 |
|
'triadev_laravel_elasticsearch_dsl', |
155
|
1 |
|
['handler'], |
156
|
1 |
|
[$handler], |
157
|
1 |
|
config( |
158
|
1 |
|
sprintf( |
159
|
1 |
|
'laravel-elasticsearch-dsl.metrics.buckets.%s', |
160
|
1 |
|
$handler |
161
|
|
|
) |
162
|
|
|
) |
163
|
|
|
); |
164
|
|
|
} catch (MetricsRegistrationException $e) { |
165
|
|
|
Log::error('Prometheus exporter metrics registration failed.'); |
166
|
|
|
} |
167
|
1 |
|
} |
168
|
|
|
|
169
|
1 |
|
private function getPrometheusExporter() : PrometheusExporterContract |
170
|
|
|
{ |
171
|
1 |
|
return app(PrometheusExporterContract::class); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|