summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/ReverseNestedTest.php
blob: 215dac63fdce7cc2636b468f6ba6a5dbe36ad3e7 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php

namespace Elastica\Test\Aggregation;

use Elastica\Aggregation\Terms;
use Elastica\Aggregation\Nested;
use Elastica\Aggregation\ReverseNested;
use Elastica\Document;
use Elastica\Query;
use Elastica\Type\Mapping;

class ReverseNestedTest extends BaseAggregationTest
{
    protected function setUp()
    {
        parent::setUp();
        $this->_index = $this->_createIndex("nested");
        $mapping = new Mapping();
        $mapping->setProperties(array(
            "comments" => array(
                "type" => "nested",
                "properties" => array(
                    "name" => array("type" => "string"),
                    "body" => array("type" => "string")
                )
            )
        ));
        $type = $this->_index->getType("test");
        $type->setMapping($mapping);
        $docs = array(
            new Document("1", array(
                "comments" => array(
                    array(
                        "name" => "bob",
                        "body" => "this is bobs comment",
                    ),
                    array(
                        "name" => "john",
                        "body" => "this is johns comment",
                    ),
                ),
                "tags" => array("foo", "bar"),
            )),
            new Document("2", array(
                 "comments" => array(
                    array(
                        "name" => "bob",
                        "body" => "this is another comment from bob",
                    ),
                    array(
                        "name" => "susan",
                        "body" => "this is susans comment",
                    ),
                ),
                "tags" => array("foo", "baz"),
            ))
        );
        $type->addDocuments($docs);
        $this->_index->refresh();
    }

    public function testPathNotSetIfNull()
    {
        $agg = new ReverseNested('nested');
        $this->assertFalse($agg->hasParam('path'));
    }

    public function testPathSetIfNotNull()
    {
        $agg = new ReverseNested('nested', 'some_field');
        $this->assertEquals('some_field', $agg->getParam('path'));
    }

    public function testReverseNestedAggregation()
    {
        $agg = new Nested("comments", "comments");
        $names = new Terms("name");
        $names->setField("comments.name");

        $tags = new Terms("tags");
        $tags->setField("tags");

        $reverseNested = new ReverseNested("main");
        $reverseNested->addAggregation($tags);

        $names->addAggregation($reverseNested);

        $agg->addAggregation($names);

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

        $this->assertArrayHasKey('name', $results);
        $nameResults = $results['name'];

        $this->assertCount(3, $nameResults['buckets']);

        // bob
        $this->assertEquals('bob', $nameResults['buckets'][0]['key']);
        $tags = array(
            array('key' => 'foo', 'doc_count' => 2),
            array('key' => 'bar', 'doc_count' => 1),
            array('key' => 'baz', 'doc_count' => 1),
        );
        $this->assertEquals($tags, $nameResults['buckets'][0]['main']['tags']['buckets']);

        // john
        $this->assertEquals('john', $nameResults['buckets'][1]['key']);
        $tags = array(
            array('key' => 'bar', 'doc_count' => 1),
            array('key' => 'foo', 'doc_count' => 1),
        );
        $this->assertEquals($tags, $nameResults['buckets'][1]['main']['tags']['buckets']);

        // susan
        $this->assertEquals('susan', $nameResults['buckets'][2]['key']);
        $tags = array(
            array('key' => 'baz', 'doc_count' => 1),
            array('key' => 'foo', 'doc_count' => 1),
        );
        $this->assertEquals($tags, $nameResults['buckets'][2]['main']['tags']['buckets']);
    }
}