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,21 +54,51 @@ 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 )) {
54
71
return ;
55
72
}
56
73
74
+ if ($ geoindex ) {
75
+ $ latitude = isset ($ array ['latitude ' ]) ? (float ) $ array ['latitude ' ] : null ;
76
+ $ longitude = isset ($ array ['longitude ' ]) ? (float ) $ array ['longitude ' ] : null ;
77
+ unset($ array ['longitude ' ]);
78
+ unset($ array ['latitude ' ]);
79
+ }
80
+
57
81
if ($ model ->getKey ()) {
58
82
$ index ->update ($ model ->getKey (), $ array );
83
+ if ($ geoindex ) {
84
+ $ geoindex ->prepareAndExecuteStatement (
85
+ 'DELETE FROM locations WHERE doc_id = :documentId; ' ,
86
+ [['key ' => ':documentId ' , 'value ' => $ model ->getKey ()]]
87
+ );
88
+ }
59
89
} else {
60
90
$ index ->insert ($ array );
61
91
}
92
+ if ($ geoindex && !empty ($ latitude ) && !empty ($ longitude )) {
93
+ $ array ['latitude ' ] = $ latitude ;
94
+ $ array ['longitude ' ] = $ longitude ;
95
+ $ geoindex ->insert ($ array );
96
+ }
62
97
});
63
98
$ index ->indexEndTransaction ();
99
+ if ($ this ->geotnt ) {
100
+ $ geoindex ->indexEndTransaction ();
101
+ }
64
102
}
65
103
66
104
/**
@@ -78,6 +116,17 @@ public function delete($models)
78
116
$ index = $ this ->tnt ->getIndex ();
79
117
$ index ->setPrimaryKey ($ model ->getKeyName ());
80
118
$ index ->delete ($ model ->getKey ());
119
+
120
+ if ($ this ->geotnt ) {
121
+ $ this ->geotnt ->selectIndex ("{$ model ->searchableAs ()}.geoindex " );
122
+ $ index = $ this ->geotnt ->getIndex ();
123
+ $ index ->loadConfig ($ this ->geotnt ->config );
124
+ $ index ->setPrimaryKey ($ model ->getKeyName ());
125
+ $ index ->prepareAndExecuteStatement (
126
+ 'DELETE FROM locations WHERE doc_id = :documentId; ' ,
127
+ [['key ' => ':documentId ' , 'value ' => $ model ->getKey ()]]
128
+ );
129
+ }
81
130
});
82
131
}
83
132
@@ -145,6 +194,9 @@ protected function performSearch(Builder $builder, array $options = [])
145
194
$ index = $ builder ->index ?: $ builder ->model ->searchableAs ();
146
195
$ limit = $ builder ->limit ?: 10000 ;
147
196
$ this ->tnt ->selectIndex ("{$ index }.index " );
197
+ if ($ this ->geotnt ) {
198
+ $ this ->geotnt ->selectIndex ("{$ index }.geoindex " );
199
+ }
148
200
149
201
$ this ->builder = $ builder ;
150
202
@@ -160,6 +212,14 @@ protected function performSearch(Builder $builder, array $options = [])
160
212
$ options
161
213
);
162
214
}
215
+
216
+ if (is_array ($ builder ->query )) {
217
+ $ location = $ builder ->query ['location ' ];
218
+ $ distance = $ builder ->query ['distance ' ];
219
+ $ limit = array_key_exists ('limit ' , $ builder ->query ) ? $ builder ->query ['limit ' ] : 10000 ;
220
+ return $ this ->geotnt ->findNearest ($ location , $ distance , $ limit );
221
+ }
222
+
163
223
if (isset ($ this ->tnt ->config ['searchBoolean ' ]) ? $ this ->tnt ->config ['searchBoolean ' ] : false ) {
164
224
return $ this ->tnt ->searchBoolean ($ builder ->query , $ limit );
165
225
} else {
@@ -260,6 +320,13 @@ public function initIndex($model)
260
320
$ indexer ->setDatabaseHandle ($ model ->getConnection ()->getPdo ());
261
321
$ indexer ->setPrimaryKey ($ model ->getKeyName ());
262
322
}
323
+ if ($ this ->geotnt && !file_exists ($ this ->tnt ->config ['storage ' ]."/ {$ indexName }.geoindex " )) {
324
+ $ indexer = $ this ->geotnt ->getIndex ();
325
+ $ indexer ->loadConfig ($ this ->geotnt ->config );
326
+ $ indexer ->createIndex ("$ indexName.geoindex " );
327
+ $ indexer ->setDatabaseHandle ($ model ->getConnection ()->getPdo ());
328
+ $ indexer ->setPrimaryKey ($ model ->getKeyName ());
329
+ }
263
330
}
264
331
265
332
/**
@@ -401,5 +468,12 @@ public function flush($model)
401
468
if (file_exists ($ pathToIndex )) {
402
469
unlink ($ pathToIndex );
403
470
}
471
+
472
+ if ($ this ->geotnt ){
473
+ $ pathToGeoIndex = $ this ->geotnt ->config ['storage ' ]."/ {$ indexName }.geoindex " ;
474
+ if (file_exists ($ pathToGeoIndex )) {
475
+ unlink ($ pathToGeoIndex );
476
+ }
477
+ }
404
478
}
405
479
}
0 commit comments