Completed
Push — develop ( 5325d9...9f3a91 )
by Sam
11s
created

FunctionScoreQuery::setFunctions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php namespace Nord\Lumen\Elasticsearch\Search\Query\Compound;
2
3
use Nord\Lumen\Elasticsearch\Search\Query\ScoreMode;
4
use Nord\Lumen\Elasticsearch\Search\Query\Traits\HasQuery;
5
use Nord\Lumen\Elasticsearch\Search\Query\Traits\HasScoreMode;
6
use Nord\Lumen\Elasticsearch\Search\Scoring\Functions\AbstractScoringFunction;
7
8
/**
9
 * The function_score allows you to modify the score of documents that are retrieved by a query. This can be useful if,
10
 * for example, a score function is computationally expensive and it is sufficient to compute the score on a filtered
11
 * set of documents.
12
 *
13
 * To use function_score, the user has to define a query and one or more functions, that compute a new score for each
14
 * document returned by the query.
15
 *
16
 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html
17
 */
18
class FunctionScoreQuery extends AbstractQuery
19
{
20
    use HasQuery;
21
    use HasScoreMode;
22
23
    /**
24
     * @var AbstractScoringFunction[]
25
     */
26
    private $functions = [];
27
28
    /**
29
     * @inheritdoc
30
     */
31
    public function toArray()
32
    {
33
        $array = [];
34
35
        $query = $this->getQuery();
36
        if (!empty($query)) {
37
            $queryArray = $query->toArray();
38
            if (!empty($queryArray)) {
39
                $array['query'] = $queryArray;
40
            } else {
41
                $array['query'] = ['match_all' => []];
42
            }
43
        }
44
45
        $functions = [];
46
        foreach ($this->getFunctions() as $function) {
47
            $functions[] = $function->toArray();
48
        }
49
50
        if (!empty($functions)) {
51
            $array['functions'] = $functions;
52
        }
53
54
        $scoreMode = $this->getScoreMode();
55
        if (!empty($scoreMode)) {
56
            $array['score_mode'] = $scoreMode;
57
        }
58
59
        return ['function_score' => $array];
60
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65 View Code Duplication
    protected function getValidScoreModes()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
    {
67
        return [
68
            ScoreMode::MODE_MULTIPLY,
69
            ScoreMode::MODE_SUM,
70
            ScoreMode::MODE_AVG,
71
            ScoreMode::MODE_FIRST,
72
            ScoreMode::MODE_MAX,
73
            ScoreMode::MODE_MIN
74
        ];
75
    }
76
77
    /**
78
     * @return AbstractScoringFunction[]
79
     */
80
    public function getFunctions()
81
    {
82
        return $this->functions;
83
    }
84
85
86
    /**
87
     * @param AbstractScoringFunction[] $functions
88
     * @return FunctionScoreQuery
89
     */
90
    public function setFunctions(array $functions)
91
    {
92
        $this->functions = [];
93
        foreach ($functions as $function) {
94
            $this->addFunction($function);
95
        }
96
        return $this;
97
    }
98
99
100
    /**
101
     * @param AbstractScoringFunction $function
102
     * @return FunctionScoreQuery
103
     */
104
    public function addFunction(AbstractScoringFunction $function)
105
    {
106
        $this->functions[] = $function;
107
        return $this;
108
    }
109
}
110