Elastica\Aggregation\IpRange::addRange PHP Method

addRange() public method

Add an ip range to this aggregation.
public addRange ( string $fromValue = null, string $toValue = null )
$fromValue string a valid ipv4 address. Low end of this range, exclusive (greater than)
$toValue string a valid ipv4 address. High end of this range, exclusive (less than)
    public function addRange($fromValue = null, $toValue = null)
    {
        if (is_null($fromValue) && is_null($toValue)) {
            throw new InvalidException('Either fromValue or toValue must be set. Both cannot be null.');
        }
        $range = [];
        if (!is_null($fromValue)) {
            $range['from'] = $fromValue;
        }
        if (!is_null($toValue)) {
            $range['to'] = $toValue;
        }
        return $this->addParam('ranges', $range);
    }

Usage Example

 public function testIpRangeAggregation()
 {
     $agg = new IpRange("ip", "address");
     $agg->addRange("192.168.1.101");
     $agg->addRange(null, "192.168.1.200");
     $cidrRange = "192.168.1.0/24";
     $agg->addMaskRange($cidrRange);
     $query = new Query();
     $query->addAggregation($agg);
     $results = $this->_index->search($query)->getAggregation("ip");
     foreach ($results['buckets'] as $bucket) {
         if (array_key_exists('key', $bucket) && $bucket['key'] == $cidrRange) {
             // the CIDR mask
             $this->assertEquals(3, $bucket['doc_count']);
         } else {
             // the normal ip ranges
             $this->assertEquals(2, $bucket['doc_count']);
         }
     }
 }