@@ -17,15 +17,17 @@ class Connection extends BaseConnection
1717 protected $ index ;
1818 protected $ maxSize ;
1919 protected $ indexPrefix ;
20-
20+ protected $ retires = null ; //null will use default
21+ protected $ sslVerification = true ;
22+ protected $ elasticMetaHeader = true ;
23+ protected $ rebuild = false ;
24+ protected $ allowIdSort = false ;
2125
2226 public function __construct (array $ config )
2327 {
2428 $ this ->config = $ config ;
2529
26- if (!empty ($ config ['index_prefix ' ])) {
27- $ this ->indexPrefix = $ config ['index_prefix ' ];
28- }
30+ $ this ->setOptions ($ config );
2931
3032 $ this ->client = $ this ->buildConnection ();
3133
@@ -37,7 +39,26 @@ public function __construct(array $config)
3739
3840 }
3941
40- public function getIndexPrefix (): string
42+ public function setOptions ($ config )
43+ {
44+ if (!empty ($ config ['index_prefix ' ])) {
45+ $ this ->indexPrefix = $ config ['index_prefix ' ];
46+ }
47+ if (!empty ($ config ['options ' ]['allow_id_sort ' ])) {
48+ $ this ->allowIdSort = $ config ['options ' ]['allow_id_sort ' ];
49+ }
50+ if (!empty ($ config ['options ' ]['ssl_verification ' ])) {
51+ $ this ->sslVerification = $ config ['options ' ]['ssl_verification ' ];
52+ }
53+ if (!empty ($ config ['options ' ]['retires ' ])) {
54+ $ this ->retires = $ config ['options ' ]['retires ' ];
55+ }
56+ if (!empty ($ config ['options ' ]['meta_header ' ])) {
57+ $ this ->elasticMetaHeader = $ config ['options ' ]['meta_header ' ];
58+ }
59+ }
60+
61+ public function getIndexPrefix (): string |null
4162 {
4263 return $ this ->indexPrefix ;
4364 }
@@ -48,7 +69,7 @@ public function setIndexPrefix($newPrefix): void
4869 }
4970
5071
51- public function getTablePrefix (): string
72+ public function getTablePrefix (): string | null
5273 {
5374 return $ this ->getIndexPrefix ();
5475 }
@@ -57,7 +78,7 @@ public function setIndex($index): string
5778 {
5879 $ this ->index = $ index ;
5980 if ($ this ->indexPrefix ) {
60- if (!(strpos ($ this ->index , $ this ->indexPrefix .'_ ' ) !== false )) {
81+ if (!(str_contains ($ this ->index , $ this ->indexPrefix .'_ ' ))) {
6182 $ this ->index = $ this ->indexPrefix .'_ ' .$ index ;
6283 }
6384 }
@@ -138,6 +159,26 @@ protected function getDefaultSchemaGrammar()
138159 return new Schema \Grammar ();
139160 }
140161
162+ public function rebuildConnection ()
163+ {
164+ $ this ->rebuild = true ;
165+ }
166+
167+ public function getClient ()
168+ {
169+ return $ this ->client ;
170+ }
171+
172+ public function getMaxSize ()
173+ {
174+ return $ this ->maxSize ;
175+ }
176+
177+ public function getAllowIdSort ()
178+ {
179+ return $ this ->allowIdSort ;
180+ }
181+
141182
142183 //----------------------------------------------------------------------
143184 // Connection Builder
@@ -160,13 +201,15 @@ protected function _httpConnection(): Client
160201 $ hosts = config ('database.connections.elasticsearch.hosts ' ) ?? null ;
161202 $ username = config ('database.connections.elasticsearch.username ' ) ?? null ;
162203 $ pass = config ('database.connections.elasticsearch.password ' ) ?? null ;
163- $ certPath = config ('database.connections.elasticsearch.ssl_cert ' ) ?? null ;
204+ $ apiId = config ('database.connections.elasticsearch.api_id ' ) ?? null ;
205+ $ apiKey = config ('database.connections.elasticsearch.api_key ' ) ?? null ;
164206 $ cb = ClientBuilder::create ()->setHosts ($ hosts );
207+ $ cb = $ this ->_builderOptions ($ cb );
165208 if ($ username && $ pass ) {
166- $ cb ->setBasicAuthentication ($ username , $ pass )-> build () ;
209+ $ cb ->setBasicAuthentication ($ username , $ pass );
167210 }
168- if ($ certPath ) {
169- $ cb ->setCABundle ( $ certPath );
211+ if ($ apiKey ) {
212+ $ cb ->setApiKey ( $ apiKey , $ apiId );
170213 }
171214
172215 return $ cb ->build ();
@@ -179,20 +222,45 @@ protected function _cloudConnection(): Client
179222 $ pass = config ('database.connections.elasticsearch.password ' ) ?? null ;
180223 $ apiId = config ('database.connections.elasticsearch.api_id ' ) ?? null ;
181224 $ apiKey = config ('database.connections.elasticsearch.api_key ' ) ?? null ;
182- $ certPath = config ( ' database.connections.elasticsearch.ssl_cert ' ) ?? null ;
225+
183226 $ cb = ClientBuilder::create ()->setElasticCloudId ($ cloudId );
184- if ($ apiId && $ apiKey ) {
185- $ cb ->setApiKey ($ apiKey , $ apiId )->build ();
186- } elseif ($ username && $ pass ) {
187- $ cb ->setBasicAuthentication ($ username , $ pass )->build ();
227+ $ cb = $ this ->_builderOptions ($ cb );
228+ if ($ username && $ pass ) {
229+ $ cb ->setBasicAuthentication ($ username , $ pass );
188230 }
189- if ($ certPath ) {
190- $ cb ->setSSLVerification ( $ certPath );
231+ if ($ apiKey ) {
232+ $ cb ->setApiKey ( $ apiKey , $ apiId );
191233 }
192234
235+
193236 return $ cb ->build ();
194237 }
195238
239+ protected function _builderOptions ($ cb )
240+ {
241+ $ cb ->setSSLVerification ($ this ->sslVerification );
242+ $ cb ->setElasticMetaHeader ($ this ->elasticMetaHeader );
243+ if ($ this ->retires ) {
244+ $ cb ->setRetries ($ this ->retires );
245+ }
246+ $ caBundle = config ('database.connections.elasticsearch.ssl_cert ' ) ?? null ;
247+ if ($ caBundle ) {
248+ $ cb ->setCABundle ($ caBundle );
249+ }
250+ $ sslCert = config ('database.connections.elasticsearch.ssl.cert ' ) ?? null ;
251+ $ sslCertPassword = config ('database.connections.elasticsearch.ssl.cert_password ' ) ?? null ;
252+ $ sslKey = config ('database.connections.elasticsearch.ssl.key ' ) ?? null ;
253+ $ sslKeyPassword = config ('database.connections.elasticsearch.ssl.key_password ' ) ?? null ;
254+ if ($ sslCert ) {
255+ $ cb ->setSSLCert ($ sslCert , $ sslCertPassword );
256+ }
257+ if ($ sslKey ) {
258+ $ cb ->setSSLKey ($ sslKey , $ sslKeyPassword );
259+ }
260+
261+ return $ cb ;
262+ }
263+
196264
197265 //----------------------------------------------------------------------
198266 // Dynamic call routing to DSL bridge
@@ -203,8 +271,11 @@ public function __call($method, $parameters)
203271 if (!$ this ->index ) {
204272 $ this ->index = $ this ->indexPrefix .'* ' ;
205273 }
206-
207- $ bridge = new Bridge ($ this ->client , $ this ->index , $ this ->maxSize , $ this ->indexPrefix );
274+ if ($ this ->rebuild ) {
275+ $ this ->client = $ this ->buildConnection ();
276+ $ this ->rebuild = false ;
277+ }
278+ $ bridge = new Bridge ($ this );
208279
209280 return $ bridge ->{'process ' .Str::studly ($ method )}(...$ parameters );
210281 }
0 commit comments