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