BoolQuery::addFilters()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
c 0
b 0
f 0
rs 10
cc 3
nc 3
nop 1
1
<?php namespace Nord\Lumen\Elasticsearch\Search\Query\Compound;
2
3
use Nord\Lumen\Elasticsearch\Search\Query\QueryDSL;
4
5
/**
6
 * A query that matches documents matching boolean combinations of other queries.
7
 *
8
 * The bool query maps to Lucene BooleanQuery. It is built using one or more boolean clauses,
9
 * each clause with a typed occurrence. The occurrence types are:
10
 *
11
 * - "must"
12
 * The clause (query) must appear in matching documents and will contribute to the score.
13
 *
14
 * - "filter"
15
 * The clause (query) must appear in matching documents. However unlike must the score of the query will be ignored.
16
 *
17
 * - "should"
18
 * The clause (query) should appear in the matching document. In a boolean query with no must or filter clauses, one or
19
 * more should clauses must match a document. The minimum number of should clauses to match can be set using the
20
 * minimum_should_match parameter.
21
 *
22
 * - "must_not"
23
 * The clause (query) must not appear in the matching documents.
24
 *
25
 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html
26
 */
27
class BoolQuery extends AbstractQuery
28
{
29
    /**
30
     * @var QueryDSL[] The clause (query) must appear in matching documents and will contribute to the score.
31
     */
32
    private $must = [];
33
34
    /**
35
     * @var QueryDSL[] The clause (query) must appear in matching documents.
36
     * However unlike must the score of the query will be ignored.
37
     */
38
    private $filter = [];
39
40
    /**
41
     * @var QueryDSL[] The clause (query) should appear in the matching document.
42
     * In a boolean query with no must or filter clauses, one or more should clauses must match a document.
43
     * The minimum number of should clauses to match can be set using the minimum_should_match parameter.
44
     */
45
    private $should = [];
46
47
    /**
48
     * @var QueryDSL[] The clause (query) must not appear in the matching documents.
49
     */
50
    private $mustNot = [];
51
52
53
    /**
54
     * @param QueryDSL $query
55
     * @return BoolQuery
56
     */
57
    public function addMust(QueryDSL $query)
58
    {
59
        $this->must[] = $query;
60
        return $this;
61
    }
62
63
64
    /**
65
     * @param QueryDSL $query
66
     * @return BoolQuery
67
     */
68
    public function addFilter(QueryDSL $query)
69
    {
70
        $this->filter[] = $query;
71
        return $this;
72
    }
73
74
    /**
75
     * @param array $filters
76
     *
77
     * @return BoolQuery
78
     */
79
    public function addFilters(array $filters)
80
    {
81
        foreach ($filters as $filter) {
82
            if ($filter instanceof QueryDSL) {
83
                $this->addFilter($filter);
84
            }
85
        }
86
        return $this;
87
    }
88
89
90
    /**
91
     * @param QueryDSL $query
92
     * @return BoolQuery
93
     */
94
    public function addShould(QueryDSL $query)
95
    {
96
        $this->should[] = $query;
97
        return $this;
98
    }
99
100
101
    /**
102
     * @param QueryDSL $query
103
     * @return BoolQuery
104
     */
105
    public function addMustNot(QueryDSL $query)
106
    {
107
        $this->mustNot[] = $query;
108
        return $this;
109
    }
110
111
112
    /**
113
     * @inheritdoc
114
     */
115
    public function toArray()
116
    {
117
        $array = [];
118
119
        $fields = [
120
            'must'    => 'must',
121
            'filter'  => 'filter',
122
            'should'  => 'should',
123
            'mustNot' => 'must_not',
124
        ];
125
126
        foreach ($fields as $propertyName => $fieldName) {
127
            $property = $this->{$propertyName};
128
129
            if (!empty($property)) {
130
                $array['bool'][$fieldName] = [];
131
                foreach ($property as $query) {
132
                    /** @var QueryDSL $query */
133
                    $array['bool'][$fieldName][] = $query->toArray();
134
                }
135
            }
136
        }
137
138
        return $array;
139
    }
140
}
141