/
CwpSearchEngine.php
164 lines (144 loc) · 4.77 KB
/
CwpSearchEngine.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
namespace CWP\Search;
use Exception;
use Psr\Log\LoggerInterface;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Extensible;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\FullTextSearch\Search\Queries\SearchQuery;
use SilverStripe\FullTextSearch\Solr\SolrIndex;
/**
* Provides interface for generating search results for a SolrIndex
*/
class CwpSearchEngine
{
use Configurable;
use Extensible;
use Injectable;
/**
* Default search options
*
* @var array
* @config
*/
private static $search_options = [
'hl' => 'true',
];
/**
* @var boolean
* @config
*/
private static $use_spellcheck = true;
/**
* Additional search options to send to search when spellcheck
* is included
*
* @var array
* @config
*/
private static $spellcheck_options = [
'spellcheck' => 'true',
'spellcheck.collate' => 'true',
// spellcheck.dictionary can also be configured to use '_spellcheck'
// dictionary when indexing fields under the _spellcheckText column
'spellcheck.dictionary' => 'default',
];
/**
* Build a SearchQuery for a new search
*
* @param string $keywords
* @param array $classes
* @return SearchQuery
*/
protected function getSearchQuery($keywords, $classes)
{
$query = new SearchQuery();
$query->classes = $classes;
$query->addSearchTerm($keywords);
$query->addExclude(SiteTree::class . '_ShowInSearch', 0);
// Artificially lower the amount of results to prevent too high resource usage.
// on subsequent canView check loop.
$query->setLimit(100);
// Allow user code to modify the search query before returning it
$this->extend('updateSearchQuery', $query);
return $query;
}
/**
* Get solr search options for this query
*
* @param bool $spellcheck True if we should include spellcheck support
* @return array
*/
protected function getSearchOptions($spellcheck)
{
$options = $this->config()->get('search_options');
if ($spellcheck) {
$options = array_merge($options, $this->config()->get('spellcheck_options'));
}
return $options;
}
/**
* Get results for a search term
*
* @param string $keywords
* @param array $classes
* @param SolrIndex $searchIndex
* @param int $limit Max number of results for this page
* @param int $start Skip this number of records
* @param bool $spellcheck True to enable spellcheck
* @return CwpSearchResult
*/
protected function getResult($keywords, $classes, $searchIndex, $limit = -1, $start = 0, $spellcheck = false)
{
// Prepare options
$query = $this->getSearchQuery($keywords, $classes);
$options = $this->getSearchOptions($spellcheck);
// Get results
$solrResult = $searchIndex->search(
$query,
$start,
$limit,
$options
);
return CwpSearchResult::create($keywords, $solrResult);
}
/**
* Get a CwpSearchResult for a given criterea
*
* @param string $keywords
* @param array $classes
* @param SolrIndex $searchIndex
* @param int $limit Max number of results for this page
* @param int $start Skip this number of records
* @param bool $followSuggestions True to enable suggested searches to be returned immediately
* @return CwpSearchResult|null
*/
public function search($keywords, $classes, $searchIndex, $limit = -1, $start = 0, $followSuggestions = false)
{
if (empty($keywords)) {
return null;
}
try {
// Begin search
$spellcheck = $this->config()->get('use_spellcheck');
$result = $this->getResult($keywords, $classes, $searchIndex, $limit, $start, $spellcheck);
// Return results if we don't need to refine this any further
if (!$followSuggestions || $result->hasResults() || !$result->getSuggestion()) {
return $result;
}
// Perform new search with the suggested terms
$suggested = $result->getSuggestion();
$newResult = $this->getResult($suggested, $classes, $searchIndex, $limit, $start, false);
$newResult->setOriginal($keywords);
// Compare new results to the original query
if ($newResult->hasResults()) {
return $newResult;
}
return $result;
} catch (Exception $e) {
Injector::inst()->get(LoggerInterface::class)->warning($e);
}
}
}