1<?php
2
3namespace Elastica;
4
5use Elastica\Collapse\InnerHits;
6
7/**
8 * Implementation of field collapse.
9 *
10 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-collapse
11 */
12class Collapse extends Param
13{
14    /**
15     * Set field to collapse.
16     */
17    public function setFieldname(string $fieldName): self
18    {
19        return $this->setParam('field', $fieldName);
20    }
21
22    /**
23     * Set inner hits for collapsed field.
24     */
25    public function setInnerHits(InnerHits $innerHits): self
26    {
27        return $this->setParam('inner_hits', $innerHits);
28    }
29
30    public function addInnerHits(InnerHits $innerHits): self
31    {
32        $hits = [];
33
34        if ($this->hasParam('inner_hits')) {
35            $existingInnerHits = $this->getParam('inner_hits');
36
37            $hits = $existingInnerHits instanceof InnerHits ? [$existingInnerHits] : $existingInnerHits;
38        }
39
40        $hits[] = $innerHits;
41
42        return $this->setParam('inner_hits', $hits);
43    }
44
45    public function setMaxConcurrentGroupSearches(int $groupSearches): self
46    {
47        return $this->setParam('max_concurrent_group_searches', $groupSearches);
48    }
49
50    public function toArray(): array
51    {
52        $data = $this->getParams();
53
54        if ($this->_rawParams) {
55            $data = \array_merge($data, $this->_rawParams);
56        }
57
58        return $this->_convertArrayable($data);
59    }
60}
61