Passed
Pull Request — develop (#82)
by Sam
01:59
created

DisMaxQuery::toArray()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 4
nop 0
1
<?php
2
3
namespace Nord\Lumen\Elasticsearch\Search\Query\Compound;
4
5
use Nord\Lumen\Elasticsearch\Search\Query\QueryDSL;
6
use Nord\Lumen\Elasticsearch\Search\Query\Traits\HasBoost;
7
use Nord\Lumen\Elasticsearch\Search\Traits\HasTieBreaker;
8
9
/**
10
 * Class DisMaxQuery
11
 *
12
 * A query that generates the union of documents produced by its subqueries, and that scores each document with the
13
 * maximum score for that document as produced by any subquery, plus a tie breaking increment for any additional
14
 * matching subqueries.
15
 *
16
 * @package Nord\Lumen\Elasticsearch\Search\Query\Compound
17
 *
18
 * @see     https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-dis-max-query.html
19
 */
20
class DisMaxQuery extends AbstractQuery
21
{
22
23
    use HasBoost;
24
    use HasTieBreaker;
25
26
    /**
27
     * @var QueryDSL[]
28
     */
29
    private $queries = [];
30
31
    /**
32
     * @return QueryDSL[]
33
     */
34
    public function getQueries()
35
    {
36
        return $this->queries;
37
    }
38
39
    /**
40
     * @param QueryDSL[] $queries
41
     *
42
     * @return $this
43
     */
44
    public function setQueries($queries)
45
    {
46
        $this->queries = $queries;
47
48
        return $this;
49
    }
50
51
    /**
52
     * @param QueryDSL $query
53
     *
54
     * @return $this
55
     */
56
    public function addQuery(QueryDSL $query)
57
    {
58
        $this->queries[] = $query;
59
60
        return $this;
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66
    public function toArray()
67
    {
68
        $body = [
69
            'queries' => array_map(function (QueryDSL $query) {
70
                return $query->toArray();
71
            }, $this->queries),
72
        ];
73
74
        if ($this->tieBreaker !== null) {
75
            $body['tie_breaker'] = $this->tieBreaker;
76
        }
77
78
        if ($this->hasBoost()) {
79
            $body['boost'] = $this->getBoost();
80
        }
81
82
        return $body;
83
    }
84
}
85