55use PDPhilip \Elasticsearch \DSL \Bridge ;
66use Elasticsearch \ClientBuilder ;
77use Illuminate \Database \Connection as BaseConnection ;
8- use Illuminate \Support \Arr ;
98use Illuminate \Support \Str ;
10- use InvalidArgumentException ;
11- use phpDocumentor \Reflection \Types \Scalar ;
129use RuntimeException ;
1310
1411
1512class Connection extends BaseConnection
1613{
17-
14+
1815 protected $ client ;
1916 protected $ index ;
2017 protected $ maxSize ;
2118 protected $ indexPrefix ;
22-
23-
19+
20+
2421 public function __construct (array $ config )
2522 {
2623 $ this ->config = $ config ;
27-
24+
2825 if (!empty ($ config ['index_prefix ' ])) {
2926 $ this ->indexPrefix = $ config ['index_prefix ' ];
3027 }
31-
28+
3229 $ this ->client = $ this ->buildConnection ();
33-
30+
3431 $ this ->useDefaultPostProcessor ();
35-
32+
3633 $ this ->useDefaultSchemaGrammar ();
37-
34+
3835 $ this ->useDefaultQueryGrammar ();
39-
36+
4037 }
41-
38+
39+ public function setIndexPrefix ($ newPrefix )
40+ {
41+ $ this ->indexPrefix = $ newPrefix ;
42+ }
43+
4244 public function getIndexPrefix ()
4345 {
4446 return $ this ->indexPrefix ;
4547 }
46-
47-
48+
49+
4850 public function getTablePrefix ()
4951 {
5052 return $ this ->getIndexPrefix ();
5153 }
52-
54+
5355 public function setIndex ($ index )
5456 {
5557 $ this ->index = $ index ;
@@ -58,98 +60,99 @@ public function setIndex($index)
5860 $ this ->index = $ this ->indexPrefix .'_ ' .$ index ;
5961 }
6062 }
61-
63+
6264 return $ this ->getIndex ();
6365 }
64-
66+
6567 public function getSchemaGrammar ()
6668 {
6769 return new Schema \Grammar ($ this );
6870 }
69-
71+
7072 public function getIndex ()
7173 {
7274 return $ this ->index ;
7375 }
74-
76+
7577 public function setMaxSize ($ value )
7678 {
7779 $ this ->maxSize = $ value ;
7880 }
79-
81+
8082 public function table ($ table , $ as = null )
8183 {
82- return $ this ->setIndex ($ table );
84+ $ query = new Query \Builder ($ this , new Query \Processor ());
85+
86+ return $ query ->from ($ table );
8387 }
84-
85-
88+
8689 /**
8790 * @inheritdoc
8891 */
8992 public function getSchemaBuilder ()
9093 {
9194 return new Schema \Builder ($ this );
9295 }
93-
94-
96+
97+
9598 /**
9699 * @inheritdoc
97100 */
98101 public function disconnect ()
99102 {
100103 unset($ this ->connection );
101104 }
102-
103-
105+
106+
104107 /**
105108 * @inheritdoc
106109 */
107110 public function getDriverName ()
108111 {
109112 return 'elasticsearch ' ;
110113 }
111-
114+
112115 /**
113116 * @inheritdoc
114117 */
115118 protected function getDefaultPostProcessor ()
116119 {
117120 return new Query \Processor ();
118121 }
119-
122+
120123 /**
121124 * @inheritdoc
122125 */
123126 protected function getDefaultQueryGrammar ()
124127 {
125128 return new Query \Grammar ();
126129 }
127-
130+
128131 /**
129132 * @inheritdoc
130133 */
131134 protected function getDefaultSchemaGrammar ()
132135 {
133136 return new Schema \Grammar ();
134137 }
135-
136-
138+
139+
137140 //----------------------------------------------------------------------
138141 // Connection Builder
139142 //----------------------------------------------------------------------
140-
143+
141144 protected function buildConnection ()
142145 {
143146 $ type = config ('database.connections.elasticsearch.auth_type ' ) ?? null ;
144147 $ type = strtolower ($ type );
145148 if (!in_array ($ type , ['http ' , 'cloud ' , 'api ' ])) {
146149 throw new RuntimeException ('Invalid [auth_type] in database config. Must be: http, cloud or api ' );
147150 }
148-
151+
149152 return $ this ->{'_ ' .$ type .'Connection ' }();
150-
153+
151154 }
152-
155+
153156 protected function _httpConnection ()
154157 {
155158 $ hosts = config ('database.connections.elasticsearch.hosts ' ) ?? null ;
@@ -163,10 +166,10 @@ protected function _httpConnection()
163166 if ($ certPath ) {
164167 $ cb ->setSSLVerification ($ certPath );
165168 }
166-
169+
167170 return $ cb ->build ();
168171 }
169-
172+
170173 protected function _cloudConnection ()
171174 {
172175 $ cloudId = config ('database.connections.elasticsearch.cloud_id ' ) ?? null ;
@@ -184,11 +187,11 @@ protected function _cloudConnection()
184187 if ($ certPath ) {
185188 $ cb ->setSSLVerification ($ certPath );
186189 }
187-
190+
188191 return $ cb ->build ();
189192 }
190-
191-
193+
194+
192195 protected function _apiConnection ()
193196 {
194197 $ apiId = config ('database.connections.elasticsearch.api_id ' ) ?? null ;
@@ -198,19 +201,23 @@ protected function _apiConnection()
198201 if ($ certPath ) {
199202 $ cb ->setSSLVerification ($ certPath );
200203 }
201-
204+
202205 return $ cb ->build ();
203206 }
204-
205-
207+
208+
206209 //----------------------------------------------------------------------
207210 // Dynamic call routing to DSL bridge
208211 //----------------------------------------------------------------------
209-
212+
210213 public function __call ($ method , $ parameters )
211214 {
215+ if (!$ this ->index ) {
216+ $ this ->index = $ this ->indexPrefix .'* ' ;
217+ }
218+
212219 $ bridge = new Bridge ($ this ->client , $ this ->index , $ this ->maxSize );
213-
220+
214221 return $ bridge ->{'process ' .Str::studly ($ method )}(...$ parameters );
215222 }
216223}
0 commit comments