|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Created by PhpStorm. |
|
5
|
|
|
* User: gordon |
|
6
|
|
|
* Date: 25/3/2561 |
|
7
|
|
|
* Time: 1:35 น. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Suilven\ManticoreSearch\Service; |
|
11
|
|
|
|
|
12
|
|
|
use Manticoresearch\Search; |
|
13
|
|
|
use SilverStripe\ORM\ArrayList; |
|
14
|
|
|
use SilverStripe\ORM\DataObject; |
|
15
|
|
|
use Suilven\FreeTextSearch\Container\SearchResults; |
|
16
|
|
|
use Suilven\FreeTextSearch\Indexes; |
|
17
|
|
|
|
|
18
|
|
|
class Searcher extends \Suilven\FreeTextSearch\Base\Searcher implements \Suilven\FreeTextSearch\Interfaces\Searcher |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var \Suilven\ManticoreSearch\Service\Client */ |
|
21
|
|
|
private $client; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct() |
|
24
|
|
|
{ |
|
25
|
|
|
$this->client = new Client(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
public function search(?string $q): SearchResults |
|
30
|
|
|
{ |
|
31
|
|
|
$startTime = \microtime(true); |
|
32
|
|
|
$client = new Client(); |
|
33
|
|
|
$manticoreClient = $client->getConnection(); |
|
34
|
|
|
|
|
35
|
|
|
$searcher = new Search($manticoreClient); |
|
36
|
|
|
$searcher->setIndex($this->indexName); |
|
37
|
|
|
$searcher->limit($this->pageSize); |
|
38
|
|
|
$offset=$this->pageSize * ($this->page-1); |
|
39
|
|
|
$searcher->offset($offset); |
|
40
|
|
|
|
|
41
|
|
|
$manticoreResult = $searcher->search($q)->highlight( |
|
42
|
|
|
[], |
|
43
|
|
|
['pre_tags' => '<b>', 'post_tags'=>'</b>'] |
|
44
|
|
|
)->get(); |
|
45
|
|
|
|
|
46
|
|
|
$indexes = new Indexes(); |
|
47
|
|
|
$index = $indexes->getIndex($this->indexName); |
|
48
|
|
|
$fields = $index->getFields(); |
|
49
|
|
|
|
|
50
|
|
|
$ssResult = new ArrayList(); |
|
51
|
|
|
while ($manticoreResult->valid()) { |
|
52
|
|
|
$hit = $manticoreResult->current(); |
|
53
|
|
|
$source = $hit->getData(); |
|
54
|
|
|
//print_r($source); |
|
55
|
|
|
$ssDataObject = new DataObject(); |
|
56
|
|
|
|
|
57
|
|
|
$keys = \array_keys($source); |
|
58
|
|
|
foreach ($keys as $key) { |
|
59
|
|
|
$keyname = $key; |
|
60
|
|
|
foreach ($fields as $field) { |
|
61
|
|
|
if (\strtolower($field) === $key) { |
|
62
|
|
|
$keyname = $field; |
|
63
|
|
|
|
|
64
|
|
|
break; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
// @todo This is a hack as $Title is rendering the ID in the template |
|
69
|
|
|
if ($keyname === 'Title') { |
|
70
|
|
|
$keyname = 'ResultTitle'; |
|
71
|
|
|
} elseif ($keyname === 'link') { |
|
72
|
|
|
$keyname = 'Link'; |
|
73
|
|
|
}; |
|
74
|
|
|
|
|
75
|
|
|
/** @phpstan-ignore-next-line */ |
|
76
|
|
|
$ssDataObject->$keyname = $source[$key]; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
// manticore lowercases fields, so as above normalize them back to the SS fieldnames |
|
81
|
|
|
$highlights = $hit->getHighlight(); |
|
82
|
|
|
$highlightsSS = []; |
|
83
|
|
|
|
|
84
|
|
|
$keys = \array_keys($highlights); |
|
85
|
|
|
foreach ($keys as $key) { |
|
86
|
|
|
if (!isset($highlights[$key])) { |
|
87
|
|
|
continue; |
|
88
|
|
|
} |
|
89
|
|
|
$keyname = $key; |
|
90
|
|
|
foreach ($fields as $field) { |
|
91
|
|
|
if (\strtolower($field) === $key) { |
|
92
|
|
|
$keyname = $field; |
|
93
|
|
|
|
|
94
|
|
|
continue; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
if ($key === 'link') { |
|
99
|
|
|
$keyname = 'Link'; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$highlightsSS[$keyname] = $highlights[$key]; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** @phpstan-ignore-next-line */ |
|
106
|
|
|
$ssDataObject->Highlights = $highlightsSS; |
|
107
|
|
|
|
|
108
|
|
|
$ssDataObject->ID = $hit->getId(); |
|
109
|
|
|
$ssResult->push($ssDataObject); |
|
110
|
|
|
$manticoreResult->next(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
// we now need to standardize the output returned |
|
114
|
|
|
|
|
115
|
|
|
$searchResults = new SearchResults(); |
|
116
|
|
|
$searchResults->setRecords($ssResult); |
|
117
|
|
|
$searchResults->setPage($this->page); |
|
118
|
|
|
$searchResults->setPageSize($this->pageSize); |
|
119
|
|
|
$searchResults->setQuery($q); |
|
|
|
|
|
|
120
|
|
|
$searchResults->setTotalNumberOfResults($manticoreResult->getTotal()); |
|
121
|
|
|
|
|
122
|
|
|
$endTime = \microtime(true); |
|
123
|
|
|
$delta = $endTime - $startTime; |
|
124
|
|
|
$delta = \round(1000*$delta)/1000; |
|
125
|
|
|
$searchResults->setTime($delta); |
|
126
|
|
|
|
|
127
|
|
|
return $searchResults; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|