4
4
5
5
use TeamTNT \TNTSearch \Indexer \TNTIndexer as BaseTNTIndexer ;
6
6
use TntSearch \Connector \PropelConnector ;
7
+ use TntSearch \Index \TntSearchIndexInterface ;
7
8
8
9
class TntIndexer extends BaseTNTIndexer
9
10
{
11
+ protected TntSearchIndexInterface $ indexObject ;
12
+
10
13
/**
11
14
* Override tu use propel instance instead of dsn.
12
15
*/
@@ -15,6 +18,63 @@ public function createConnector(array $config): PropelConnector
15
18
return new PropelConnector ();
16
19
}
17
20
21
+ public function saveWordlist ($ stems ): array
22
+ {
23
+ $ terms = [];
24
+ $ stems ->map (function ($ column , $ key ) use (&$ terms ) {
25
+ $ weight = $ this ->indexObject ->getFieldWeights ($ key );
26
+ foreach ($ column as $ term ) {
27
+ if (array_key_exists ($ term , $ terms )) {
28
+ $ terms [$ term ]['hits ' ] = (int ) $ terms [$ term ]['hits ' ] * $ weight ;
29
+ $ terms [$ term ]['docs ' ] = 1 ;
30
+ } else {
31
+ $ terms [$ term ] = [
32
+ 'hits ' => 1 * $ weight ,
33
+ 'docs ' => 1 ,
34
+ 'id ' => 0
35
+ ];
36
+ }
37
+ }
38
+ });
39
+
40
+ foreach ($ terms as $ key => $ term ) {
41
+ try {
42
+ $ this ->insertWordlistStmt ->bindParam (":keyword " , $ key );
43
+ $ this ->insertWordlistStmt ->bindParam (":hits " , $ term ['hits ' ]);
44
+ $ this ->insertWordlistStmt ->bindParam (":docs " , $ term ['docs ' ]);
45
+ $ this ->insertWordlistStmt ->execute ();
46
+
47
+ $ terms [$ key ]['id ' ] = $ this ->index ->lastInsertId ();
48
+ if ($ this ->inMemory ) {
49
+ $ this ->inMemoryTerms [$ key ] = $ terms [$ key ]['id ' ];
50
+ }
51
+ } catch (\Exception $ e ) {
52
+ if ($ e ->getCode () == 23000 ) {
53
+ $ this ->updateWordlistStmt ->bindValue (':docs ' , $ term ['docs ' ]);
54
+ $ this ->updateWordlistStmt ->bindValue (':hits ' , $ term ['hits ' ]);
55
+ $ this ->updateWordlistStmt ->bindValue (':keyword ' , $ key );
56
+ $ this ->updateWordlistStmt ->execute ();
57
+ if (!$ this ->inMemory ) {
58
+ $ this ->selectWordlistStmt ->bindValue (':keyword ' , $ key );
59
+ $ this ->selectWordlistStmt ->execute ();
60
+ $ res = $ this ->selectWordlistStmt ->fetch (PDO ::FETCH_ASSOC );
61
+ $ terms [$ key ]['id ' ] = $ res ['id ' ];
62
+ } else {
63
+ $ terms [$ key ]['id ' ] = $ this ->inMemoryTerms [$ key ];
64
+ }
65
+ } else {
66
+ echo "Error while saving wordlist: " . $ e ->getMessage () . "\n" ;
67
+ }
68
+
69
+ // Statements must be refreshed, because in this state they have error attached to them.
70
+ $ this ->statementsPrepared = false ;
71
+ $ this ->prepareStatementsForIndex ();
72
+
73
+ }
74
+ }
75
+ return $ terms ;
76
+ }
77
+
18
78
/**
19
79
* Allow to handle PDOConnection from propel.
20
80
*/
@@ -25,4 +85,9 @@ public function setDatabasePropelConnector($dbh): void
25
85
$ this ->dbh ->setAttribute (\PDO ::MYSQL_ATTR_USE_BUFFERED_QUERY , false );
26
86
}
27
87
}
88
+
89
+ public function setIndexObject (TntSearchIndexInterface $ indexObject ): void
90
+ {
91
+ $ this ->indexObject = $ indexObject ;
92
+ }
28
93
}
0 commit comments