1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CWP\Search; |
4
|
|
|
|
5
|
|
|
use CWP\Search\Extensions\CwpSearchBoostExtension; |
6
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
7
|
|
|
use SilverStripe\FullTextSearch\Search\Queries\SearchQuery; |
8
|
|
|
use SilverStripe\FullTextSearch\Solr\SolrIndex; |
9
|
|
|
use SilverStripe\FullTextSearch\Solr\Stores\SolrConfigStore; |
10
|
|
|
use SilverStripe\SiteConfig\SiteConfig; |
11
|
|
|
use SilverStripe\View\ArrayData; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Abstract wrapper for all cwp-core features |
15
|
|
|
* |
16
|
|
|
* Can be extended by user indexes to include these features. {@see CwpSolrIndex} for an example |
17
|
|
|
*/ |
18
|
|
|
abstract class CwpSearchIndex extends SolrIndex |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Copy all fields into both search and spellcheck data source |
22
|
|
|
* |
23
|
|
|
* @var array |
24
|
|
|
* @config |
25
|
|
|
*/ |
26
|
|
|
private static $copy_fields = [ |
|
|
|
|
27
|
|
|
'_text', |
28
|
|
|
'_spellcheckText', |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Default dictionary to use. This will overwrite the 'spellcheck.dictionary' option for searches given, |
33
|
|
|
* unless set to empty. |
34
|
|
|
* |
35
|
|
|
* '_spellcheck' is a predefined by the cwp infrastructure, which is configured |
36
|
|
|
* to be built from the '_spellcheckText' field. You can't rename this within CWP. |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
* @config |
40
|
|
|
*/ |
41
|
|
|
private static $dictionary = '_spellcheck'; |
42
|
|
|
|
43
|
|
|
public function init() |
44
|
|
|
{ |
45
|
|
|
// Add optional boost |
46
|
|
|
if (SiteTree::has_extension(CwpSearchBoostExtension::class)) { |
47
|
|
|
$this->addFulltextField('SearchBoost'); |
48
|
|
|
$this->setFieldBoosting(SiteTree::class . '_SearchBoost', SiteTree::config()->get('search_boost')); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Upload config for this index to the given store |
54
|
|
|
* |
55
|
|
|
* @param SolrConfigStore $store |
56
|
|
|
*/ |
57
|
|
|
public function uploadConfig($store) |
58
|
|
|
{ |
59
|
|
|
parent::uploadConfig($store); |
60
|
|
|
|
61
|
|
|
// Upload configured synonyms {@see SynonymsSiteConfig} |
62
|
|
|
$siteConfig = SiteConfig::current_site_config(); |
63
|
|
|
if ($siteConfig->SearchSynonyms) { |
64
|
|
|
$store->uploadString( |
65
|
|
|
$this->getIndexName(), |
66
|
|
|
'synonyms.txt', |
67
|
|
|
$siteConfig->SearchSynonyms |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
public function getFieldDefinitions() |
77
|
|
|
{ |
78
|
|
|
$xml = parent::getFieldDefinitions(); |
79
|
|
|
$xml .= "\n\n\t\t<!-- Additional custom fields for spell checking -->"; |
80
|
|
|
$xml .= "\n\t\t<field name='_spellcheckText' type='textSpellHtml' indexed='true' " |
81
|
|
|
. "stored='false' multiValued='true' />"; |
82
|
|
|
|
83
|
|
|
return $xml; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* |
88
|
|
|
* @param SearchQuery $query |
89
|
|
|
* @param int $offset |
90
|
|
|
* @param int $limit |
91
|
|
|
* @param array $params |
92
|
|
|
* @return ArrayData |
93
|
|
|
*/ |
94
|
|
|
public function search(SearchQuery $query, $offset = -1, $limit = -1, $params = []) |
95
|
|
|
{ |
96
|
|
|
// Override dictionary if given |
97
|
|
|
if ($dictionary = $this->config()->dictionary) { |
98
|
|
|
$params["spellcheck.dictionary"] = $dictionary; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return parent::search($query, $offset, $limit, $params); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|