Passed
Push — develop ( f2149c...9e9b3a )
by Sam
01:50 queued 17s
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
    use HasBoost;
23
    use HasTieBreaker;
24
25
    /**
26
     * @var QueryDSL[]
27
     */
28
    private $queries = [];
29
30
    /**
31
     * @return QueryDSL[]
32
     */
33
    public function getQueries()
34
    {
35
        return $this->queries;
36
    }
37
38
    /**
39
     * @param QueryDSL[] $queries
40
     *
41
     * @return $this
42
     */
43
    public function setQueries($queries)
44
    {
45
        $this->queries = $queries;
46
47
        return $this;
48
    }
49
50
    /**
51
     * @param QueryDSL $query
52
     *
53
     * @return $this
54
     */
55
    public function addQuery(QueryDSL $query)
56
    {
57
        $this->queries[] = $query;
58
59
        return $this;
60
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65
    public function toArray()
66
    {
67
        $body = [
68
            'queries' => array_map(function (QueryDSL $query) {
69
                return $query->toArray();
70
            }, $this->getQueries()),
71
        ];
72
73
        if ($this->tieBreaker !== null) {
74
            $body['tie_breaker'] = $this->tieBreaker;
75
        }
76
77
        if ($this->hasBoost()) {
78
            $body['boost'] = $this->getBoost();
79
        }
80
81
        return ['dis_max' => $body];
82
    }
83
}
84