Completed
Push — master ( aa6457...bb06c3 )
by Federico
02:20
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
3
namespace Elastica\Aggregation;
4
5
use Elastica\Script\AbstractScript;
6
use Elastica\Script\ScriptFields;
7
8
/**
9
 * Class TopHits.
10
 *
11
 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html
12
 */
13
class TopHits extends AbstractAggregation
14
{
15
    /**
16
     * @return array
17
     */
18 View Code Duplication
    public function toArray()
19
    {
20
        $array = parent::toArray();
21
22
        // if there are no params, it's ok, but ES will throw exception if json
23
        // will be like {"top_hits":[]} instead of {"top_hits":{}}
24
        if (empty($array['top_hits'])) {
25
            $array['top_hits'] = new \stdClass();
26
        }
27
28
        return $array;
29
    }
30
31
    /**
32
     * The maximum number of top matching hits to return per bucket. By default the top three matching hits are returned.
33
     *
34
     * @param int $size
35
     *
36
     * @return $this
37
     */
38
    public function setSize($size)
39
    {
40
        return $this->setParam('size', (int) $size);
41
    }
42
43
    /**
44
     * The offset from the first result you want to fetch.
45
     *
46
     * @param int $from
47
     *
48
     * @return $this
49
     */
50
    public function setFrom($from)
51
    {
52
        return $this->setParam('from', (int) $from);
53
    }
54
55
    /**
56
     * How the top matching hits should be sorted. By default the hits are sorted by the score of the main query.
57
     *
58
     * @param array $sortArgs
59
     *
60
     * @return $this
61
     */
62
    public function setSort(array $sortArgs)
63
    {
64
        return $this->setParam('sort', $sortArgs);
65
    }
66
67
    /**
68
     * Allows to control how the _source field is returned with every hit.
69
     *
70
     * @param array|string|bool $params Fields to be returned or false to disable source
71
     *
72
     * @return $this
73
     */
74
    public function setSource($params)
75
    {
76
        return $this->setParam('_source', $params);
77
    }
78
79
    /**
80
     * Returns a version for each search hit.
81
     *
82
     * @param bool $version
83
     *
84
     * @return $this
85
     */
86
    public function setVersion($version)
87
    {
88
        return $this->setParam('version', (bool) $version);
89
    }
90
91
    /**
92
     * Enables explanation for each hit on how its score was computed.
93
     *
94
     * @param bool $explain
95
     *
96
     * @return $this
97
     */
98
    public function setExplain($explain)
99
    {
100
        return $this->setParam('explain', (bool) $explain);
101
    }
102
103
    /**
104
     * Set script fields.
105
     *
106
     * @param array|\Elastica\Script\ScriptFields $scriptFields
107
     *
108
     * @return $this
109
     */
110
    public function setScriptFields($scriptFields)
111
    {
112
        if (is_array($scriptFields)) {
113
            $scriptFields = new ScriptFields($scriptFields);
114
        }
115
116
        return $this->setParam('script_fields', $scriptFields);
117
    }
118
119
    /**
120
     * Adds a Script to the aggregation.
121
     *
122
     * @param string                          $name
123
     * @param \Elastica\Script\AbstractScript $script
124
     *
125
     * @return $this
126
     */
127 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...
128
    {
129
        if (!isset($this->_params['script_fields'])) {
130
            $this->_params['script_fields'] = new ScriptFields();
131
        }
132
133
        $this->_params['script_fields']->addScript($name, $script);
134
135
        return $this;
136
    }
137
138
    /**
139
     * Sets highlight arguments for the results.
140
     *
141
     * @param array $highlightArgs
142
     *
143
     * @return $this
144
     */
145
    public function setHighlight(array $highlightArgs)
146
    {
147
        return $this->setParam('highlight', $highlightArgs);
148
    }
149
150
    /**
151
     * Allows to return the field data representation of a field for each hit.
152
     *
153
     * @param array $fields
154
     *
155
     * @return $this
156
     */
157
    public function setFieldDataFields(array $fields)
158
    {
159
        return $this->setParam('docvalue_fields', $fields);
160
    }
161
}
162