1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Firesphere\ElasticSearch\Queries; |
4
|
|
|
|
5
|
|
|
use Firesphere\SearchBackend\Queries\BaseQuery; |
6
|
|
|
use SilverStripe\Core\Injector\Injectable; |
7
|
|
|
|
8
|
|
|
class ElasticQuery extends BaseQuery |
9
|
|
|
{ |
10
|
|
|
use Injectable; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var array Sorting settings |
14
|
|
|
*/ |
15
|
|
|
protected $sort = []; |
16
|
|
|
/** |
17
|
|
|
* @var bool Enable spellchecking? |
18
|
|
|
*/ |
19
|
|
|
protected $spellcheck = true; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array Filters to use/apply |
23
|
|
|
*/ |
24
|
|
|
protected $filters = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array Filters that are not exclusive |
28
|
|
|
*/ |
29
|
|
|
protected $orFilters = []; |
30
|
|
|
/** |
31
|
|
|
* @var int Minimum results a facet query has to have |
32
|
|
|
*/ |
33
|
|
|
protected $facetsMinCount = 1; |
34
|
|
|
/** |
35
|
|
|
* @var array Search terms |
36
|
|
|
*/ |
37
|
|
|
protected $terms = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $boostedFields = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var bool |
46
|
|
|
*/ |
47
|
|
|
protected $highlight = true; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get the sort fields |
51
|
|
|
* |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
|
public function getSort(): array |
55
|
|
|
{ |
56
|
|
|
return $this->sort; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Set the sort fields |
61
|
|
|
* |
62
|
|
|
* @param array $sort |
63
|
|
|
* @return $this |
64
|
|
|
*/ |
65
|
|
|
public function setSort($sort): self |
66
|
|
|
{ |
67
|
|
|
$this->sort = $sort; |
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get the search terms |
75
|
|
|
* |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
|
|
public function getTerms(): array |
79
|
|
|
{ |
80
|
|
|
return $this->terms; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Set the search tearms |
85
|
|
|
* |
86
|
|
|
* @param array $terms |
87
|
|
|
* @return $this |
88
|
|
|
*/ |
89
|
|
|
public function setTerms($terms): self |
90
|
|
|
{ |
91
|
|
|
$this->terms = $terms; |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Each boosted query needs a separate addition! |
98
|
|
|
* e.g. $this->addTerm('test', ['MyField', 'MyOtherField'], 3) |
99
|
|
|
* followed by |
100
|
|
|
* $this->addTerm('otherTest', ['Title'], 5); |
101
|
|
|
* |
102
|
|
|
* If you want a generic boost on all terms, use addTerm only once, but boost on each field |
103
|
|
|
* |
104
|
|
|
* The fields parameter is used to boost on |
105
|
|
|
* // @param string $term Term to search for |
106
|
|
|
* @param array $fields fields to boost on |
107
|
|
|
* @param int $boost Boost value |
108
|
|
|
* @param bool|float $fuzzy True or a value to the maximum amount of iterations |
109
|
|
|
* @return $this |
110
|
|
|
* @todo fix this to not be Solr~ish |
111
|
|
|
* For generic boosting, use @addBoostedField($field, $boost), this will add the boost at Index time |
112
|
|
|
* |
113
|
|
|
*/ |
114
|
|
|
public function addTerm(string $term, array $fields = [], int $boost = 1, $fuzzy = null): self |
|
|
|
|
115
|
|
|
{ |
116
|
|
|
$this->terms[] = [ |
117
|
|
|
'text' => $term, |
118
|
|
|
'fields' => $fields, |
119
|
|
|
'boost' => $boost |
120
|
|
|
]; |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $key Field to apply filter on |
127
|
|
|
* @param string|array $value Value(s) to filter on |
128
|
|
|
* @return BaseQuery |
129
|
|
|
*/ |
130
|
|
|
public function addFilter($key, $value): BaseQuery |
131
|
|
|
{ |
132
|
|
|
$this->filters[$key] = $value; |
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
|
|
public function getFilters(): array |
141
|
|
|
{ |
142
|
|
|
return $this->filters; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param array $filters |
147
|
|
|
* @return BaseQuery |
148
|
|
|
*/ |
149
|
|
|
public function setFilters(array $filters): BaseQuery |
150
|
|
|
{ |
151
|
|
|
$this->filters = $filters; |
152
|
|
|
|
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function getOrFilters(): array |
157
|
|
|
{ |
158
|
|
|
return $this->orFilters; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function setOrFilters(array $orFilters): void |
162
|
|
|
{ |
163
|
|
|
$this->orFilters = $orFilters; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function addOrFilters(string $key, string $orFilters): void |
167
|
|
|
{ |
168
|
|
|
$this->orFilters[$key] = $orFilters; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function getBoostedFields(): array |
172
|
|
|
{ |
173
|
|
|
return $this->boostedFields; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function setBoostedFields(array $boostedFields): void |
177
|
|
|
{ |
178
|
|
|
$this->boostedFields = $boostedFields; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function addBoostedField($key, $value) |
182
|
|
|
{ |
183
|
|
|
$this->boostedFields[$key] = $value; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function isHighlight(): bool |
187
|
|
|
{ |
188
|
|
|
return $this->highlight; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function setHighlight(bool $highlight): void |
192
|
|
|
{ |
193
|
|
|
$this->highlight = $highlight; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.