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
|
7 |
|
* If you want a generic boost on all terms, use addTerm only once, but boost on each field |
78
|
|
|
* |
79
|
7 |
|
* The fields parameter is used to boost on |
80
|
7 |
|
* |
81
|
7 |
|
* For generic boosting, use @addBoostedField($field, $boost), this will add the boost at Index time |
82
|
7 |
|
* |
83
|
7 |
|
* @param string $term Term to search for |
84
|
|
|
* @param array $fields fields to boost on |
85
|
|
|
* @param int $boost Boost value |
86
|
7 |
|
* @param bool|float $fuzzy True or a value to the maximum amount of iterations |
87
|
|
|
* @return $this |
88
|
|
|
*/ |
89
|
|
|
public function addTerm(string $term, array $fields = [], int $boost = 0, $fuzzy = null): self |
90
|
|
|
{ |
91
|
|
|
$this->terms[] = [ |
92
|
|
|
'text' => $term, |
93
|
|
|
'fields' => $fields, |
94
|
|
|
'boost' => $boost, |
95
|
|
|
'fuzzy' => $fuzzy, |
96
|
4 |
|
]; |
97
|
|
|
|
98
|
4 |
|
return $this; |
99
|
4 |
|
} |
100
|
|
|
|
101
|
4 |
|
/** |
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
|
|
|
public function addFilter($field, $value): self |
109
|
|
|
{ |
110
|
2 |
|
$field = str_replace('.', '_', $field); |
111
|
|
|
$this->filter[$field] = $value; |
112
|
2 |
|
|
113
|
2 |
|
return $this; |
114
|
|
|
} |
115
|
2 |
|
|
116
|
|
|
/** |
117
|
|
|
* Add a field to be returned |
118
|
|
|
* |
119
|
|
|
* @param string $field fieldname |
120
|
|
|
* @return $this |
121
|
|
|
*/ |
122
|
|
|
public function addField($field): self |
123
|
|
|
{ |
124
|
|
|
$field = str_replace('.', '_', $field); |
125
|
4 |
|
$this->fields[] = $field; |
126
|
|
|
|
127
|
4 |
|
return $this; |
128
|
4 |
|
} |
129
|
|
|
|
130
|
4 |
|
/** |
131
|
|
|
* Exclude fields from the search action |
132
|
|
|
* |
133
|
|
|
* @param string $field |
134
|
|
|
* @param string|array|Criteria $value |
135
|
|
|
* @return $this |
136
|
|
|
*/ |
137
|
|
|
public function addExclude($field, $value): self |
138
|
|
|
{ |
139
|
|
|
$field = str_replace('.', '_', $field); |
140
|
2 |
|
$this->exclude[$field] = $value; |
141
|
|
|
|
142
|
2 |
|
return $this; |
143
|
|
|
} |
144
|
2 |
|
|
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
|
|
|
public function addFacetFilter($field, $value): self |
153
|
|
|
{ |
154
|
1 |
|
$value = is_array($value) ? $value : [$value]; |
155
|
|
|
foreach ($value as $item) { |
156
|
1 |
|
$this->andFacetFilter[$field][] = $item; |
157
|
|
|
} |
158
|
1 |
|
|
159
|
|
|
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
|
|
|
public function addAndFacetFilter($field, $value): self |
170
|
|
|
{ |
171
|
|
|
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
|
|
|
public function addOrFacetFilter($field, $value): self |
182
|
|
|
{ |
183
|
|
|
$value = is_array($value) ? $value : [$value]; |
184
|
|
|
foreach ($value as $item) { |
185
|
|
|
$this->orFacetFilter[$field][] = $item; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
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
|
|
|
public function addSort($field, $direction): self |
199
|
|
|
{ |
200
|
|
|
$this->sort[$field] = $direction; |
201
|
|
|
|
202
|
|
|
return $this; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|