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 |
41
|
|
|
* [ |
42
|
|
|
* 'FacetTitle' => [1, 2, 3] |
43
|
|
|
* ] |
44
|
|
|
* |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
protected $facetFilter = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Key => value pairs of facets to apply |
51
|
|
|
* [ |
52
|
|
|
* 'FacetTitle' => [1, 2, 3], |
53
|
|
|
* 'FacetTitle2' => [1, 2, 3] |
54
|
|
|
* ] |
55
|
|
|
* |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
protected $orFacetFilter = []; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var array Fields to exclude |
62
|
|
|
*/ |
63
|
|
|
protected $exclude = []; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var array Sorting order |
67
|
|
|
*/ |
68
|
|
|
protected $sort = []; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Each boosted query needs a separate addition! |
72
|
|
|
* e.g. $this->addTerm('test', ['MyField', 'MyOtherField'], 3) |
73
|
|
|
* followed by |
74
|
|
|
* $this->addTerm('otherTest', ['Title'], 5); |
75
|
|
|
* |
76
|
|
|
* If you want a generic boost on all terms, use addTerm only once, but boost on each field |
77
|
7 |
|
* |
78
|
|
|
* The fields parameter is used to boost on |
79
|
7 |
|
* |
80
|
7 |
|
* For generic boosting, use @addBoostedField($field, $boost), this will add the boost at Index time |
81
|
7 |
|
* |
82
|
7 |
|
* @param string $term Term to search for |
83
|
7 |
|
* @param array $fields fields to boost on |
84
|
|
|
* @param int $boost Boost value |
85
|
|
|
* @param bool|float $fuzzy True or a value to the maximum amount of iterations |
86
|
7 |
|
* @return $this |
87
|
|
|
*/ |
88
|
|
|
public function addTerm(string $term, array $fields = [], int $boost = 0, $fuzzy = null): self |
89
|
|
|
{ |
90
|
|
|
$this->terms[] = [ |
91
|
|
|
'text' => $term, |
92
|
|
|
'fields' => $fields, |
93
|
|
|
'boost' => $boost, |
94
|
|
|
'fuzzy' => $fuzzy, |
95
|
|
|
]; |
96
|
4 |
|
|
97
|
|
|
return $this; |
98
|
4 |
|
} |
99
|
4 |
|
|
100
|
|
|
/** |
101
|
4 |
|
* Adds filters to filter on by value |
102
|
|
|
* |
103
|
|
|
* @param string $field Field to filter on |
104
|
|
|
* @param string|array|Criteria $value Value for this field |
105
|
|
|
* @return $this |
106
|
|
|
*/ |
107
|
|
|
public function addFilter($field, $value): self |
108
|
|
|
{ |
109
|
|
|
$field = str_replace('.', '_', $field); |
110
|
2 |
|
$this->filter[$field] = $value; |
111
|
|
|
|
112
|
2 |
|
return $this; |
113
|
2 |
|
} |
114
|
|
|
|
115
|
2 |
|
/** |
116
|
|
|
* Add a field to be returned |
117
|
|
|
* |
118
|
|
|
* @param string $field fieldname |
119
|
|
|
* @return $this |
120
|
|
|
*/ |
121
|
|
|
public function addField($field): self |
122
|
|
|
{ |
123
|
|
|
$field = str_replace('.', '_', $field); |
124
|
|
|
$this->fields[] = $field; |
125
|
4 |
|
|
126
|
|
|
return $this; |
127
|
4 |
|
} |
128
|
4 |
|
|
129
|
|
|
/** |
130
|
4 |
|
* Exclude fields from the search action |
131
|
|
|
* |
132
|
|
|
* @param string $field |
133
|
|
|
* @param string|array|Criteria $value |
134
|
|
|
* @return $this |
135
|
|
|
*/ |
136
|
|
|
public function addExclude($field, $value): self |
137
|
|
|
{ |
138
|
|
|
$field = str_replace('.', '_', $field); |
139
|
|
|
$this->exclude[$field] = $value; |
140
|
2 |
|
|
141
|
|
|
return $this; |
142
|
2 |
|
} |
143
|
|
|
|
144
|
2 |
|
/** |
145
|
|
|
* Add faceting |
146
|
|
|
* |
147
|
|
|
* @param string $field Field to facet |
148
|
|
|
* @param string|array $value Value to facet |
149
|
|
|
* @return $this |
150
|
|
|
*/ |
151
|
|
|
public function addFacetFilter($field, $value): self |
152
|
|
|
{ |
153
|
|
|
$this->facetFilter[$field][] = $value; |
154
|
1 |
|
|
155
|
|
|
return $this; |
156
|
1 |
|
} |
157
|
|
|
|
158
|
1 |
|
/** |
159
|
|
|
* Add faceting |
160
|
|
|
* |
161
|
|
|
* @param string $field Field to facet |
162
|
|
|
* @param string|array $value Value to facet |
163
|
|
|
* @return $this |
164
|
|
|
*/ |
165
|
|
|
public function addOrFacetFilter($field, $value): self |
166
|
|
|
{ |
167
|
|
|
$this->orFacetFilter[$field][] = $value; |
168
|
|
|
|
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Add a field to sort on |
174
|
|
|
* |
175
|
|
|
* @param string $field |
176
|
|
|
* @param string $direction |
177
|
|
|
* @return $this |
178
|
|
|
*/ |
179
|
|
|
public function addSort($field, $direction): self |
180
|
|
|
{ |
181
|
|
|
$this->sort[$field] = $direction; |
182
|
|
|
|
183
|
|
|
return $this; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|