Skip to content

Commit d0512cb

Browse files
committed
[FEATURE] Add greaterThanOrEqual and lessThanOrEqual operations
1 parent 24b10c6 commit d0512cb

3 files changed

Lines changed: 49 additions & 1 deletion

File tree

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,17 @@ public function greaterThan($propertyName, $propertyValue) {
252252
return $this->queryFilter('range', array($propertyName => array('gt' => $propertyValue)));
253253
}
254254

255+
/**
256+
* add a range filter (gte) for the given property
257+
*
258+
* @param string $propertyName
259+
* @param mixed $propertyValue
260+
* @return ElasticSearchQueryBuilder
261+
*/
262+
public function greaterThanOrEqual($propertyName, $propertyValue) {
263+
return $this->queryFilter('range', array($propertyName => array('gte' => $propertyValue)));
264+
}
265+
255266
/**
256267
* add a range filter (lt) for the given property
257268
*
@@ -263,6 +274,18 @@ public function lessThan($propertyName, $propertyValue) {
263274
return $this->queryFilter('range', array($propertyName => array('lt' => $propertyValue)));
264275
}
265276

277+
278+
/**
279+
* add a range filter (lte) for the given property
280+
*
281+
* @param string $propertyName
282+
* @param mixed $propertyValue
283+
* @return ElasticSearchQueryBuilder
284+
*/
285+
public function lessThanOrEqual($propertyName, $propertyValue) {
286+
return $this->queryFilter('range', array($propertyName => array('lte' => $propertyValue)));
287+
}
288+
266289
/**
267290
* LOW-LEVEL API
268291
*/

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,9 @@ Furthermore, the following operators are supported:
174174
* `nodeType("Your.Node:Type")`
175175
* `exactMatch('propertyName', value)`; supports simple types: `exactMatch('tag', 'foo')`, or node references: `exactMatch('author', authorNode)`
176176
* `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
177178
* `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
178180
* `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.
179181
* `limit(5)` -- only return five results. If not specified, the default limit by ElasticSearch applies (which is at 10 by default)
180182
* `from(5)` -- return the results starting from the 6th one

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)