|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Firesphere\SolrSearch\Traits; |
|
5
|
|
|
|
|
6
|
|
|
trait BaseQueryTrait |
|
7
|
|
|
{ |
|
8
|
|
|
/** |
|
9
|
|
|
* @var array |
|
10
|
|
|
*/ |
|
11
|
|
|
protected $terms = []; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var array |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $filter = []; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $fields = []; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $facetFilter = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $exclude = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Each boosted query needs a separate addition! |
|
35
|
|
|
* e.g. $this->addTerm('test', ['MyField', 'MyOtherField'], 3) |
|
36
|
|
|
* followed by |
|
37
|
|
|
* $this->addTerm('otherTest', ['Title'], 5); |
|
38
|
|
|
* |
|
39
|
|
|
* If you want a generic boost on all terms, use addTerm only once, but boost on each field |
|
40
|
|
|
* |
|
41
|
|
|
* The fields parameter is used to boost on |
|
42
|
|
|
* |
|
43
|
|
|
* For generic boosting, use @addBoostedField($field, $boost), this will add the boost at Index time |
|
44
|
|
|
* @param string $term Term to search for |
|
45
|
|
|
* @param array $fields fields to boost on |
|
46
|
|
|
* @param int $boost Boost value |
|
47
|
|
|
* @param bool|float $fuzzy True or a value to the maximum amount of iterations |
|
48
|
|
|
* @return $this |
|
49
|
|
|
*/ |
|
50
|
|
|
public function addTerm($term, $fields = [], $boost = 0, $fuzzy = null): self |
|
51
|
|
|
{ |
|
52
|
|
|
$this->terms[] = [ |
|
53
|
|
|
'text' => $term, |
|
54
|
|
|
'fields' => $fields, |
|
55
|
|
|
'boost' => $boost, |
|
56
|
|
|
'fuzzy' => $fuzzy |
|
57
|
|
|
]; |
|
58
|
|
|
|
|
59
|
|
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param string $field |
|
64
|
|
|
* @param string $value |
|
65
|
|
|
* @return $this |
|
66
|
|
|
*/ |
|
67
|
|
|
public function addFilter($field, $value): self |
|
68
|
|
|
{ |
|
69
|
|
|
$field = str_replace('.', '_', $field); |
|
70
|
|
|
$this->filter[$field] = $value; |
|
71
|
|
|
|
|
72
|
|
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Add a field to be returned |
|
77
|
|
|
* @param string $field fieldname |
|
78
|
|
|
* @return $this |
|
79
|
|
|
*/ |
|
80
|
|
|
public function addField($field): self |
|
81
|
|
|
{ |
|
82
|
|
|
$field = str_replace('.', '_', $field); |
|
83
|
|
|
$this->fields[] = $field; |
|
84
|
|
|
|
|
85
|
|
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param $field |
|
90
|
|
|
* @param $value |
|
91
|
|
|
* @return $this |
|
92
|
|
|
*/ |
|
93
|
|
|
public function addExclude($field, $value): self |
|
94
|
|
|
{ |
|
95
|
|
|
$field = str_replace('.', '_', $field); |
|
96
|
|
|
$this->exclude[$field] = $value; |
|
97
|
|
|
|
|
98
|
|
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param string $field |
|
103
|
|
|
* @param string $value |
|
104
|
|
|
* @return $this |
|
105
|
|
|
*/ |
|
106
|
|
|
public function addFacetFilter($field, $value): self |
|
107
|
|
|
{ |
|
108
|
|
|
$this->facetFilter[$field][] = $value; |
|
109
|
|
|
|
|
110
|
|
|
return $this; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|