Skip to content

Commit 3a92e0a

Browse files
committed
Added ability to use function score and highlight
1 parent fa4f3bb commit 3a92e0a

8 files changed

Lines changed: 426 additions & 31 deletions

File tree

src/ElasticQuery.php

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,60 +6,54 @@
66
class ElasticQuery implements \Spameri\ElasticQuery\Entity\ArrayInterface
77
{
88

9-
/**
10-
* @var \Spameri\ElasticQuery\Query\QueryCollection
11-
*/
12-
private $query;
9+
private \Spameri\ElasticQuery\Query\QueryCollection $query;
1310

14-
/**
15-
* @var \Spameri\ElasticQuery\Filter\FilterCollection
16-
*/
17-
private $filter;
11+
private \Spameri\ElasticQuery\Filter\FilterCollection $filter;
1812

19-
/**
20-
* @var \Spameri\ElasticQuery\Options\SortCollection
21-
*/
22-
private $sort;
13+
private \Spameri\ElasticQuery\Options\SortCollection $sort;
2314

24-
/**
25-
* @var \Spameri\ElasticQuery\Aggregation\AggregationCollection
26-
*/
27-
private $aggregation;
15+
private \Spameri\ElasticQuery\Aggregation\AggregationCollection $aggregation;
2816

29-
/**
30-
* @var \Spameri\ElasticQuery\Options
31-
*/
32-
private $options;
17+
private \Spameri\ElasticQuery\Options $options;
18+
19+
private ?\Spameri\ElasticQuery\Highlight $highlight;
20+
21+
private ?\Spameri\ElasticQuery\FunctionScore $functionScore;
3322

3423

3524
public function __construct(
36-
?\Spameri\ElasticQuery\Query\QueryCollection $query = NULL
37-
, ?\Spameri\ElasticQuery\Filter\FilterCollection $filter = NULL
38-
, ?\Spameri\ElasticQuery\Options\SortCollection $sort = NULL
39-
, ?\Spameri\ElasticQuery\Aggregation\AggregationCollection $aggregation = NULL
40-
, ?Options $options = NULL
25+
?\Spameri\ElasticQuery\Query\QueryCollection $query = NULL,
26+
?\Spameri\ElasticQuery\Filter\FilterCollection $filter = NULL,
27+
?\Spameri\ElasticQuery\Options\SortCollection $sort = NULL,
28+
?\Spameri\ElasticQuery\Aggregation\AggregationCollection $aggregation = NULL,
29+
?\Spameri\ElasticQuery\Highlight $highlight = NULL,
30+
?\Spameri\ElasticQuery\FunctionScore $functionScore = NULL,
31+
?\Spameri\ElasticQuery\Options $options = NULL
4132
)
4233
{
43-
if ( ! $query) {
34+
if ($query === NULL) {
4435
$query = new \Spameri\ElasticQuery\Query\QueryCollection();
4536
}
46-
if ( ! $filter) {
37+
if ($filter === NULL) {
4738
$filter = new \Spameri\ElasticQuery\Filter\FilterCollection();
4839
}
49-
if ( ! $sort) {
40+
if ($sort === NULL) {
5041
$sort = new \Spameri\ElasticQuery\Options\SortCollection();
5142
}
52-
if ( ! $aggregation) {
43+
if ($aggregation === NULL) {
5344
$aggregation = new \Spameri\ElasticQuery\Aggregation\AggregationCollection();
5445
}
55-
if ( ! $options) {
56-
$options = new Options();
46+
if ($options === NULL) {
47+
$options = new \Spameri\ElasticQuery\Options();
5748
}
49+
5850
$this->query = $query;
5951
$this->filter = $filter;
6052
$this->sort = $sort;
6153
$this->aggregation = $aggregation;
6254
$this->options = $options;
55+
$this->highlight = $highlight;
56+
$this->functionScore = $functionScore;
6357
}
6458

6559

@@ -87,6 +81,18 @@ public function options(): \Spameri\ElasticQuery\Options
8781
}
8882

8983

84+
public function highlight(): ?\Spameri\ElasticQuery\Highlight
85+
{
86+
return $this->highlight;
87+
}
88+
89+
90+
public function functionScore(): ?\Spameri\ElasticQuery\FunctionScore
91+
{
92+
return $this->functionScore;
93+
}
94+
95+
9096
public function addMustQuery(\Spameri\ElasticQuery\Query\LeafQueryInterface $leafQuery): void
9197
{
9298
$this->query->must()->add($leafQuery);
@@ -126,6 +132,10 @@ public function toArray(): array
126132
$array['query'] = $queryArray;
127133
}
128134

135+
if ($this->functionScore !== NULL) {
136+
$array['query'] = $this->functionScore->toArray($array['query']);
137+
}
138+
129139
$filterArray = $this->filter->toArray();
130140
if ($filterArray) {
131141
$array['filter'] = $filterArray;
@@ -141,6 +151,10 @@ public function toArray(): array
141151
$array['aggs'] = $aggregation;
142152
}
143153

154+
if ($this->highlight !== NULL) {
155+
$array['highlight'] = $this->highlight->toArray();
156+
}
157+
144158
return $array;
145159
}
146160

src/FunctionScore.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\ElasticQuery;
4+
5+
class FunctionScore
6+
{
7+
8+
public const SCORE_MODE_MULTIPLY = 'multiply';
9+
public const SCORE_MODE_SUM = 'sum';
10+
public const SCORE_MODE_AVG = 'avg';
11+
public const SCORE_MODE_FIRST = 'first';
12+
public const SCORE_MODE_MAX = 'max';
13+
public const SCORE_MODE_MIN = 'min';
14+
15+
private \Spameri\ElasticQuery\FunctionScore\FunctionScoreCollection $function;
16+
17+
private ?string $scoreMode;
18+
19+
20+
public function __construct(
21+
?\Spameri\ElasticQuery\FunctionScore\FunctionScoreCollection $function = NULL,
22+
?string $scoreMode = NULL
23+
) {
24+
$this->function = $function ?? new \Spameri\ElasticQuery\FunctionScore\FunctionScoreCollection();
25+
$this->scoreMode = $scoreMode;
26+
}
27+
28+
29+
public function function(): \Spameri\ElasticQuery\FunctionScore\FunctionScoreCollection
30+
{
31+
return $this->function;
32+
}
33+
34+
35+
public function scoreMode(): ?string
36+
{
37+
return $this->scoreMode;
38+
}
39+
40+
41+
public function toArray(array $queryPart): array
42+
{
43+
$functions = [];
44+
foreach ($this->function() as $function) {
45+
$functions[] = $function->toArray();
46+
}
47+
48+
$array = [
49+
'function_score' => [
50+
'query' => $queryPart,
51+
'functions' => $functions,
52+
],
53+
];
54+
55+
if ($this->scoreMode !== NULL) {
56+
$array['function_score']['score_mode'] = $this->scoreMode;
57+
}
58+
59+
return $array;
60+
}
61+
62+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\ElasticQuery\FunctionScore;
4+
5+
class FunctionScoreCollection implements \Spameri\ElasticQuery\Collection\SimpleCollectionInterface
6+
{
7+
8+
/**
9+
* @var array<\Spameri\ElasticQuery\FunctionScore\FunctionScoreInterface>
10+
*/
11+
private array $collection;
12+
13+
14+
public function __construct(
15+
\Spameri\ElasticQuery\FunctionScore\FunctionScoreInterface ... $collection
16+
)
17+
{
18+
$this->collection = [];
19+
foreach ($collection as $item) {
20+
$this->add($item);
21+
}
22+
}
23+
24+
25+
/**
26+
* @param \Spameri\ElasticQuery\FunctionScore\FunctionScoreInterface $item
27+
*/
28+
public function add(
29+
$item
30+
): void
31+
{
32+
$this->collection[$item->key()] = $item;
33+
}
34+
35+
36+
public function remove(
37+
string $key
38+
): bool
39+
{
40+
if (isset($this->collection[$key])) {
41+
unset($this->collection[$key]);
42+
43+
return TRUE;
44+
}
45+
46+
return FALSE;
47+
}
48+
49+
50+
public function get(
51+
string $key
52+
): ?\Spameri\ElasticQuery\FunctionScore\FunctionScoreInterface
53+
{
54+
return $this->collection[$key] ?? NULL;
55+
}
56+
57+
58+
public function isValue(
59+
string $key
60+
): bool
61+
{
62+
if (isset($this->collection[$key])) {
63+
return TRUE;
64+
}
65+
66+
return FALSE;
67+
}
68+
69+
70+
public function count(): int
71+
{
72+
return \count($this->collection);
73+
}
74+
75+
76+
public function keys(): array
77+
{
78+
return \array_map('\strval', \array_keys($this->collection));
79+
}
80+
81+
82+
public function clear(): void
83+
{
84+
$this->collection = [];
85+
}
86+
87+
88+
public function getIterator(): \ArrayIterator
89+
{
90+
return new \ArrayIterator($this->collection);
91+
}
92+
93+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\ElasticQuery\FunctionScore;
4+
5+
interface FunctionScoreInterface
6+
extends
7+
\Spameri\ElasticQuery\Entity\ArrayInterface,
8+
\Spameri\ElasticQuery\Collection\Item
9+
{
10+
11+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\ElasticQuery\FunctionScore\ScoreFunction;
4+
5+
class FieldValueFactor implements \Spameri\ElasticQuery\FunctionScore\FunctionScoreInterface
6+
{
7+
8+
public const MODIFIER_NONE = 'none';
9+
public const MODIFIER_LOG = 'log';
10+
public const MODIFIER_LOG1P = 'log1p';
11+
public const MODIFIER_LOG2P = 'log2p';
12+
public const MODIFIER_LN = 'ln';
13+
public const MODIFIER_LN1P = 'ln1p';
14+
public const MODIFIER_LN2P = 'ln2p';
15+
public const MODIFIER_SQUARE = 'square';
16+
public const MODIFIER_SQRT = 'sqrt';
17+
public const MODIFIER_RECIPROCAL = 'reciprocal';
18+
19+
private string $field;
20+
21+
private float $factor;
22+
23+
private string $modifier;
24+
25+
private float $missing;
26+
27+
28+
public function __construct(
29+
string $field,
30+
float $factor = 1.0,
31+
string $modifier = self::MODIFIER_NONE,
32+
float $missing = 1.0
33+
) {
34+
$this->field = $field;
35+
$this->factor = $factor;
36+
$this->modifier = $modifier;
37+
$this->missing = $missing;
38+
}
39+
40+
41+
public function key(): string
42+
{
43+
return 'field_value_factor_' . $this->field;
44+
}
45+
46+
47+
public function field(): string
48+
{
49+
return $this->field;
50+
}
51+
52+
53+
public function factor(): float
54+
{
55+
return $this->factor;
56+
}
57+
58+
59+
public function modifier(): string
60+
{
61+
return $this->modifier;
62+
}
63+
64+
65+
public function missing(): float
66+
{
67+
return $this->missing;
68+
}
69+
70+
71+
public function toArray(): array
72+
{
73+
return [
74+
'field_value_factor' => [
75+
'field' => $this->field,
76+
'factor' => $this->factor,
77+
'modifier' => $this->modifier,
78+
'missing' => $this->missing,
79+
],
80+
];
81+
}
82+
83+
}

0 commit comments

Comments
 (0)