summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/lib/Elastica/Facet/Terms.php
blob: c1dab80077ac54625f4adf88f94a135279049be7 (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
125
126
127
128
129
<?php

namespace Elastica\Facet;

use Elastica\Exception\InvalidException;
use Elastica\Script;

/**
 * Implements the terms facet.
 *
 * @category Xodoa
 * @package Elastica
 * @author Nicolas Ruflin <spam@ruflin.com>
 * @author Jasper van Wanrooy <jasper@vanwanrooy.net>
 * @link http://www.elasticsearch.org/guide/reference/api/search/facets/terms-facet.html
 */
class Terms extends AbstractFacet
{
    /**
     * Holds the types of ordering which are allowed
     * by Elasticsearch.
     *
     * @var array
     */
    protected $_orderTypes = array('count', 'term', 'reverse_count', 'reverse_term');

    /**
     * Sets the field for the terms.
     *
     * @param  string                    $field The field name for the terms.
     * @return \Elastica\Facet\Terms
     */
    public function setField($field)
    {
        return $this->setParam('field', $field);
    }

    /**
     * Sets the script for the term.
     *
     * @param  string                   $script The script for the term.
     * @return \Elastica\Facet\Terms
     */
    public function setScript($script)
    {
        $script = Script::create($script);
        foreach ($script->toArray() as $param => $value) {
            $this->setParam($param, $value);
        }

        return $this;
    }

    /**
     * Sets multiple fields for the terms.
     *
     * @param  array                     $fields Numerical array with the fields for the terms.
     * @return \Elastica\Facet\Terms
     */
    public function setFields(array $fields)
    {
        return $this->setParam('fields', $fields);
    }

    /**
     * Sets the flag to return all available terms. When they
     * don't have a hit, they have a count of zero.
     *
     * @param  bool                      $allTerms Flag to fetch all terms.
     * @return \Elastica\Facet\Terms
     */
    public function setAllTerms($allTerms)
    {
        return $this->setParam('all_terms', (bool) $allTerms);
    }

    /**
     * Sets the ordering type for this facet. Elasticsearch
     * internal default is count.
     *
     * @param  string                              $type The order type to set use for sorting of the terms.
     * @throws \Elastica\Exception\InvalidException When an invalid order type was set.
     * @return \Elastica\Facet\Terms
     */
    public function setOrder($type)
    {
        if (!in_array($type, $this->_orderTypes)) {
            throw new InvalidException('Invalid order type: ' . $type);
        }

        return $this->setParam('order', $type);
    }

    /**
     * Set an array with terms which are omitted in the search.
     *
     * @param  array                     $exclude Numerical array which includes all terms which needs to be ignored.
     * @return \Elastica\Facet\Terms
     */
    public function setExclude(array $exclude)
    {
        return $this->setParam('exclude', $exclude);
    }

    /**
     * Sets the amount of terms to be returned.
     *
     * @param  int                       $size The amount of terms to be returned.
     * @return \Elastica\Facet\Terms
     */
    public function setSize($size)
    {
        return $this->setParam('size', (int) $size);
    }

    /**
     * Creates the full facet definition, which includes the basic
     * facet definition of the parent.
     *
     * @see \Elastica\Facet\AbstractFacet::toArray()
     * @return array
     */
    public function toArray()
    {
        $this->_setFacetParam('terms', $this->_params);

        return parent::toArray();
    }
}