CompoundQueries   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 72
rs 10
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A boosting() 0 5 1
A get_boosting() 0 5 2
A constant_score() 0 5 1
A jsonSerializeCompoundQueries() 0 7 1
A dis_max() 0 9 2
A get_dis_max() 0 5 2
A get_constant_score() 0 5 2
1
<?php
2
3
namespace olvlvl\ElasticsearchDSL\Query;
4
5
use olvlvl\ElasticsearchDSL\Helpers;
6
use olvlvl\ElasticsearchDSL\Query\Compound\BoostingQuery;
7
use olvlvl\ElasticsearchDSL\Query\Compound\ConstantScoreQuery;
8
use olvlvl\ElasticsearchDSL\Query\Compound\DisMaxQuery;
9
10
trait CompoundQueries
11
{
12
	use BoolQueries;
13
14
	/**
15
	 * @var BoostingQuery[]
16
	 */
17
	private $boosting_queries = [];
18
19
	protected function get_boosting()
20
	{
21
		$query = reset($this->boosting_queries);
22
23
		return $query ? $query : $this->boosting();
24
	}
25
26
	public function boosting(float $negative_boost = .5): BoostingQuery
27
	{
28
		$this->boosting_queries[] = $query = new BoostingQuery($negative_boost);
29
30
		return $query;
31
	}
32
33
	/**
34
	 * @var ConstantScoreQuery[]
35
	 */
36
	private $constant_score_queries = [];
37
38
	protected function get_constant_score()
39
	{
40
		$query = reset($this->constant_score_queries);
41
42
		return $query ? $query : $this->constant_score();
43
	}
44
45
	public function constant_score(float $score = 1.0): ConstantScoreQuery
46
	{
47
		$this->constant_score_queries[] = $query = new ConstantScoreQuery($score);
48
49
		return $query;
50
	}
51
52
	/**
53
	 * @var DisMaxQuery[]
54
	 */
55
	private $dis_max_queries = [];
56
57
	protected function get_dis_max()
58
	{
59
		$query = reset($this->dis_max_queries);
60
61
		return $query ? $query : $this->dis_max();
62
	}
63
64
	public function dis_max(callable $config = null): DisMaxQuery
65
	{
66
		$this->dis_max_queries[] = $q = new DisMaxQuery;
67
68
		if ($config) {
69
			$config($q);
70
		}
71
72
		return $q;
73
	}
74
75
	protected function jsonSerializeCompoundQueries(): array
76
	{
77
		return Helpers::filter_merge(
78
			$this->bool_queries,
79
			$this->boosting_queries,
80
			$this->constant_score_queries,
81
			$this->dis_max_queries
82
		);
83
	}
84
}
85