summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/test/lib/Elastica/Test/Facet/TermsStatsTest.php
blob: bea9b78d26f0c6601c6055dadfc1f83151464087 (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
<?php

namespace Elastica\Test\Facet;

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

class TermsStatsTest extends BaseTest
{
    public function testOrder()
    {
        $client = $this->_getClient();
        $index  = $client->getIndex( 'test' );
        $index->create( array( ), true );
        $type = $index->getType( 'helloworld' );

        $doc = new Document( 1, array( 'name' => 'tom', 'paid' => 7 ) );
        $type->addDocument( $doc );
        $doc   = new Document( 2, array( 'name' => 'tom', 'paid' => 2 ) );
        $type->addDocument( $doc );
        $doc   = new Document( 3, array( 'name' => 'tom', 'paid' => 5 ) );
        $type->addDocument( $doc );
        $doc   = new Document( 4, array( 'name' => 'mike', 'paid' => 13 ) );
        $type->addDocument( $doc );
        $doc   = new Document( 5, array( 'name' => 'mike', 'paid' => 1 ) );
        $type->addDocument( $doc );
        $doc   = new Document( 6, array( 'name' => 'mike', 'paid' => 15 ) );
        $type->addDocument( $doc );

        $facet = new TermsStats( 'test' );
        $facet->setKeyField( 'name' );
        $facet->setValueField( 'paid' );
        $facet->setOrder( 'reverse_total' );

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

        $index->refresh();

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

        $this->assertEquals(14, $facets[ 'test' ][ 'terms' ][0]['total'] );
        $this->assertEquals(29, $facets[ 'test' ][ 'terms' ][1]['total'] );
    }

    public function testQuery()
    {
        $client = $this->_getClient();
        $index  = $client->getIndex( 'test' );
        $index->create( array( ), true );
        $type = $index->getType( 'helloworld' );

        $doc = new Document( 1, array( 'name' => 'tom', 'paid' => 7 ) );
        $type->addDocument( $doc );
        $doc   = new Document( 2, array( 'name' => 'tom', 'paid' => 2 ) );
        $type->addDocument( $doc );
        $doc   = new Document( 3, array( 'name' => 'tom', 'paid' => 5 ) );
        $type->addDocument( $doc );
        $doc   = new Document( 4, array( 'name' => 'mike', 'paid' => 13 ) );
        $type->addDocument( $doc );
        $doc   = new Document( 5, array( 'name' => 'mike', 'paid' => 1 ) );
        $type->addDocument( $doc );
        $doc   = new Document( 6, array( 'name' => 'mike', 'paid' => 15 ) );
        $type->addDocument( $doc );

        $facet = new TermsStats( 'test' );
        $facet->setKeyField( 'name' );
        $facet->setValueField( 'paid' );

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

        $index->refresh();

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

        $this->assertEquals( 2, count( $facets[ 'test' ][ 'terms' ] ) );
        foreach ($facets[ 'test' ][ 'terms' ] as $facet) {
            if ($facet[ 'term' ] === 'tom') {
                $this->assertEquals( 14, $facet[ 'total' ] );
            }
            if ($facet[ 'term' ] === 'mike') {
                $this->assertEquals( 29, $facet[ 'total' ] );
            }
        }
    }

	public function testSetSize()
	{
		$facet = new TermsStats( 'test' );
		$facet->setSize(100);

		$data = $facet->toArray();

		$this->assertArrayHasKey('size', $data['terms_stats']);
		$this->assertEquals(100, $data['terms_stats']['size']);
	}
}