-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSchema.php
229 lines (193 loc) · 7.15 KB
/
Schema.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
/*
* This file is part of Pucene.
*
* (c) asapo.at
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Pucene\DbalDriver;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\Schema as DoctrineSchema;
use Schranz\Search\SEAL\Schema\FieldType;
use Schranz\Search\SEAL\Schema\Index;
/**
* @internal
*/
class Schema
{
private string $prefix;
private DoctrineSchema $schema;
/**
* @var string[]
*/
private array $tableNames;
public function __construct(
private Index $index,
) {
$this->prefix = $this->index->name;
$this->schema = new DoctrineSchema();
$this->createDocumentsTable();
$this->createFieldsTable();
$this->createTermsTable();
$this->createDocumentTermsTable();
$this->createDocumentFieldsTables();
}
private function createDocumentsTable(): void
{
$this->tableNames['documents'] = sprintf('pu_%s_documents', $this->prefix);
$documents = $this->schema->createTable($this->tableNames['documents']);
$documents->addColumn('id', 'string', ['length' => 255]);
$documents->addColumn('number', 'integer', ['autoincrement' => true]);
$documents->addColumn('type', 'string', ['length' => 255]);
$documents->addColumn('document', 'json');
$documents->addColumn('indexed_at', 'datetime');
$documents->setPrimaryKey(['number']);
$documents->addIndex(['type']);
$documents->addUniqueIndex(['id']);
}
private function createFieldsTable(): void
{
$this->tableNames['fields'] = sprintf('pu_%s_fields', $this->prefix);
$table = $this->schema->createTable($this->tableNames['fields']);
$table->addColumn('id', 'integer', ['autoincrement' => true]);
$table->addColumn('document_id', 'string', ['length' => 255]);
$table->addColumn('field_name', 'string', ['length' => 255]);
$table->addColumn('field_length', 'float', ['default' => 0]);
$table->setPrimaryKey(['id']);
$table->addForeignKeyConstraint(
$this->tableNames['documents'],
['document_id'],
['id'],
['onDelete' => 'CASCADE']
);
$table->addIndex(['field_name']);
}
private function createDocumentTermsTable(): void
{
$this->tableNames['document_terms'] = sprintf('pu_%s_document_terms', $this->prefix);
$fields = $this->schema->createTable($this->tableNames['document_terms']);
$fields->addColumn('id', 'integer', ['autoincrement' => true]);
$fields->addColumn('document_id', 'string', ['length' => 255]);
$fields->addColumn('field_name', 'string', ['length' => 255]);
$fields->addColumn('term', 'string', ['length' => 255]);
$fields->addColumn('term_frequency', 'integer', ['default' => 0]);
$fields->addColumn('field_length', 'float', ['default' => 0]);
$fields->setPrimaryKey(['id']);
$fields->addForeignKeyConstraint(
$this->tableNames['documents'],
['document_id'],
['id'],
['onDelete' => 'CASCADE']
);
$fields->addForeignKeyConstraint($this->tableNames['terms'], ['term'], ['term']);
$fields->addIndex(['field_name', 'term']);
}
private function createTermsTable(): void
{
$this->tableNames['terms'] = sprintf('pu_%s_terms', $this->prefix);
$fields = $this->schema->createTable($this->tableNames['terms']);
$fields->addColumn('term', 'string', ['length' => 255]);
$fields->setPrimaryKey(['term']);
$fields->addIndex(['term']);
}
/**
* Create table per mapping type.
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html
*/
private function createDocumentFieldsTables(): void
{
// Text types
$this->createDocumentFieldsTable(FieldType::IDENTIFIER, 'text');
$this->createDocumentFieldsTable(FieldType::TEXT, 'text');
// TODO KEYWORD $this->createDocumentFieldsTable(FieldType::KEYWORD, 'text');
// Numeric types
$this->createDocumentFieldsTable(FieldType::FLOAT, 'float');
$this->createDocumentFieldsTable(FieldType::INTEGER, 'bigint');
// Date types
$this->createDocumentFieldsTable(FieldType::DATETIME, 'datetime');
// Boolean types
$this->createDocumentFieldsTable(FieldType::BOOLEAN, 'boolean');
}
/**
* @param FieldType $type
* @param string $columnType
* @param array<string, mixed> $options
*/
private function createDocumentFieldsTable(FieldType $type, string $columnType, array $options = []): void
{
$tableName = sprintf('document_field_%ss', strtolower($type->name));
$this->tableNames[$tableName] = sprintf('pu_%s_' . $tableName, $this->prefix);
$fields = $this->schema->createTable($this->tableNames[$tableName]);
$fields->addColumn('id', 'integer', ['autoincrement' => true]);
$fields->addColumn('document_id', 'string', ['length' => 255]);
$fields->addColumn('field_name', 'string', ['length' => 255]);
$fields->addColumn(
'value',
$columnType,
array_merge(
['notnull' => false],
$options
)
);
$fields->setPrimaryKey(['id']);
$fields->addForeignKeyConstraint(
$this->tableNames['documents'],
['document_id'],
['id'],
['onDelete' => 'CASCADE']
);
$fields->addIndex(['field_name']);
// Text and blobs can't be indexed.
if (!in_array($columnType, ['text', 'blob'], true)) {
$fields->addIndex(['value']);
}
}
/**
* @return string[]
*/
public function toSql(AbstractPlatform $platform): array
{
return $this->schema->toSql($platform);
}
/**
* @return string[]
*/
public function toDropSql(AbstractPlatform $platform): array
{
return $this->schema->toDropSql($platform);
}
public function getDocumentsTableName(): string
{
return $this->tableNames['documents'];
}
public function getFieldsTableName(): string
{
return $this->tableNames['fields'];
}
public function getFieldTableName(FieldType $type): ?string
{
return $this->tableNames[sprintf('document_field_%ss', strtolower($type->name))] ?? null;
}
public function getDocumentTermsTableName(): string
{
return $this->tableNames['document_terms'];
}
public function getTermsTableName(): string
{
return $this->tableNames['terms'];
}
public function getColumnType(FieldType $type): string
{
return match ($type) {
FieldType::TEXT, FieldType::IDENTIFIER => 'text', // TODO FieldType::KEYWORD
FieldType::FLOAT => 'float',
FieldType::INTEGER => 'bigint',
FieldType::BOOLEAN => 'boolean',
FieldType::DATETIME => 'datetime',
default => throw new \Exception('Unexpected match value'),
};
}
}