File tree 2 files changed +42
-4
lines changed
test/lib/Elastica/Test/Aggregation
2 files changed +42
-4
lines changed Original file line number Diff line number Diff line change @@ -15,10 +15,11 @@ class Range extends AbstractSimpleAggregation
15
15
* Add a range to this aggregation
16
16
* @param int|float $fromValue low end of this range, exclusive (greater than)
17
17
* @param int|float $toValue high end of this range, exclusive (less than)
18
+ * @param string $key customized key value
18
19
* @return Range
19
20
* @throws \Elastica\Exception\InvalidException
20
21
*/
21
- public function addRange ($ fromValue = null , $ toValue = null )
22
+ public function addRange ($ fromValue = null , $ toValue = null , $ key = null )
22
23
{
23
24
if (is_null ($ fromValue ) && is_null ($ toValue )) {
24
25
throw new InvalidException ("Either fromValue or toValue must be set. Both cannot be null. " );
@@ -30,6 +31,10 @@ public function addRange($fromValue = null, $toValue = null)
30
31
if (!is_null ($ toValue )) {
31
32
$ range ['to ' ] = $ toValue ;
32
33
}
34
+ if (!is_null ($ key )) {
35
+ $ range ['key ' ] = $ key ;
36
+ }
37
+
33
38
return $ this ->addParam ('ranges ' , $ range );
34
39
}
35
40
@@ -42,4 +47,4 @@ public function setKeyedResponse($keyed = true)
42
47
{
43
48
return $ this ->setParam ('keyed ' , (bool )$ keyed );
44
49
}
45
- }
50
+ }
Original file line number Diff line number Diff line change @@ -37,5 +37,38 @@ public function testRangeAggregation()
37
37
38
38
$ this ->assertEquals (2 , $ results ['buckets ' ][0 ]['doc_count ' ]);
39
39
}
40
- }
41
-
40
+
41
+
42
+ public function testRangeAggregationWithKey ()
43
+ {
44
+ $ agg = new Range ("range " );
45
+ $ agg ->setField ("price " );
46
+ $ agg ->addRange (null , 50 , "cheap " );
47
+ $ agg ->addRange (50 , 100 , "average " );
48
+ $ agg ->addRange (100 , null , "expensive " );
49
+
50
+ $ expected = array (
51
+ 'range ' =>
52
+ array (
53
+ 'field ' => 'price ' ,
54
+ 'ranges ' =>
55
+ array (
56
+ array (
57
+ 'to ' => 50 ,
58
+ 'key ' => 'cheap ' ,
59
+ ),
60
+ array (
61
+ 'from ' => 50 ,
62
+ 'to ' => 100 ,
63
+ 'key ' => 'average ' ,
64
+ ),
65
+ array (
66
+ 'from ' => 100 ,
67
+ 'key ' => 'expensive ' ,
68
+ ),
69
+ ),
70
+ ),
71
+ );
72
+
73
+ $ this ->assertEquals ($ expected , $ agg ->toArray ());
74
+ }}
You can’t perform that action at this time.
0 commit comments