Skip to content

Commit c3ff964

Browse files
author
Christian Müller
committed
Merge pull request #48 from networkteam/range-queries
[FEATURE] Support greaterThan and lessThan for range filters
2 parents ebb3dc8 + 22dca08 commit c3ff964

3 files changed

Lines changed: 81 additions & 9 deletions

File tree

Classes/Flowpack/ElasticSearch/ContentRepositoryAdaptor/Eel/ElasticSearchQueryBuilder.php

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,16 +229,61 @@ public function from($from) {
229229
/**
230230
* add an exact-match query for a given property
231231
*
232-
* @param $propertyName
233-
* @param $propertyValue
232+
* @param string $propertyName Name of the property
233+
* @param mixed $value Value for comparison
234234
* @return ElasticSearchQueryBuilder
235235
*/
236-
public function exactMatch($propertyName, $propertyValue) {
237-
if ($propertyValue instanceof NodeInterface) {
238-
$propertyValue = $propertyValue->getIdentifier();
236+
public function exactMatch($propertyName, $value) {
237+
if ($value instanceof NodeInterface) {
238+
$value = $value->getIdentifier();
239239
}
240240

241-
return $this->queryFilter('term', array($propertyName => $propertyValue));
241+
return $this->queryFilter('term', array($propertyName => $value));
242+
}
243+
244+
/**
245+
* add a range filter (gt) for the given property
246+
*
247+
* @param string $propertyName Name of the property
248+
* @param mixed $value Value for comparison
249+
* @return ElasticSearchQueryBuilder
250+
*/
251+
public function greaterThan($propertyName, $value) {
252+
return $this->queryFilter('range', array($propertyName => array('gt' => $value)));
253+
}
254+
255+
/**
256+
* add a range filter (gte) for the given property
257+
*
258+
* @param string $propertyName Name of the property
259+
* @param mixed $value Value for comparison
260+
* @return ElasticSearchQueryBuilder
261+
*/
262+
public function greaterThanOrEqual($propertyName, $value) {
263+
return $this->queryFilter('range', array($propertyName => array('gte' => $value)));
264+
}
265+
266+
/**
267+
* add a range filter (lt) for the given property
268+
*
269+
* @param string $propertyName Name of the property
270+
* @param mixed $value Value for comparison
271+
* @return ElasticSearchQueryBuilder
272+
*/
273+
public function lessThan($propertyName, $value) {
274+
return $this->queryFilter('range', array($propertyName => array('lt' => $value)));
275+
}
276+
277+
278+
/**
279+
* add a range filter (lte) for the given property
280+
*
281+
* @param string $propertyName Name of the property
282+
* @param mixed $value Value for comparison
283+
* @return ElasticSearchQueryBuilder
284+
*/
285+
public function lessThanOrEqual($propertyName, $value) {
286+
return $this->queryFilter('range', array($propertyName => array('lte' => $value)));
242287
}
243288

244289
/**

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,11 @@ search underneath the current site node (like in the example above).
172172
Furthermore, the following operators are supported:
173173

174174
* `nodeType("Your.Node:Type")`
175-
* `exactMatch(key, value)`; supports simple types: `exactMatch('tag', 'foo')`, or node references: `exactMatch('author', authorNode)`
175+
* `exactMatch('propertyName', value)`; supports simple types: `exactMatch('tag', 'foo')`, or node references: `exactMatch('author', authorNode)`
176+
* `greaterThan('propertyName', value)` -- range filter with property values greater than the given value
177+
* `greaterThanOrEqual('propertyName', value)` -- range filter with property values greater than or equal to the given value
178+
* `lessThan('propertyName', value)` -- range filter with property values less than the given value
179+
* `lessThanOrEqual('propertyName', value)` -- range filter with property values less than or equal to the given value
176180
* `sortAsc('propertyName')` and `sortDesc('propertyName')` -- can also be used multiple times, e.g. `sortAsc('tag').sortDesc(`date')` will first sort by tag ascending, and then by date descending.
177181
* `limit(5)` -- only return five results. If not specified, the default limit by ElasticSearch applies (which is at 10 by default)
178182
* `from(5)` -- return the results starting from the 6th one
@@ -347,4 +351,4 @@ In order to understand what's going on, the following commands are helpful:
347351

348352
* use `./flow nodeindex:showMapping` to show the currently defined ElasticSearch Mapping
349353
* use the `.log()` statement inside queries to dump them to the ElasticSearch Log
350-
* the logfile `Data/Logs/ElasticSearch.log` contains loads of helpful information.
354+
* the logfile `Data/Logs/ElasticSearch.log` contains loads of helpful information.

Tests/Unit/Eel/ElasticSearchQueryBuilderTest.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ public function limitWorks() {
160160
$this->assertSame(2, $actual['size']);
161161
}
162162

163-
164163
/**
165164
* @test
166165
*/
@@ -175,6 +174,30 @@ public function sortDescWorks() {
175174
$this->assertSame($expected, $actual['sort']);
176175
}
177176

177+
public function rangeConstraintExamples() {
178+
return array(
179+
array('greaterThan', 'gt', 10),
180+
array('greaterThanOrEqual', 'gte', 20),
181+
array('lessThan', 'lt', 'now'),
182+
array('lessThanOrEqual', 'lte', 40)
183+
);
184+
}
185+
186+
/**
187+
* @test
188+
* @dataProvider rangeConstraintExamples
189+
*/
190+
public function rangeConstraintsWork($constraint, $operator, $value) {
191+
$this->queryBuilder->$constraint('fieldName', $value);
192+
$expected = array(
193+
'range' => array(
194+
'fieldName' => array($operator => $value)
195+
)
196+
);
197+
$actual = $this->queryBuilder->getRequest();
198+
$this->assertInArray($expected, $actual['query']['filtered']['filter']['bool']['must']);
199+
}
200+
178201
/**
179202
* Test helper
180203
*

0 commit comments

Comments
 (0)