8
8
use Laravel \Scout \Builder ;
9
9
use Laravel \Scout \Engines \Engine ;
10
10
use TeamTNT \TNTSearch \Exceptions \IndexNotFoundException ;
11
+ use TeamTNT \TNTSearch \TNTGeoSearch ;
11
12
use TeamTNT \TNTSearch \TNTSearch ;
12
13
13
14
class TNTSearchEngine extends Engine
@@ -17,6 +18,11 @@ class TNTSearchEngine extends Engine
17
18
*/
18
19
protected $ tnt ;
19
20
21
+ /**
22
+ * @var TNTGeoSearch
23
+ */
24
+ protected $ geotnt ;
25
+
20
26
/**
21
27
* @var Builder
22
28
*/
@@ -26,10 +32,12 @@ class TNTSearchEngine extends Engine
26
32
* Create a new engine instance.
27
33
*
28
34
* @param TNTSearch $tnt
35
+ * @param TNTGeoSearch|null $geotnt
29
36
*/
30
- public function __construct (TNTSearch $ tnt )
37
+ public function __construct (TNTSearch $ tnt, TNTGeoSearch $ geotnt = null )
31
38
{
32
39
$ this ->tnt = $ tnt ;
40
+ $ this ->geotnt = $ geotnt ;
33
41
}
34
42
35
43
/**
@@ -46,8 +54,17 @@ public function update($models)
46
54
$ index = $ this ->tnt ->getIndex ();
47
55
$ index ->setPrimaryKey ($ models ->first ()->getKeyName ());
48
56
57
+ $ geoindex = null ;
58
+ if ($ this ->geotnt ) {
59
+ $ this ->geotnt ->selectIndex ("{$ models ->first ()->searchableAs ()}.geoindex " );
60
+ $ geoindex = $ this ->geotnt ->getIndex ();
61
+ $ geoindex ->loadConfig ($ this ->geotnt ->config );
62
+ $ geoindex ->setPrimaryKey ($ models ->first ()->getKeyName ());
63
+ $ geoindex ->indexBeginTransaction ();
64
+ }
65
+
49
66
$ index ->indexBeginTransaction ();
50
- $ models ->each (function ($ model ) use ($ index ) {
67
+ $ models ->each (function ($ model ) use ($ index, $ geoindex ) {
51
68
$ array = $ model ->toSearchableArray ();
52
69
53
70
if (empty ($ array )) {
@@ -56,11 +73,25 @@ public function update($models)
56
73
57
74
if ($ model ->getKey ()) {
58
75
$ index ->update ($ model ->getKey (), $ array );
76
+ if ($ geoindex ) {
77
+ $ geoindex ->prepareAndExecuteStatement (
78
+ 'DELETE FROM locations WHERE doc_id = :documentId; ' ,
79
+ [['key ' => ':documentId ' , 'value ' => $ model ->getKey ()]]
80
+ );
81
+ }
59
82
} else {
60
83
$ index ->insert ($ array );
61
84
}
85
+ if ($ geoindex && !empty ($ array ['longitude ' ]) && !empty ($ array ['latitude ' ])) {
86
+ $ array ['longitude ' ] = (float ) $ array ['longitude ' ];
87
+ $ array ['latitude ' ] = (float ) $ array ['latitude ' ];
88
+ $ geoindex ->insert ($ array );
89
+ }
62
90
});
63
91
$ index ->indexEndTransaction ();
92
+ if ($ this ->geotnt ) {
93
+ $ geoindex ->indexEndTransaction ();
94
+ }
64
95
}
65
96
66
97
/**
@@ -78,6 +109,17 @@ public function delete($models)
78
109
$ index = $ this ->tnt ->getIndex ();
79
110
$ index ->setPrimaryKey ($ model ->getKeyName ());
80
111
$ index ->delete ($ model ->getKey ());
112
+
113
+ if ($ this ->geotnt ) {
114
+ $ this ->geotnt ->selectIndex ("{$ model ->searchableAs ()}.geoindex " );
115
+ $ index = $ this ->geotnt ->getIndex ();
116
+ $ index ->loadConfig ($ this ->geotnt ->config );
117
+ $ index ->setPrimaryKey ($ model ->getKeyName ());
118
+ $ index ->prepareAndExecuteStatement (
119
+ 'DELETE FROM locations WHERE doc_id = :documentId; ' ,
120
+ [['key ' => ':documentId ' , 'value ' => $ model ->getKey ()]]
121
+ );
122
+ }
81
123
});
82
124
}
83
125
@@ -145,6 +187,9 @@ protected function performSearch(Builder $builder, array $options = [])
145
187
$ index = $ builder ->index ?: $ builder ->model ->searchableAs ();
146
188
$ limit = $ builder ->limit ?: 10000 ;
147
189
$ this ->tnt ->selectIndex ("{$ index }.index " );
190
+ if ($ this ->geotnt ) {
191
+ $ this ->geotnt ->selectIndex ("{$ index }.geoindex " );
192
+ }
148
193
149
194
$ this ->builder = $ builder ;
150
195
@@ -160,6 +205,14 @@ protected function performSearch(Builder $builder, array $options = [])
160
205
$ options
161
206
);
162
207
}
208
+
209
+ if (is_array ($ builder ->query )) {
210
+ $ location = $ builder ->query ['location ' ];
211
+ $ distance = $ builder ->query ['distance ' ];
212
+ $ limit = array_key_exists ('limit ' , $ builder ->query ) ? $ builder ->query ['limit ' ] : 10 ;
213
+ return $ this ->geotnt ->findNearest ($ location , $ distance , $ limit );
214
+ }
215
+
163
216
if (isset ($ this ->tnt ->config ['searchBoolean ' ]) ? $ this ->tnt ->config ['searchBoolean ' ] : false ) {
164
217
return $ this ->tnt ->searchBoolean ($ builder ->query , $ limit );
165
218
} else {
@@ -260,6 +313,13 @@ public function initIndex($model)
260
313
$ indexer ->setDatabaseHandle ($ model ->getConnection ()->getPdo ());
261
314
$ indexer ->setPrimaryKey ($ model ->getKeyName ());
262
315
}
316
+ if ($ this ->geotnt && !file_exists ($ this ->tnt ->config ['storage ' ]."/ {$ indexName }.geoindex " )) {
317
+ $ indexer = $ this ->geotnt ->getIndex ();
318
+ $ indexer ->loadConfig ($ this ->geotnt ->config );
319
+ $ indexer ->createIndex ("$ indexName.geoindex " );
320
+ $ indexer ->setDatabaseHandle ($ model ->getConnection ()->getPdo ());
321
+ $ indexer ->setPrimaryKey ($ model ->getKeyName ());
322
+ }
263
323
}
264
324
265
325
/**
@@ -401,5 +461,12 @@ public function flush($model)
401
461
if (file_exists ($ pathToIndex )) {
402
462
unlink ($ pathToIndex );
403
463
}
464
+
465
+ if ($ this ->geotnt ){
466
+ $ pathToGeoIndex = $ this ->geotnt ->config ['storage ' ]."/ {$ indexName }.geoindex " ;
467
+ if (file_exists ($ pathToGeoIndex )) {
468
+ unlink ($ pathToGeoIndex );
469
+ }
470
+ }
404
471
}
405
472
}
0 commit comments