Skip to content

Commit ead7916

Browse files
committed
Add weight on field
1 parent d216c7c commit ead7916

File tree

5 files changed

+88
-2
lines changed

5 files changed

+88
-2
lines changed

Config/module.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<language>en_US</language>
1414
<language>fr_FR</language>
1515
</languages>
16-
<version>2.1.3</version>
16+
<version>2.1.4</version>
1717
<authors>
1818
<author>
1919
<name>Nicolas Barbey</name>

Index/BaseIndex.php

+16-1
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,28 @@
1515

1616
abstract class BaseIndex implements TntSearchIndexInterface
1717
{
18+
const FIELD_WEIGHT = [
19+
'title' => 10,
20+
'chapo' => 5
21+
];
22+
1823
public function __construct(
1924
protected EventDispatcherInterface $disptacher,
2025
protected TntSearchProvider $tntSearchProvider
21-
) {}
26+
)
27+
{
28+
}
2229

2330
public function getTokenizer(): string
2431
{
2532
return Tokenizer::class;
2633
}
2734

35+
public function getFieldWeights($field): int
36+
{
37+
return self::FIELD_WEIGHT[$field] ?? 1;
38+
}
39+
2840
public function getIndexName(): string
2941
{
3042
$reflectionClass = new ReflectionClass(get_called_class());
@@ -75,8 +87,11 @@ protected function indexOneIndex(string $locale = null): void
7587

7688
$this->disptacher->dispatch($extendQueryEvent, ExtendQueryEvent::EXTEND_QUERY . $indexName);
7789

90+
$tntIndexer->setIndexObject($this);
91+
7892
$tntIndexer->query($extendQueryEvent->getQuery());
7993
$tntIndexer->run();
94+
8095
} catch (Exception $ex) {
8196
Tlog::getInstance()->addError("Error indexation on index $indexFileName : " . $ex->getMessage());
8297
}

Index/Product.php

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
class Product extends BaseIndex
1010
{
11+
const FIELD_WEIGHT = [
12+
'title' => 10,
13+
];
14+
1115
public function isTranslatable(): bool
1216
{
1317
return true;

Index/TntSearchIndexInterface.php

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
interface TntSearchIndexInterface
66
{
7+
public function getFieldWeights(string $field): int;
8+
79
public function isTranslatable(): bool;
810

911
public function index(): void;

Service/Support/TntIndexer.php

+65
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44

55
use TeamTNT\TNTSearch\Indexer\TNTIndexer as BaseTNTIndexer;
66
use TntSearch\Connector\PropelConnector;
7+
use TntSearch\Index\TntSearchIndexInterface;
78

89
class TntIndexer extends BaseTNTIndexer
910
{
11+
protected TntSearchIndexInterface $indexObject;
12+
1013
/**
1114
* Override tu use propel instance instead of dsn.
1215
*/
@@ -15,6 +18,63 @@ public function createConnector(array $config): PropelConnector
1518
return new PropelConnector();
1619
}
1720

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+
1878
/**
1979
* Allow to handle PDOConnection from propel.
2080
*/
@@ -25,4 +85,9 @@ public function setDatabasePropelConnector($dbh): void
2585
$this->dbh->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
2686
}
2787
}
88+
89+
public function setIndexObject(TntSearchIndexInterface $indexObject): void
90+
{
91+
$this->indexObject = $indexObject;
92+
}
2893
}

0 commit comments

Comments
 (0)