TermQuery   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 12
c 1
b 0
f 0
dl 0
loc 35
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 3
A toArray() 0 14 4
1
<?php namespace Nord\Lumen\Elasticsearch\Search\Query\TermLevel;
2
3
use Nord\Lumen\Elasticsearch\Search\Query\Traits\HasBoost;
4
use Nord\Lumen\Elasticsearch\Search\Query\Traits\HasValue;
5
6
/**
7
 * The term query finds documents that contain the exact term specified in the inverted index.
8
 *
9
 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html
10
 */
11
class TermQuery extends AbstractQuery
12
{
13
    use HasBoost;
14
    use HasValue;
15
16
    /**
17
     * TermQuery constructor.
18
     *
19
     * @param string|null $field
20
     * @param mixed|null  $value
21
     */
22
    public function __construct(?string $field = null, $value = null)
23
    {
24
        if ($field !== null && $value !== null) {
25
            $this->setField($field)->setValue($value);
26
        }
27
    }
28
29
    /**
30
     * @inheritdoc
31
     */
32
    public function toArray()
33
    {
34
        $term = ['value' => $this->getValue()];
35
36
        $boost = $this->getBoost();
37
        if (null !== $boost) {
38
            $term['boost'] = $boost;
39
        }
40
41
        if (count($term) === 1 && isset($term['value'])) {
42
            $term = $term['value'];
43
        }
44
45
        return ['term' => [$this->getField() => $term]];
46
    }
47
}
48