-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMoreLikeThis.php
More file actions
78 lines (63 loc) · 1.7 KB
/
MoreLikeThis.php
File metadata and controls
78 lines (63 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
declare(strict_types = 1);
namespace Spameri\ElasticQuery\Query;
/**
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-mlt-query.html
*/
class MoreLikeThis implements \Spameri\ElasticQuery\Query\LeafQueryInterface
{
/**
* @param array<int, string> $fields
* @param array<int, string|array<string, mixed>> $like Texts or doc refs (['_index' => ..., '_id' => ...]).
* @param array<int, string|array<string, mixed>> $unlike
*/
public function __construct(
private array $fields,
private array $like,
private array $unlike = [],
private int|null $minTermFreq = null,
private int|null $maxQueryTerms = null,
private int|string|null $minimumShouldMatch = null,
)
{
if ($fields === []) {
throw new \Spameri\ElasticQuery\Exception\InvalidArgumentException(
'MoreLikeThis query requires at least one field.',
);
}
if ($like === []) {
throw new \Spameri\ElasticQuery\Exception\InvalidArgumentException(
'MoreLikeThis query requires at least one like value.',
);
}
}
public function key(): string
{
return 'more_like_this_' . \implode('-', $this->fields);
}
/**
* @return array<string, array<string, mixed>>
*/
public function toArray(): array
{
$body = [
'fields' => $this->fields,
'like' => $this->like,
];
if ($this->unlike !== []) {
$body['unlike'] = $this->unlike;
}
if ($this->minTermFreq !== null) {
$body['min_term_freq'] = $this->minTermFreq;
}
if ($this->maxQueryTerms !== null) {
$body['max_query_terms'] = $this->maxQueryTerms;
}
if ($this->minimumShouldMatch !== null) {
$body['minimum_should_match'] = $this->minimumShouldMatch;
}
return [
'more_like_this' => $body,
];
}
}