summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/NestedTest.php
blob: ed6de99ead1301dfcf7a1af1c1c4d1d6558fef61 (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
57
58
59
60
61
62
<?php

namespace Elastica\Test\Aggregation;


use Elastica\Aggregation\Min;
use Elastica\Aggregation\Nested;
use Elastica\Document;
use Elastica\Query;
use Elastica\Type\Mapping;

class NestedTest extends BaseAggregationTest
{
    protected function setUp()
    {
        parent::setUp();
        $this->_index = $this->_createIndex("nested");
        $mapping = new Mapping();
        $mapping->setProperties(array(
            "resellers" => array(
                "type" => "nested",
                "properties" => array(
                    "name" => array("type" => "string"),
                    "price" => array("type" => "double")
                )
            )
        ));
        $type = $this->_index->getType("test");
        $type->setMapping($mapping);
        $docs = array(
            new Document("1", array(
                "resellers" => array(
                    "name" => "spacely sprockets",
                    "price" => 5.55
                )
            )),
            new Document("1", array(
                "resellers" => array(
                    "name" => "cogswell cogs",
                    "price" => 4.98
                )
            ))
        );
        $type->addDocuments($docs);
        $this->_index->refresh();
    }

    public function testNestedAggregation()
    {
        $agg = new Nested("resellers", "resellers");
        $min = new Min("min_price");
        $min->setField("price");
        $agg->addAggregation($min);

        $query = new Query();
        $query->addAggregation($agg);
        $results = $this->_index->search($query)->getAggregation("resellers");

        $this->assertEquals(4.98, $results['min_price']['value']);
    }
}