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 SilverStripe\ORM\DataObject; |
13
|
|
|
use Suilven\FreeTextSearch\Factory\IndexerFactory; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class BulkIndexer |
17
|
|
|
* |
18
|
|
|
* @package Suilven\ManticoreSearch\Service |
19
|
|
|
* @todo Move some of this into a base indexer |
20
|
|
|
*/ |
21
|
|
|
class BulkIndexer implements \Suilven\FreeTextSearch\Interfaces\BulkIndexer |
22
|
|
|
{ |
23
|
|
|
/** @var array<int,array<string,string|float|bool|int|null>> */ |
24
|
|
|
protected $bulkIndexData; |
25
|
|
|
|
26
|
|
|
/** @var string */ |
27
|
|
|
protected $index; |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
public function __construct() |
31
|
|
|
{ |
32
|
|
|
$this->resetBulkIndexData(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
/** @param string $newIndex the new index name */ |
37
|
|
|
public function setIndex(string $newIndex): void |
38
|
|
|
{ |
39
|
|
|
$this->index = $newIndex; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Note this makes the assumption of unique IDs, along with one index |
45
|
|
|
*/ |
46
|
|
|
public function addDataObject(DataObject $dataObject): void |
47
|
|
|
{ |
48
|
|
|
$factory = new IndexerFactory(); |
49
|
|
|
$indexer = $factory->getIndexer(); |
50
|
|
|
$indexer->setIndexName($this->index); |
51
|
|
|
$payload = $indexer->getIndexablePayload($dataObject); |
52
|
|
|
$toIndex = $payload[$this->index]; |
53
|
|
|
|
54
|
|
|
$keys = \array_keys($toIndex); |
55
|
|
|
foreach ($keys as $key) { |
56
|
|
|
if (!\is_null($toIndex[$key])) { |
57
|
|
|
continue; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$toIndex[$key] = ''; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// @todo Fix indexing of parent id |
64
|
|
|
unset($toIndex['ParentID']); |
65
|
|
|
$this->bulkIndexData[$dataObject->ID] = $toIndex; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
public function indexDataObjects(): int |
70
|
|
|
{ |
71
|
|
|
$body = []; |
72
|
|
|
$nDataObjects = 0; |
73
|
|
|
|
74
|
|
|
foreach (\array_keys($this->bulkIndexData) as $dataObjectID) { |
75
|
|
|
$docPayload = [ |
76
|
|
|
'replace' => [ |
77
|
|
|
'index' => $this->index, |
78
|
|
|
'id' => $dataObjectID, |
79
|
|
|
'doc' => $this->bulkIndexData[$dataObjectID], |
80
|
|
|
], |
81
|
|
|
]; |
82
|
|
|
$body[] = $docPayload; |
83
|
|
|
$nDataObjects++; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ($nDataObjects === 0) { |
87
|
|
|
return 0; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$coreClient = new Client(); |
91
|
|
|
$client = $coreClient->getConnection(); |
92
|
|
|
$payload = ['body' => $body]; |
93
|
|
|
$client->bulk($payload); |
94
|
|
|
$this->resetBulkIndexData(); |
95
|
|
|
|
96
|
|
|
return $nDataObjects; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
private function resetBulkIndexData(): void |
101
|
|
|
{ |
102
|
|
|
$this->bulkIndexData = []; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|