1010use TeamTNT \Scout \Events \SearchPerformed ;
1111use TeamTNT \TNTSearch \Exceptions \IndexNotFoundException ;
1212use TeamTNT \TNTSearch \TNTSearch ;
13+ use TeamTNT \TNTSearch \TNTGeoSearch ;
1314
1415class TNTSearchEngine extends Engine
1516{
@@ -25,14 +26,20 @@ class TNTSearchEngine extends Engine
2526 */
2627 protected $ builder ;
2728
29+ /**
30+ * @var TNTGeoSearch
31+ */
32+ protected $ geotnt ;
33+
2834 /**
2935 * Create a new engine instance.
3036 *
3137 * @param TNTSearch $tnt
3238 */
33- public function __construct (TNTSearch $ tnt )
39+ public function __construct (TNTSearch $ tnt, TNTGeoSearch $ geotnt = null )
3440 {
3541 $ this ->tnt = $ tnt ;
42+ $ this ->geotnt = $ geotnt ;
3643 }
3744
3845 public function getTNT ()
@@ -54,21 +61,52 @@ public function update($models)
5461 $ index = $ this ->tnt ->getIndex ();
5562 $ index ->setPrimaryKey ($ models ->first ()->getKeyName ());
5663
64+ $ geoindex = null ;
65+ if ($ this ->geotnt ) {
66+ $ this ->geotnt ->selectIndex ("{$ models ->first ()->searchableAs ()}.geoindex " );
67+ $ geoindex = $ this ->geotnt ->getIndex ();
68+ $ geoindex ->loadConfig ($ this ->geotnt ->config );
69+ $ geoindex ->setPrimaryKey ($ models ->first ()->getKeyName ());
70+ $ geoindex ->indexBeginTransaction ();
71+ }
72+
5773 $ index ->indexBeginTransaction ();
58- $ models ->each (function ($ model ) use ($ index ) {
74+ $ models ->each (function ($ model ) use ($ index, $ geoindex ) {
5975 $ array = $ model ->toSearchableArray ();
6076
6177 if (empty ($ array )) {
6278 return ;
6379 }
6480
81+ if ($ geoindex ) {
82+ $ latitude = isset ($ array ['latitude ' ]) ? (float ) $ array ['latitude ' ] : null ;
83+ $ longitude = isset ($ array ['longitude ' ]) ? (float ) $ array ['longitude ' ] : null ;
84+ unset($ array ['longitude ' ]);
85+ unset($ array ['latitude ' ]);
86+ }
87+
6588 if ($ model ->getKey ()) {
6689 $ index ->update ($ model ->getKey (), $ array );
90+
91+ if ($ geoindex ) {
92+ $ geoindex ->prepareAndExecuteStatement (
93+ 'DELETE FROM locations WHERE doc_id = :documentId; ' ,
94+ [['key ' => ':documentId ' , 'value ' => $ model ->getKey ()]]
95+ );
96+ }
6797 } else {
6898 $ index ->insert ($ array );
6999 }
100+ if ($ geoindex && !empty ($ latitude ) && !empty ($ longitude )) {
101+ $ array ['latitude ' ] = $ latitude ;
102+ $ array ['longitude ' ] = $ longitude ;
103+ $ geoindex ->insert ($ array );
104+ }
70105 });
71106 $ index ->indexEndTransaction ();
107+ if ($ this ->geotnt ) {
108+ $ geoindex ->indexEndTransaction ();
109+ }
72110 }
73111
74112 /**
@@ -86,6 +124,17 @@ public function delete($models)
86124 $ index = $ this ->tnt ->getIndex ();
87125 $ index ->setPrimaryKey ($ model ->getKeyName ());
88126 $ index ->delete ($ model ->getKey ());
127+
128+ if ($ this ->geotnt ) {
129+ $ this ->geotnt ->selectIndex ("{$ model ->searchableAs ()}.geoindex " );
130+ $ index = $ this ->geotnt ->getIndex ();
131+ $ index ->loadConfig ($ this ->geotnt ->config );
132+ $ index ->setPrimaryKey ($ model ->getKeyName ());
133+ $ index ->prepareAndExecuteStatement (
134+ 'DELETE FROM locations WHERE doc_id = :documentId; ' ,
135+ [['key ' => ':documentId ' , 'value ' => $ model ->getKey ()]]
136+ );
137+ }
89138 });
90139 }
91140
@@ -154,6 +203,10 @@ protected function performSearch(Builder $builder, array $options = [])
154203 $ limit = $ builder ->limit ?: 10000 ;
155204 $ this ->tnt ->selectIndex ("{$ index }.index " );
156205
206+ if ($ this ->geotnt ) {
207+ $ this ->geotnt ->selectIndex ("{$ index }.geoindex " );
208+ }
209+
157210 $ this ->builder = $ builder ;
158211
159212 if (isset ($ builder ->model ->asYouType )) {
@@ -169,6 +222,13 @@ protected function performSearch(Builder $builder, array $options = [])
169222 );
170223 }
171224
225+ if (is_array ($ builder ->query )) {
226+ $ location = $ builder ->query ['location ' ];
227+ $ distance = $ builder ->query ['distance ' ];
228+ $ limit = array_key_exists ('limit ' , $ builder ->query ) ? $ builder ->query ['limit ' ] : 10000 ;
229+ return $ this ->geotnt ->findNearest ($ location , $ distance , $ limit );
230+ }
231+
172232 $ builder ->query = $ this ->applyFilters ('query_expansion ' , $ builder ->query , get_class ($ builder ->model ));
173233
174234 if (isset ($ this ->tnt ->config ['searchBoolean ' ]) ? $ this ->tnt ->config ['searchBoolean ' ] : false ) {
@@ -181,6 +241,7 @@ protected function performSearch(Builder $builder, array $options = [])
181241 event (new SearchPerformed ($ builder , $ res ));
182242 return $ res ;
183243 }
244+
184245 }
185246
186247 /**
@@ -276,6 +337,14 @@ public function initIndex($model)
276337 $ indexer ->setDatabaseHandle ($ model ->getConnection ()->getPdo ());
277338 $ indexer ->setPrimaryKey ($ model ->getKeyName ());
278339 }
340+
341+ if ($ this ->geotnt && !file_exists ($ this ->tnt ->config ['storage ' ]."/ {$ indexName }.geoindex " )) {
342+ $ indexer = $ this ->geotnt ->getIndex ();
343+ $ indexer ->loadConfig ($ this ->geotnt ->config );
344+ $ indexer ->createIndex ("$ indexName.geoindex " );
345+ $ indexer ->setDatabaseHandle ($ model ->getConnection ()->getPdo ());
346+ $ indexer ->setPrimaryKey ($ model ->getKeyName ());
347+ }
279348 }
280349
281350 /**
@@ -417,6 +486,13 @@ public function flush($model)
417486 if (file_exists ($ pathToIndex )) {
418487 unlink ($ pathToIndex );
419488 }
489+
490+ if ($ this ->geotnt ){
491+ $ pathToGeoIndex = $ this ->geotnt ->config ['storage ' ]."/ {$ indexName }.geoindex " ;
492+ if (file_exists ($ pathToGeoIndex )) {
493+ unlink ($ pathToGeoIndex );
494+ }
495+ }
420496 }
421497
422498 /**
0 commit comments