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
|
|
|
use ReflectionException; |
15
|
|
|
|
16
|
|
|
use SilverStripe\Core\ClassInfo; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Trait BaseQueryTrait Extraction from the BaseQuery class to keep things readable. |
20
|
|
|
* |
21
|
|
|
* This trait adds the support for adding the basic field/filter/term/facet options |
22
|
|
|
* |
23
|
|
|
* @package Firesphere\SolrSearch\Traits |
24
|
|
|
*/ |
25
|
|
|
trait BaseQueryTrait |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var array Terms to search |
29
|
|
|
*/ |
30
|
|
|
protected $terms = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array Fields to filter |
34
|
|
|
*/ |
35
|
|
|
protected $filter = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array Fields to search |
39
|
|
|
*/ |
40
|
|
|
protected $fields = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Key => value pairs of facets to apply |
44
|
|
|
* [ |
45
|
|
|
* 'FacetTitle' => [1, 2, 3] |
46
|
|
|
* ] |
47
|
|
|
* |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
protected $facetFilter = []; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var array Fields to exclude |
54
|
|
|
*/ |
55
|
|
|
protected $exclude = []; |
56
|
|
|
/** |
57
|
|
|
* @var array Classes to exclude through hierarchy |
58
|
|
|
*/ |
59
|
|
|
protected $excludedSubClasses = []; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Each boosted query needs a separate addition! |
63
|
|
|
* e.g. $this->addTerm('test', ['MyField', 'MyOtherField'], 3) |
64
|
|
|
* followed by |
65
|
|
|
* $this->addTerm('otherTest', ['Title'], 5); |
66
|
|
|
* |
67
|
|
|
* If you want a generic boost on all terms, use addTerm only once, but boost on each field |
68
|
|
|
* |
69
|
|
|
* The fields parameter is used to boost on |
70
|
7 |
|
* |
71
|
|
|
* For generic boosting, use @addBoostedField($field, $boost), this will add the boost at Index time |
72
|
7 |
|
* |
73
|
7 |
|
* @param string $term Term to search for |
74
|
7 |
|
* @param array $fields fields to boost on |
75
|
7 |
|
* @param int $boost Boost value |
76
|
7 |
|
* @param bool|float $fuzzy True or a value to the maximum amount of iterations |
77
|
|
|
* @return $this |
78
|
|
|
*/ |
79
|
7 |
|
public function addTerm($term, $fields = [], $boost = 0, $fuzzy = null): self |
80
|
|
|
{ |
81
|
|
|
$this->terms[] = [ |
82
|
|
|
'text' => $term, |
83
|
|
|
'fields' => $fields, |
84
|
|
|
'boost' => $boost, |
85
|
|
|
'fuzzy' => $fuzzy, |
86
|
|
|
]; |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
2 |
|
} |
90
|
|
|
|
91
|
2 |
|
/** |
92
|
2 |
|
* Adds filters to filter on by value |
93
|
|
|
* |
94
|
2 |
|
* @param string $field |
95
|
|
|
* @param string|array $value |
96
|
|
|
* @return $this |
97
|
|
|
*/ |
98
|
|
|
public function addFilter($field, $value): self |
99
|
|
|
{ |
100
|
|
|
$field = str_replace('.', '_', $field); |
101
|
|
|
$this->filter[$field] = $value; |
102
|
|
|
|
103
|
2 |
|
return $this; |
104
|
|
|
} |
105
|
2 |
|
|
106
|
2 |
|
/** |
107
|
|
|
* Add a field to be returned |
108
|
2 |
|
* |
109
|
|
|
* @param string $field fieldname |
110
|
|
|
* @return $this |
111
|
|
|
*/ |
112
|
|
|
public function addField($field): self |
113
|
|
|
{ |
114
|
|
|
$field = str_replace('.', '_', $field); |
115
|
|
|
$this->fields[] = $field; |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
2 |
|
} |
119
|
|
|
|
120
|
2 |
|
/** |
121
|
2 |
|
* Exclude fields from the search action |
122
|
|
|
* |
123
|
2 |
|
* @param string $field |
124
|
|
|
* @param string|array $value |
125
|
|
|
* @return $this |
126
|
|
|
*/ |
127
|
|
|
public function addExclude($field, $value): self |
128
|
|
|
{ |
129
|
|
|
$field = str_replace('.', '_', $field); |
130
|
|
|
$this->exclude[$field] = $value; |
131
|
|
|
|
132
|
|
|
return $this; |
133
|
2 |
|
} |
134
|
|
|
|
135
|
2 |
|
/** |
136
|
|
|
* Add the subclasses of the given class to exclude. |
137
|
2 |
|
* It's all merged in to a single array, for easier generation of the filter. |
138
|
|
|
* |
139
|
|
|
* @param string $class |
140
|
|
|
* @return $this |
141
|
|
|
* @throws ReflectionException |
142
|
|
|
*/ |
143
|
|
|
public function addExcludedSubclasses($class): self |
144
|
|
|
{ |
145
|
|
|
$subClasses = ClassInfo::subclassesFor($class, false); |
146
|
|
|
$excluded = $this->getExcludedSubClasses(); |
|
|
|
|
147
|
|
|
$this->excludedSubClasses = array_merge($excluded, $subClasses); |
148
|
|
|
|
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Add faceting |
154
|
|
|
* |
155
|
|
|
* @param string $field |
156
|
|
|
* @param string|array $value |
157
|
|
|
* @return $this |
158
|
|
|
*/ |
159
|
|
|
public function addFacetFilter($field, $value): self |
160
|
|
|
{ |
161
|
|
|
$this->facetFilter[$field][] = $value; |
162
|
|
|
|
163
|
|
|
return $this; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|