|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Created by PhpStorm. |
|
5
|
|
|
* User: gordon |
|
6
|
|
|
* Date: 24/3/2561 |
|
7
|
|
|
* Time: 21:14 น. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Suilven\ManticoreSearch\Service; |
|
11
|
|
|
|
|
12
|
|
|
use Suilven\FreeTextSearch\Indexes; |
|
13
|
|
|
use Suilven\FreeTextSearch\Types\FieldTypes; |
|
14
|
|
|
|
|
15
|
|
|
// @phpcs:disable Generic.Files.LineLength.TooLong |
|
16
|
|
|
// @phpcs:disable SlevomatCodingStandard.Files.LineLength.LineTooLong |
|
17
|
|
|
class IndexCreator extends \Suilven\FreeTextSearch\Base\IndexCreator implements \Suilven\FreeTextSearch\Interfaces\IndexCreator |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Create an index |
|
21
|
|
|
* |
|
22
|
|
|
* @todo Refactor into Indexer base |
|
23
|
|
|
* @param string $indexName the name of the index |
|
24
|
|
|
*/ |
|
25
|
|
|
public function createIndex(string $indexName): void |
|
26
|
|
|
{ |
|
27
|
|
|
$fields = $this->getFields($indexName); |
|
28
|
|
|
$storedFields = $this->getStoredFields($indexName); |
|
29
|
|
|
$specs = $this->getFieldSpecs($indexName); |
|
30
|
|
|
|
|
31
|
|
|
\error_log('SPECS'); |
|
32
|
|
|
\print_r($specs); |
|
33
|
|
|
|
|
34
|
|
|
$columns = []; |
|
35
|
|
|
foreach ($fields as $field) { |
|
36
|
|
|
$fieldType = $specs[$field]; |
|
37
|
|
|
|
|
38
|
|
|
// this will be the most common |
|
39
|
|
|
$indexType = 'text'; |
|
40
|
|
|
|
|
41
|
|
|
// @todo configure index to strip HTML |
|
42
|
|
|
switch ($fieldType) { |
|
43
|
|
|
case FieldTypes::FOREIGN_KEY: |
|
44
|
|
|
// @todo this perhaps needs to be a token |
|
45
|
|
|
// See https://docs.manticoresearch.com/3.4.0/html/indexing/data_types.html |
|
46
|
|
|
|
|
47
|
|
|
// @todo also how to mark strings for tokenizing? |
|
48
|
|
|
$indexType = 'bigint'; |
|
49
|
|
|
|
|
50
|
|
|
break; |
|
51
|
|
|
case FieldTypes::INTEGER: |
|
52
|
|
|
$indexType = 'integer'; |
|
53
|
|
|
|
|
54
|
|
|
break; |
|
55
|
|
|
case FieldTypes::FLOAT: |
|
56
|
|
|
$indexType = 'float'; |
|
57
|
|
|
|
|
58
|
|
|
break; |
|
59
|
|
|
case FieldTypes::TIME: |
|
60
|
|
|
$indexType = 'timestamp'; |
|
61
|
|
|
|
|
62
|
|
|
break; |
|
63
|
|
|
case FieldTypes::BOOLEAN: |
|
64
|
|
|
// @todo is there a better type? |
|
65
|
|
|
$indexType = 'integer'; |
|
66
|
|
|
|
|
67
|
|
|
break; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$options = []; |
|
71
|
|
|
if ($indexType === 'text') { |
|
72
|
|
|
$options = ['indexed', 'stored']; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
// override for Link, do not index it. The storing of the Link URL is to save on database hierarchy |
|
76
|
|
|
// traversal when rendering search results |
|
77
|
|
|
if ($field === 'Link' || \in_array($field, $storedFields, true)) { |
|
78
|
|
|
$options = ['stored']; |
|
79
|
|
|
} |
|
80
|
|
|
$columns[$field] = ['type' => $indexType, 'options' => $options]; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
// @todo Add has one |
|
85
|
|
|
|
|
86
|
|
|
$indexes = new Indexes(); |
|
87
|
|
|
$index = $indexes->getIndex($indexName); |
|
88
|
|
|
$mvaFields = $index->getHasManyFields(); |
|
89
|
|
|
error_log(print_r($mvaFields, true)); |
|
90
|
|
|
|
|
91
|
|
|
foreach(array_keys($mvaFields) as $mvaColumnName) { |
|
92
|
|
|
$columns[$mvaColumnName] = ['type' => 'multi']; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$client = new Client(); |
|
96
|
|
|
$manticoreClient = $client->getConnection(); |
|
97
|
|
|
|
|
98
|
|
|
$settings = [ |
|
99
|
|
|
'rt_mem_limit' => '256M', |
|
100
|
|
|
'dict' => 'keywords', |
|
101
|
|
|
'min_infix_len' => 2, |
|
102
|
|
|
'html_strip' => 1, |
|
103
|
|
|
'bigram_index' => 'both_freq' |
|
104
|
|
|
]; |
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
// drop index, and updating an existing one does not effect change |
|
108
|
|
|
$manticoreClient->indices()->drop(['index' => $indexName, 'body'=>['silent'=>true]]); |
|
109
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
$manticoreIndex = new \Manticoresearch\Index($manticoreClient, $indexName); |
|
112
|
|
|
|
|
113
|
|
|
$manticoreIndex->create( |
|
114
|
|
|
$columns, |
|
115
|
|
|
$settings, |
|
116
|
|
|
true |
|
117
|
|
|
); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|