|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Firesphere\SolrCompatibility\Extensions; |
|
5
|
|
|
|
|
6
|
|
|
use Firesphere\SolrSearch\Indexes\BaseIndex; |
|
7
|
|
|
use Firesphere\SolrSearch\Queries\BaseQuery; |
|
8
|
|
|
use Firesphere\SolrSearch\Results\SearchResult; |
|
9
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
|
10
|
|
|
use LogicException; |
|
11
|
|
|
use ReflectionException; |
|
12
|
|
|
use SilverStripe\Control\Controller; |
|
13
|
|
|
use SilverStripe\Core\Extension; |
|
14
|
|
|
use SilverStripe\Dev\Debug; |
|
15
|
|
|
use SilverStripe\ORM\ValidationException; |
|
16
|
|
|
use SilverStripe\View\ArrayData; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class FulltextSearchExtension |
|
20
|
|
|
* Backward compatibility stubs for the Full text search module |
|
21
|
|
|
* |
|
22
|
|
|
* @package Firesphere\SolrSearch\Extensions |
|
23
|
|
|
* @property BaseIndex|FulltextSearchExtension $owner |
|
24
|
|
|
*/ |
|
25
|
|
|
class FulltextSearchExtension extends Extension |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Generate a yml version of the init method indexes |
|
29
|
|
|
*/ |
|
30
|
1 |
|
public function initToYml(): void |
|
31
|
|
|
{ |
|
32
|
1 |
|
if (function_exists('yaml_emit')) { |
|
33
|
|
|
/** @var BaseIndex $owner */ |
|
34
|
|
|
$owner = $this->owner; |
|
35
|
|
|
$result = [ |
|
36
|
|
|
BaseIndex::class => [ |
|
37
|
|
|
$owner->getIndexName() => |
|
38
|
|
|
[ |
|
39
|
|
|
'Classes' => $owner->getClasses(), |
|
40
|
|
|
'FulltextFields' => array_values($owner->getFulltextFields()), |
|
41
|
|
|
'SortFields' => $owner->getSortFields(), |
|
42
|
|
|
'FilterFields' => $owner->getFilterFields(), |
|
43
|
|
|
'BoostedFields' => $owner->getBoostedFields(), |
|
44
|
|
|
'CopyFields' => $owner->getCopyFields(), |
|
45
|
|
|
'DefaultField' => $owner->getDefaultField(), |
|
46
|
|
|
'FacetFields' => $owner->getFacetFields(), |
|
47
|
|
|
'StoredFields' => $owner->getStoredFields(), |
|
48
|
|
|
], |
|
49
|
|
|
], |
|
50
|
|
|
]; |
|
51
|
|
|
|
|
52
|
|
|
Debug::dump(yaml_emit($result)); |
|
53
|
|
|
|
|
54
|
|
|
return; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
throw new LogicException('yaml-emit PHP module missing'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Convert the SearchResult class to a Full text search compatible ArrayData |
|
62
|
|
|
* |
|
63
|
|
|
* @param SearchResult|ArrayData $results |
|
64
|
|
|
*/ |
|
65
|
2 |
|
public function updateSearchResults(&$results): void |
|
66
|
|
|
{ |
|
67
|
2 |
|
$request = Controller::curr()->getRequest(); |
|
68
|
|
|
$data = [ |
|
69
|
2 |
|
'Matches' => $results->getPaginatedMatches($request), |
|
70
|
2 |
|
'Facets' => $results->getFacets(), |
|
71
|
2 |
|
'Highlights' => $results->getHighlight(), |
|
72
|
2 |
|
'Spellcheck' => $results->getSpellcheck(), |
|
73
|
2 |
|
'Suggestion' => $results->getCollatedSpellcheck(), |
|
74
|
2 |
|
'SuggestionNice' => $this->getCollatedNice($results->getCollatedSpellcheck()), |
|
75
|
2 |
|
'SuggestionQueryString' => $results->getCollatedSpellcheck(), |
|
76
|
|
|
]; |
|
77
|
|
|
// Override the results with an FTS compatible feature list |
|
78
|
2 |
|
$results = ArrayData::create($data); |
|
79
|
2 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Create a spellcheck string that's not the literal collation with Solr query parts |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $spellcheck |
|
85
|
|
|
* @return string mixed |
|
86
|
|
|
*/ |
|
87
|
2 |
|
protected function getCollatedNice($spellcheck): string |
|
88
|
|
|
{ |
|
89
|
2 |
|
return str_replace(' +', ' ', $spellcheck); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Convert the old search method to the new BaseIndex doSearch methods |
|
94
|
|
|
* |
|
95
|
|
|
* @param BaseQuery $query |
|
96
|
|
|
* @param int $start deprecated in favour of $query, exists for backward compatibility with FTS |
|
97
|
|
|
* @param int $limit deprecated in favour of $query, exists for backward compatibility with FTS |
|
98
|
|
|
* @param array $params deprecated in favour of $query, exists for backward compatibility with FTS |
|
99
|
|
|
* @param bool $spellcheck deprecated in favour of #query, exists for backward compatibility with FTS |
|
100
|
|
|
* @return SearchResult|ArrayData|mixed |
|
101
|
|
|
* @throws ValidationException |
|
102
|
|
|
* @throws GuzzleException |
|
103
|
|
|
* @throws ReflectionException |
|
104
|
|
|
* @deprecated This is used as an Fulltext Search compatibility method. Call doSearch instead with the correct Query |
|
105
|
|
|
*/ |
|
106
|
2 |
|
public function search($query, $start = 0, $limit = 10, $params = [], $spellcheck = null) |
|
107
|
|
|
{ |
|
108
|
2 |
|
$query->getStart() === $start ?: $query->setStart($start); |
|
109
|
2 |
|
$query->getRows() === $limit ?: $query->setRows($limit); |
|
110
|
2 |
|
$query->hasSpellcheck() !== $spellcheck ?: $query->setSpellcheck($spellcheck); |
|
111
|
2 |
|
if (isset($params['fq']) && !count($query->getFields())) { |
|
112
|
1 |
|
$query->addField($params['fq']); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** @var BaseIndex $owner */ |
|
116
|
2 |
|
$owner = $this->owner; |
|
117
|
|
|
|
|
118
|
2 |
|
return $owner->doSearch($query); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|