Completed
Pull Request — develop (#22)
by Sam
02:00
created

AbstractQuery::getScoreMode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php namespace Nord\Lumen\Elasticsearch\Search\Query\Joining;
2
3
use Nord\Lumen\Elasticsearch\Exceptions\InvalidArgument;
4
use Nord\Lumen\Elasticsearch\Search\Query\QueryDSL;
5
6
/**
7
 * Performing full SQL-style joins in a distributed system like Elasticsearch is prohibitively expensive.
8
 * Instead, Elasticsearch offers two forms of join which are designed to scale horizontally.
9
 *
10
 * - "nested" query
11
 * Documents may contains fields of type nested. These fields are used to index arrays of objects, where each object can
12
 * be queried (with the nested query) as an independent document.
13
 *
14
 * - "has_child" and "has_parent" queries
15
 * A parent-child relationship can exist between two document types within a single index. The has_child query returns
16
 * parent documents whose child documents match the specified query, while the has_parent query returns child documents
17
 * whose parent document matches the specified query.
18
 *
19
 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/joining-queries.html
20
 */
21
abstract class AbstractQuery extends QueryDSL
22
{
23
    const SCORE_MODE_AVG   = 'avg';
24
    const SCORE_MODE_SUM   = 'sum';
25
    const SCORE_MODE_MIN   = 'min';
26
    const SCORE_MODE_MAX   = 'max';
27
    const SCORE_MODE_SCORE = 'score';
28
    const SCORE_MODE_NONE  = 'none';
29
30
    /**
31
     * @var string
32
     */
33
    protected $scoreMode;
34
35
    /**
36
     * @return array
37
     */
38
    abstract protected function getValidScoreModes();
39
40
    /**
41
     * @return string
42
     */
43
    public function getScoreMode()
44
    {
45
        return $this->scoreMode;
46
    }
47
48
    /**
49
     * @param string $scoreMode
50
     *
51
     * @return $this
52
     */
53
    public function setScoreMode($scoreMode)
54
    {
55
        $this->assertScoreMode($scoreMode);
56
        $this->scoreMode = $scoreMode;
57
58
        return $this;
59
    }
60
61
    /**
62
     * @param string $scoreMode
63
     *
64
     * @throws InvalidArgument
65
     */
66
    protected function assertScoreMode($scoreMode)
67
    {
68
        $validModes = $this->getValidScoreModes();
69
        
70
        if (!in_array($scoreMode, $validModes)) {
71
            throw new InvalidArgument(sprintf(
72
                '`score_mode` must be one of "%s", "%s" given.',
73
                implode(', ', $validModes),
74
                $scoreMode
75
            ));
76
        }
77
    }
78
}
79