1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class FulltextSearchExtension|Firesphere\SolrCompatibility\Extensions\FulltextSearchExtension provide help for |
4
|
|
|
* migrating from the old module |
5
|
|
|
* |
6
|
|
|
* @package Firesphere\Solr\Compatibility |
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\Core\Extension; |
20
|
|
|
use SilverStripe\Dev\Debug; |
21
|
|
|
use SilverStripe\ORM\ValidationException; |
22
|
|
|
use SilverStripe\View\ArrayData; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class \Firesphere\SolrCompatibility\Extensions\FulltextSearchExtension |
26
|
|
|
* Backward compatibility stubs for the Full text search module |
27
|
|
|
* |
28
|
|
|
* @package Firesphere\Solr\Compatibility |
29
|
|
|
* @property BaseIndex|FulltextSearchExtension $owner |
30
|
|
|
*/ |
31
|
|
|
class FulltextSearchExtension extends Extension |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* Generate a yml version of the init method indexes |
35
|
|
|
*/ |
36
|
1 |
|
public function initToYml(): void |
37
|
|
|
{ |
38
|
|
|
// @codeCoverageIgnoreStart |
39
|
|
|
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
|
|
|
// @codeCoverageIgnoreEnd |
64
|
|
|
|
65
|
1 |
|
throw new LogicException('yaml-emit PHP module missing'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Convert the SearchResult class to a Full text search compatible ArrayData |
70
|
|
|
* |
71
|
|
|
* @param SearchResult|ArrayData $results |
72
|
|
|
*/ |
73
|
2 |
|
public function updateSearchResults(&$results): void |
74
|
|
|
{ |
75
|
|
|
$data = [ |
76
|
2 |
|
'Matches' => $results->getPaginatedMatches(), |
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
|
2 |
|
'Original' => $results |
84
|
|
|
]; |
85
|
|
|
// Override the results with an FTS compatible feature list |
86
|
2 |
|
$results = ArrayData::create($data); |
87
|
2 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Create a spellcheck string that's not the literal collation with Solr query parts |
91
|
|
|
* |
92
|
|
|
* @param string $spellcheck |
93
|
|
|
* @return string mixed |
94
|
|
|
*/ |
95
|
2 |
|
protected function getCollatedNice($spellcheck): string |
96
|
|
|
{ |
97
|
2 |
|
return str_replace(' +', ' ', $spellcheck); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Convert the old search method to the new BaseIndex doSearch methods |
102
|
|
|
* |
103
|
|
|
* @param BaseQuery $query |
104
|
|
|
* @param int $start deprecated in favour of $query, exists for backward compatibility with FTS |
105
|
|
|
* @param int $limit deprecated in favour of $query, exists for backward compatibility with FTS |
106
|
|
|
* @param array $params deprecated in favour of $query, exists for backward compatibility with FTS |
107
|
|
|
* @param bool $spellcheck deprecated in favour of #query, exists for backward compatibility with FTS |
108
|
|
|
* @return SearchResult|ArrayData|mixed |
109
|
|
|
* @throws ValidationException |
110
|
|
|
* @throws GuzzleException |
111
|
|
|
* @throws ReflectionException |
112
|
|
|
* @deprecated This is used as an Fulltext Search compatibility method. Call doSearch instead with the correct Query |
113
|
|
|
*/ |
114
|
2 |
|
public function search($query, $start = 0, $limit = 10, $params = [], $spellcheck = null) |
115
|
|
|
{ |
116
|
2 |
|
$query->getStart() === $start ?: $query->setStart($start); |
117
|
2 |
|
$query->getRows() === $limit ?: $query->setRows($limit); |
118
|
2 |
|
$query->hasSpellcheck() !== $spellcheck ?: $query->setSpellcheck($spellcheck); |
119
|
2 |
|
if (isset($params['fq']) && !count($query->getFields())) { |
120
|
1 |
|
$query->addField($params['fq']); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** @var BaseIndex $owner */ |
124
|
2 |
|
$owner = $this->owner; |
125
|
|
|
|
126
|
2 |
|
return $owner->doSearch($query); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Add a Fulltext Field |
131
|
|
|
* |
132
|
|
|
* @param bool $includeSubclasses Compatibility mode, not actually used |
133
|
|
|
* @throws ReflectionException |
134
|
|
|
* @deprecated Please use addAllFulltextFields(). IncludeSubClasses is not used anymore |
135
|
|
|
*/ |
136
|
1 |
|
public function addFulltextFields($includeSubclasses = true) |
|
|
|
|
137
|
|
|
{ |
138
|
|
|
/** @var BaseIndex $owner */ |
139
|
1 |
|
$owner = $this->owner; |
140
|
|
|
|
141
|
1 |
|
$owner->addAllFulltextFields(); |
142
|
1 |
|
} |
143
|
|
|
} |
144
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.