summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/test/lib/Elastica/Test/Facet/StatisticalTest.php
blob: dc3742893da12a2dbf9f7f80e42e59bc0c0f1c37 (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
<?php
namespace Elastica\Test\Facet;

use Elastica\Document;
use Elastica\Facet\Statistical;
use Elastica\Query;
use Elastica\Query\MatchAll;
use Elastica\Test\Base as BaseTest;

/**
 * @todo Add test for Statistical with setScript
 */
class StatisticalTest extends BaseTest
{
    /**
     * @group functional
     */
    public function testStatisticalWithSetField()
    {
        $client = $this->_getClient();
        $index = $client->getIndex('test');
        $index->create(array(), true);
        $type = $index->getType('helloworld');

        $doc = new Document(1, array('price' => 10));
        $type->addDocument($doc);
        $doc = new Document(2, array('price' => 35));
        $type->addDocument($doc);
        $doc = new Document(2, array('price' => 45));
        $type->addDocument($doc);

        $facet = new Statistical('stats');
        $facet->setField('price');

        $query = new Query();
        $query->addFacet($facet);
        $query->setQuery(new MatchAll());

        $index->refresh();

        $response = $type->search($query);
        $facets = $response->getFacets();

        $this->assertEquals(55, $facets['stats']['total']);
        $this->assertEquals(10, $facets['stats']['min']);
        $this->assertEquals(45, $facets['stats']['max']);
    }

    /**
     * @group functional
     */
    public function testStatisticalWithSetFields()
    {
        $client = $this->_getClient();
        $index = $client->getIndex('test');
        $index->create(array(), true);
        $type = $index->getType('helloworld');

        $doc = new Document(1, array('price' => 10, 'price2' => 20));
        $type->addDocument($doc);
        $doc = new Document(2, array('price' => 35, 'price2' => 70));
        $type->addDocument($doc);
        $doc = new Document(2, array('price' => 45, 'price2' => 90));
        $type->addDocument($doc);

        $facet = new Statistical('stats');
        $facet->setFields(array('price', 'price2'));

        $query = new Query();
        $query->addFacet($facet);
        $query->setQuery(new MatchAll());

        $index->refresh();

        $response = $type->search($query);
        $facets = $response->getFacets();

        $this->assertEquals(165, $facets['stats']['total']);
        $this->assertEquals(10, $facets['stats']['min']);
        $this->assertEquals(90, $facets['stats']['max']);
    }
}