MultiMatchQuery   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 51
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getType() 0 4 1
A toArray() 0 13 2
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\ElasticsearchDSL\Query\FullText;
13
14
use ONGR\ElasticsearchDSL\BuilderInterface;
15
use ONGR\ElasticsearchDSL\ParametersTrait;
16
17
/**
18
 * Represents Elasticsearch "multi_match" query.
19
 *
20
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html
21
 *
22
 * Allows `$fields` to be an empty array to represent 'no fields'. From the Elasticsearch documentation:
23
 *
24
 * If no fields are provided, the multi_match query defaults to the `index.query.default_field` index settings,
25
 * which in turn defaults to `*`. `*` extracts all fields in the mapping that are eligible to term queries and filters
26
 * the metadata fields. All extracted fields are then combined to build a query.
27
 */
28
class MultiMatchQuery implements BuilderInterface
29
{
30
    use ParametersTrait;
31
32
    /**
33
     * @var array
34
     */
35
    private $fields = [];
36
37
    /**
38
     * @var string
39
     */
40
    private $query;
41
42
    /**
43
     * @param array  $fields
44
     * @param string $query
45
     * @param array  $parameters
46
     */
47
    public function __construct(array $fields, $query, array $parameters = [])
48
    {
49
        $this->fields = $fields;
50
        $this->query = $query;
51
        $this->setParameters($parameters);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function getType()
58
    {
59
        return 'multi_match';
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function toArray()
66
    {
67
        $query = [
68
            'query' => $this->query,
69
        ];
70
        if (count($this->fields)) {
71
            $query['fields'] = $this->fields;
72
        }
73
74
        $output = $this->processArray($query);
75
76
        return [$this->getType() => $output];
77
    }
78
}
79