summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/lib/Elastica/Facet/Range.php
blob: f81caf3d24b17ea00ee46f27c069c30b0595a625 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
namespace Elastica\Facet;

use Elastica\Exception\InvalidException;

/**
 * Implements the range facet.
 *
 * @author Jasper van Wanrooy <jasper@vanwanrooy.net>
 *
 * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/search-facets-range-facet.html
 * @deprecated Facets are deprecated and will be removed in a future release. You are encouraged to migrate to aggregations instead.
 */
class Range extends AbstractFacet
{
    /**
     * Sets the field for the range.
     *
     * @param string $field The name of the field for range.
     *
     * @return $this
     */
    public function setField($field)
    {
        return $this->setParam('field', $field);
    }

    /**
     * Sets the fields by their separate key and value fields.
     *
     * @param string $keyField   The key_field param for the range.
     * @param string $valueField The key_value param for the range.
     *
     * @return $this
     */
    public function setKeyValueFields($keyField, $valueField)
    {
        return $this->setParam('key_field', $keyField)
                    ->setParam('value_field', $valueField);
    }

    /**
     * Sets the key and value for this facet by script.
     *
     * @param string $keyScript   Script to check whether it falls into the range.
     * @param string $valueScript Script to use for statistical calculations.
     *
     * @return $this
     */
    public function setKeyValueScripts($keyScript, $valueScript)
    {
        return $this->setParam('key_script', $keyScript)
                    ->setParam('value_script', $valueScript);
    }

    /**
     * Sets the ranges for the facet all at once. Sample ranges:
     * array (
     *     array('to' => 50),
     *     array('from' => 20, 'to' 70),
     *     array('from' => 70, 'to' => 120),
     *     array('from' => 150)
     * ).
     *
     * @param array $ranges Numerical array with range definitions.
     *
     * @return $this
     */
    public function setRanges(array $ranges)
    {
        return $this->setParam('ranges', $ranges);
    }

    /**
     * Adds a range to the range facet.
     *
     * @param mixed $from The from for the range.
     * @param mixed $to   The to for the range.
     *
     * @return $this
     */
    public function addRange($from = null, $to = null)
    {
        if (!isset($this->_params['ranges']) || !is_array($this->_params['ranges'])) {
            $this->_params['ranges'] = array();
        }

        $range = array();
        if (isset($from)) {
            $range['from'] = $from;
        }
        if (isset($to)) {
            $range['to'] = $to;
        }
        $this->_params['ranges'][] = $range;

        return $this;
    }

    /**
     * Creates the full facet definition, which includes the basic
     * facet definition of the parent.
     *
     * @see \Elastica\Facet\AbstractFacet::toArray()
     *
     * @throws \Elastica\Exception\InvalidException When the right fields haven't been set.
     *
     * @return array
     */
    public function toArray()
    {
        /*
         * Check the facet for validity.
         * There are three ways to set the key and value field for the range:
         *  - a single field for both key and value; or
         *  - separate fields for key and value; or
         *  - separate scripts for key and value.
         */
        $fieldTypesSet = 0;
        if (isset($this->_params['field'])) {
            $fieldTypesSet++;
        }
        if (isset($this->_params['key_field'])) {
            $fieldTypesSet++;
        }
        if (isset($this->_params['key_script'])) {
            $fieldTypesSet++;
        }

        if ($fieldTypesSet === 0) {
            throw new InvalidException('Neither field, key_field nor key_script is set.');
        } elseif ($fieldTypesSet > 1) {
            throw new InvalidException('Either field, key_field and key_value or key_script and value_script should be set.');
        }

        /*
         * Set the range in the abstract as param.
         */
        $this->_setFacetParam('range', $this->_params);

        return parent::toArray();
    }
}