1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\SearchIndex\Query\Algolia; |
4
|
|
|
|
5
|
|
|
class SearchQuery |
6
|
|
|
{ |
7
|
|
|
const LOGICAL_OPERATOR_AND = 'and'; |
8
|
|
|
const LOGICAL_OPERATOR_OR = 'or'; |
9
|
|
|
|
10
|
|
|
protected $query = ''; |
11
|
|
|
|
12
|
|
|
/** @var bool */ |
13
|
|
|
protected $useLocationAwareSearch = false; |
14
|
|
|
|
15
|
|
|
/** @var int */ |
16
|
|
|
protected $lat; |
17
|
|
|
|
18
|
|
|
/** @var int */ |
19
|
|
|
protected $lng; |
20
|
|
|
|
21
|
|
|
/** @var int */ |
22
|
|
|
protected $aroundRadius; |
23
|
|
|
|
24
|
|
|
/** @var array */ |
25
|
|
|
protected $dateRestrictions = []; |
26
|
|
|
|
27
|
|
|
/** @var array */ |
28
|
|
|
protected $numericFilters = []; |
29
|
|
|
|
30
|
|
|
/** @var array */ |
31
|
|
|
protected $facets = []; |
32
|
|
|
|
33
|
|
|
/** @var int */ |
34
|
|
|
protected $hitsPerPage = 10000; |
35
|
|
|
|
36
|
|
|
/** @var int */ |
37
|
|
|
protected $page = 0; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Set the query to search for. |
41
|
|
|
* |
42
|
|
|
* @param mixed $query |
43
|
|
|
* |
44
|
|
|
* @return SearchQuery |
45
|
|
|
*/ |
46
|
|
|
public function searchFor($query) |
47
|
|
|
{ |
48
|
|
|
$this->query = $query; |
49
|
|
|
|
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Create a location aware search. |
55
|
|
|
* |
56
|
|
|
* @param int $lat |
57
|
|
|
* @param int $lng |
58
|
|
|
* @param int $aroundRadius |
59
|
|
|
* |
60
|
|
|
* @return $this |
61
|
|
|
*/ |
62
|
|
|
public function aroundLocation($lat, $lng, $aroundRadius = 30000) |
63
|
|
|
{ |
64
|
|
|
$this->lat = $lat; |
65
|
|
|
$this->lng = $lng; |
66
|
|
|
$this->aroundRadius = $aroundRadius; |
67
|
|
|
$this->useLocationAwareSearch = true; |
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Set a date restriction. |
74
|
|
|
* |
75
|
|
|
* @param string $dateFieldName |
76
|
|
|
* @param string $operation |
77
|
|
|
* @param \DateTime $date |
78
|
|
|
* |
79
|
|
|
* @return $this |
80
|
|
|
*/ |
81
|
|
|
public function withDateRestriction($dateFieldName, $operation, \DateTime $date) |
82
|
|
|
{ |
83
|
|
|
$this->dateRestrictions[] = compact('dateFieldName', 'operation', 'date'); |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Set a facet. |
90
|
|
|
* |
91
|
|
|
* @param string $name |
92
|
|
|
* @param string $value |
93
|
|
|
* |
94
|
|
|
* @return $this |
95
|
|
|
*/ |
96
|
|
|
public function withFacet($name, $value) |
97
|
|
|
{ |
98
|
|
|
$this->facets[] = $name.':'.$value; |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param int $hitsPerPage |
105
|
|
|
* |
106
|
|
|
* @return SearchQuery |
107
|
|
|
*/ |
108
|
|
|
public function setHitsPerPage($hitsPerPage) |
109
|
|
|
{ |
110
|
|
|
$this->hitsPerPage = $hitsPerPage; |
111
|
|
|
|
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param int $page |
117
|
|
|
* |
118
|
|
|
* @return SearchQuery |
119
|
|
|
*/ |
120
|
|
|
public function setPage($page) |
121
|
|
|
{ |
122
|
|
|
$this->page = $page; |
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Set a numeric filter. |
129
|
|
|
* |
130
|
|
|
* @param string $name |
131
|
|
|
* @param string|array $values |
132
|
|
|
* @param string $logicalOperator |
133
|
|
|
* |
134
|
|
|
* @return $this |
135
|
|
|
*/ |
136
|
|
|
public function withNumericFilter($name, $values, $logicalOperator = 'and') |
137
|
|
|
{ |
138
|
|
|
if (!is_array($values)) { |
139
|
|
|
$values = [$values]; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$numericalFilterArray = array_map(function ($value) use ($name) { |
143
|
|
|
return "{$name}={$value}"; |
144
|
|
|
}, $values); |
145
|
|
|
|
146
|
|
|
$numericalFilter = implode(',', $numericalFilterArray); |
147
|
|
|
|
148
|
|
|
if ($logicalOperator == self::LOGICAL_OPERATOR_OR) { |
149
|
|
|
$numericalFilter = "({$numericalFilter})"; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$this->numericFilters[] = $numericalFilter; |
153
|
|
|
|
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Get the query as an array. |
159
|
|
|
* |
160
|
|
|
* @return array |
161
|
|
|
*/ |
162
|
|
|
public function toArray() |
163
|
|
|
{ |
164
|
|
|
$query = [ |
165
|
|
|
'numericFilters' => '', |
166
|
|
|
'facetFilters' => '', |
167
|
|
|
]; |
168
|
|
|
|
169
|
|
|
if ($this->query != '') { |
170
|
|
|
$query['query'] = $this->query; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
if ($this->useLocationAwareSearch) { |
174
|
|
|
$query['aroundLatLng'] = $this->lat.','.$this->lng; |
175
|
|
|
$query['aroundRadius'] = $this->aroundRadius; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
foreach ($this->dateRestrictions as $dateRestriction) { |
179
|
|
|
$query['numericFilters'] .= ','. |
180
|
|
|
$dateRestriction['dateFieldName']. |
181
|
|
|
$dateRestriction['operation']. |
182
|
|
|
$dateRestriction['date']->getTimeStamp(); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
foreach ($this->numericFilters as $filter) { |
186
|
|
|
$query['numericFilters'] .= ','.$filter; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
foreach ($this->facets as $facet) { |
190
|
|
|
$query['facetFilters'] .= ','.$facet; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$query['page'] = $this->page; |
194
|
|
|
$query['hitsPerPage'] = $this->hitsPerPage; |
195
|
|
|
|
196
|
|
|
return $query; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|