summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/IpRangeTest.php
blob: fce6f8578b27b0a664ef4597cc1f3c4b91cf4c98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php

namespace Elastica\Test\Aggregation;


use Elastica\Aggregation\IpRange;
use Elastica\Document;
use Elastica\Query;
use Elastica\Type\Mapping;

class IpRangeTest extends BaseAggregationTest
{
    protected function setUp()
    {
        parent::setUp();
        $this->_index = $this->_createIndex("ip_range");
        $mapping = new Mapping();
        $mapping->setProperties(array(
            "address" => array("type" => "ip")
        ));
        $type = $this->_index->getType("test");
        $type->setMapping($mapping);
        $docs = array(
            new Document("1", array("address" => "192.168.1.100")),
            new Document("2", array("address" => "192.168.1.150")),
            new Document("3", array("address" => "192.168.1.200")),
        );
        $type->addDocuments($docs);
        $this->_index->refresh();
    }

    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']);
            }
        }
    }
}