Completed
Push — master ( 4cdb71...eee063 )
by Nicolas
02:28
created

lib/Elastica/Aggregation/TopHits.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Elastica\Aggregation;
3
4
use Elastica\Script\AbstractScript;
5
use Elastica\Script\ScriptFields;
6
7
/**
8
 * Class TopHits.
9
 *
10
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html
11
 */
12
class TopHits extends AbstractAggregation
13
{
14
    /**
15
     * @return array
16
     */
17
    public function toArray()
18
    {
19
        $array = parent::toArray();
20
21
        // if there are no params, it's ok, but ES will throw exception if json
22
        // will be like {"top_hits":[]} instead of {"top_hits":{}}
23
        if (empty($array['top_hits'])) {
24
            $array['top_hits'] = new \stdClass();
25
        }
26
27
        return $array;
28
    }
29
30
    /**
31
     * The maximum number of top matching hits to return per bucket. By default the top three matching hits are returned.
32
     *
33
     * @param int $size
34
     *
35
     * @return $this
36
     */
37
    public function setSize($size)
38
    {
39
        return $this->setParam('size', (int) $size);
40
    }
41
42
    /**
43
     * The offset from the first result you want to fetch.
44
     *
45
     * @param int $from
46
     *
47
     * @return $this
48
     */
49
    public function setFrom($from)
50
    {
51
        return $this->setParam('from', (int) $from);
52
    }
53
54
    /**
55
     * How the top matching hits should be sorted. By default the hits are sorted by the score of the main query.
56
     *
57
     * @param array $sortArgs
58
     *
59
     * @return $this
60
     */
61
    public function setSort(array $sortArgs)
62
    {
63
        return $this->setParam('sort', $sortArgs);
64
    }
65
66
    /**
67
     * Allows to control how the _source field is returned with every hit.
68
     *
69
     * @param array|bool $params Fields to be returned or false to disable source
70
     *
71
     * @return $this
72
     */
73
    public function setSource($params)
74
    {
75
        return $this->setParam('_source', $params);
76
    }
77
78
    /**
79
     * Returns a version for each search hit.
80
     *
81
     * @param bool $version
82
     *
83
     * @return $this
84
     */
85
    public function setVersion($version)
86
    {
87
        return $this->setParam('version', (bool) $version);
88
    }
89
90
    /**
91
     * Enables explanation for each hit on how its score was computed.
92
     *
93
     * @param bool $explain
94
     *
95
     * @return $this
96
     */
97
    public function setExplain($explain)
98
    {
99
        return $this->setParam('explain', (bool) $explain);
100
    }
101
102
    /**
103
     * Set script fields.
104
     *
105
     * @param array|\Elastica\Script\ScriptFields $scriptFields
106
     *
107
     * @return $this
108
     */
109
    public function setScriptFields($scriptFields)
110
    {
111
        if (is_array($scriptFields)) {
112
            $scriptFields = new ScriptFields($scriptFields);
113
        }
114
115
        return $this->setParam('script_fields', $scriptFields);
116
    }
117
118
    /**
119
     * Adds a Script to the aggregation.
120
     *
121
     * @param string                          $name
122
     * @param \Elastica\Script\AbstractScript $script
123
     *
124
     * @return $this
125
     */
126 View Code Duplication
    public function addScriptField($name, AbstractScript $script)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
127
    {
128
        if (!isset($this->_params['script_fields'])) {
129
            $this->_params['script_fields'] = new ScriptFields();
130
        }
131
132
        $this->_params['script_fields']->addScript($name, $script);
133
134
        return $this;
135
    }
136
137
    /**
138
     * Sets highlight arguments for the results.
139
     *
140
     * @param array $highlightArgs
141
     *
142
     * @return $this
143
     */
144
    public function setHighlight(array $highlightArgs)
145
    {
146
        return $this->setParam('highlight', $highlightArgs);
147
    }
148
149
    /**
150
     * Allows to return the field data representation of a field for each hit.
151
     *
152
     * @param array $fields
153
     *
154
     * @return $this
155
     */
156
    public function setFieldDataFields(array $fields)
157
    {
158
        return $this->setParam('docvalue_fields', $fields);
159
    }
160
}
161