|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Trait BaseQueryTrait|Firesphere\SolrSearch\Traits\BaseQueryTrait Trait to clean up the |
|
4
|
|
|
* {@link \Firesphere\SolrSearch\Queries\BaseQuery} |
|
5
|
|
|
* |
|
6
|
|
|
* @package Firesphere\SolrSearch\Traits |
|
7
|
|
|
* @author Simon `Firesphere` Erkelens; Marco `Sheepy` Hermo |
|
8
|
|
|
* @copyright Copyright (c) 2018 - now() Firesphere & Sheepy |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Firesphere\SolrSearch\Traits; |
|
12
|
|
|
|
|
13
|
|
|
use Minimalcode\Search\Criteria; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Trait BaseQueryTrait Extraction from the BaseQuery class to keep things readable. |
|
17
|
|
|
* |
|
18
|
|
|
* This trait adds the support for adding the basic field/filter/term/facet options |
|
19
|
|
|
* |
|
20
|
|
|
* @package Firesphere\SolrSearch\Traits |
|
21
|
|
|
*/ |
|
22
|
|
|
trait BaseQueryTrait |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var array Terms to search |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $terms = []; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var array Fields to filter |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $filter = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var array Fields to search |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $fields = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Key => value pairs of facets to apply in AND fashion |
|
41
|
|
|
* [ |
|
42
|
|
|
* 'FacetTitle' => [1, 2, 3], |
|
43
|
|
|
* 'FacetTitle2' => [1, 2, 3] |
|
44
|
|
|
* ] |
|
45
|
|
|
* |
|
46
|
|
|
* @var array |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $andFacetFilter = []; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Key => value pairs of facets to apply in OR fashion |
|
52
|
|
|
* [ |
|
53
|
|
|
* 'FacetTitle' => [1, 2, 3], |
|
54
|
|
|
* 'FacetTitle2' => [1, 2, 3] |
|
55
|
|
|
* ] |
|
56
|
|
|
* |
|
57
|
|
|
* @var array |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $orFacetFilter = []; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @var array Fields to exclude |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $exclude = []; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @var array Sorting order |
|
68
|
|
|
*/ |
|
69
|
|
|
protected $sort = []; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Each boosted query needs a separate addition! |
|
73
|
|
|
* e.g. $this->addTerm('test', ['MyField', 'MyOtherField'], 3) |
|
74
|
|
|
* followed by |
|
75
|
|
|
* $this->addTerm('otherTest', ['Title'], 5); |
|
76
|
|
|
* |
|
77
|
|
|
* If you want a generic boost on all terms, use addTerm only once, but boost on each field |
|
78
|
|
|
* |
|
79
|
|
|
* The fields parameter is used to boost on |
|
80
|
|
|
* |
|
81
|
|
|
* For generic boosting, use @addBoostedField($field, $boost), this will add the boost at Index time |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $term Term to search for |
|
84
|
|
|
* @param array $fields fields to boost on |
|
85
|
|
|
* @param int $boost Boost value |
|
86
|
|
|
* @param bool|float $fuzzy True or a value to the maximum amount of iterations |
|
87
|
|
|
* @return $this |
|
88
|
|
|
*/ |
|
89
|
7 |
|
public function addTerm(string $term, array $fields = [], int $boost = 0, $fuzzy = null): self |
|
90
|
|
|
{ |
|
91
|
7 |
|
$this->terms[] = [ |
|
92
|
7 |
|
'text' => $term, |
|
93
|
7 |
|
'fields' => $fields, |
|
94
|
7 |
|
'boost' => $boost, |
|
95
|
7 |
|
'fuzzy' => $fuzzy, |
|
96
|
|
|
]; |
|
97
|
|
|
|
|
98
|
7 |
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Adds filters to filter on by value |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $field Field to filter on |
|
105
|
|
|
* @param string|array|Criteria $value Value for this field |
|
106
|
|
|
* @return $this |
|
107
|
|
|
*/ |
|
108
|
4 |
|
public function addFilter($field, $value): self |
|
109
|
|
|
{ |
|
110
|
4 |
|
$field = str_replace('.', '_', $field); |
|
111
|
4 |
|
$this->filter[$field] = $value; |
|
112
|
|
|
|
|
113
|
4 |
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Add a field to be returned |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $field fieldname |
|
120
|
|
|
* @return $this |
|
121
|
|
|
*/ |
|
122
|
2 |
|
public function addField($field): self |
|
123
|
|
|
{ |
|
124
|
2 |
|
$field = str_replace('.', '_', $field); |
|
125
|
2 |
|
$this->fields[] = $field; |
|
126
|
|
|
|
|
127
|
2 |
|
return $this; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Exclude fields from the search action |
|
132
|
|
|
* |
|
133
|
|
|
* @param string $field |
|
134
|
|
|
* @param string|array|Criteria $value |
|
135
|
|
|
* @return $this |
|
136
|
|
|
*/ |
|
137
|
4 |
|
public function addExclude($field, $value): self |
|
138
|
|
|
{ |
|
139
|
4 |
|
$field = str_replace('.', '_', $field); |
|
140
|
4 |
|
$this->exclude[$field] = $value; |
|
141
|
|
|
|
|
142
|
4 |
|
return $this; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Add faceting fields that need to be faceted in an AND format |
|
147
|
|
|
* |
|
148
|
|
|
* @param string $field Field to facet |
|
149
|
|
|
* @param string|array $value Value to facet |
|
150
|
|
|
* @return $this |
|
151
|
|
|
*/ |
|
152
|
2 |
|
public function addFacetFilter($field, $value): self |
|
153
|
|
|
{ |
|
154
|
2 |
|
$value = is_array($value) ? $value : [$value]; |
|
155
|
2 |
|
foreach ($value as $item) { |
|
156
|
2 |
|
$this->andFacetFilter[$field][] = $item; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
2 |
|
return $this; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Stub for addFacetFilter to add an AND filter |
|
164
|
|
|
* |
|
165
|
|
|
* @param string $field |
|
166
|
|
|
* @param string|array $value |
|
167
|
|
|
* @return $this |
|
168
|
|
|
*/ |
|
169
|
1 |
|
public function addAndFacetFilter($field, $value): self |
|
170
|
|
|
{ |
|
171
|
1 |
|
return $this->addFacetFilter($field, $value); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Add faceting that need to be faceted in an OR formats |
|
176
|
|
|
* |
|
177
|
|
|
* @param string $field Field to facet |
|
178
|
|
|
* @param string|array $value Value to facet |
|
179
|
|
|
* @return $this |
|
180
|
|
|
*/ |
|
181
|
1 |
|
public function addOrFacetFilter($field, $value): self |
|
182
|
|
|
{ |
|
183
|
1 |
|
$value = is_array($value) ? $value : [$value]; |
|
184
|
1 |
|
foreach ($value as $item) { |
|
185
|
1 |
|
$this->orFacetFilter[$field][] = $item; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
1 |
|
return $this; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Add a field to sort on |
|
193
|
|
|
* |
|
194
|
|
|
* @param string $field |
|
195
|
|
|
* @param string $direction |
|
196
|
|
|
* @return $this |
|
197
|
|
|
*/ |
|
198
|
1 |
|
public function addSort($field, $direction): self |
|
199
|
|
|
{ |
|
200
|
1 |
|
$this->sort[$field] = $direction; |
|
201
|
|
|
|
|
202
|
1 |
|
return $this; |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|