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
|
|
|
* Get the sort fields |
46
|
|
|
* |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
|
public function getSort(): array |
50
|
|
|
{ |
51
|
|
|
return $this->sort; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Set the sort fields |
56
|
|
|
* |
57
|
|
|
* @param array $sort |
58
|
|
|
* @return $this |
59
|
|
|
*/ |
60
|
|
|
public function setSort($sort): self |
61
|
|
|
{ |
62
|
|
|
$this->sort = $sort; |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get the search terms |
70
|
|
|
* |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
|
|
public function getTerms(): array |
74
|
|
|
{ |
75
|
|
|
return $this->terms; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Set the search tearms |
80
|
|
|
* |
81
|
|
|
* @param array $terms |
82
|
|
|
* @return $this |
83
|
|
|
*/ |
84
|
|
|
public function setTerms($terms): self |
85
|
|
|
{ |
86
|
|
|
$this->terms = $terms; |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Each boosted query needs a separate addition! |
93
|
|
|
* e.g. $this->addTerm('test', ['MyField', 'MyOtherField'], 3) |
94
|
|
|
* followed by |
95
|
|
|
* $this->addTerm('otherTest', ['Title'], 5); |
96
|
|
|
* |
97
|
|
|
* If you want a generic boost on all terms, use addTerm only once, but boost on each field |
98
|
|
|
* |
99
|
|
|
* The fields parameter is used to boost on |
100
|
|
|
* // @param string $term Term to search for |
101
|
|
|
* @param array $fields fields to boost on |
102
|
|
|
* @param int $boost Boost value |
103
|
|
|
* @param bool|float $fuzzy True or a value to the maximum amount of iterations |
104
|
|
|
* @return $this |
105
|
|
|
* @todo fix this to not be Solr~ish |
106
|
|
|
* For generic boosting, use @addBoostedField($field, $boost), this will add the boost at Index time |
107
|
|
|
* |
108
|
|
|
*/ |
109
|
|
|
public function addTerm(string $term, array $fields = [], int $boost = 1, $fuzzy = null): self |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
$this->terms[] = [ |
112
|
|
|
'text' => $term, |
113
|
|
|
'fields' => $fields, |
114
|
|
|
'boost' => $boost |
115
|
|
|
]; |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $key Field to apply filter on |
122
|
|
|
* @param string|array $value Value(s) to filter on |
123
|
|
|
* @return BaseQuery |
124
|
|
|
*/ |
125
|
|
|
public function addFilter($key, $value): BaseQuery |
126
|
|
|
{ |
127
|
|
|
$this->filters[$key] = $value; |
128
|
|
|
|
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
|
|
public function getFilters(): array |
136
|
|
|
{ |
137
|
|
|
return $this->filters; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param array $filters |
142
|
|
|
* @return BaseQuery |
143
|
|
|
*/ |
144
|
|
|
public function setFilters(array $filters): BaseQuery |
145
|
|
|
{ |
146
|
|
|
$this->filters = $filters; |
147
|
|
|
|
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function getOrFilters(): array |
152
|
|
|
{ |
153
|
|
|
return $this->orFilters; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function setOrFilters(array $orFilters): void |
157
|
|
|
{ |
158
|
|
|
$this->orFilters = $orFilters; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function addOrFilters(string $key, string $orFilters): void |
162
|
|
|
{ |
163
|
|
|
$this->orFilters[$key] = $orFilters; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function getBoostedFields(): array |
167
|
|
|
{ |
168
|
|
|
return $this->boostedFields; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function setBoostedFields(array $boostedFields): void |
172
|
|
|
{ |
173
|
|
|
$this->boostedFields = $boostedFields; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function addBoostedField($key, $value) |
177
|
|
|
{ |
178
|
|
|
$this->boostedFields[$key] = $value; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.