-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathHighlight.php
More file actions
150 lines (119 loc) · 3.84 KB
/
Copy pathHighlight.php
File metadata and controls
150 lines (119 loc) · 3.84 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
declare(strict_types = 1);
namespace Spameri\ElasticQuery;
/**
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/highlighting.html
*/
class Highlight implements \Spameri\ElasticQuery\Entity\ArrayInterface
{
private \Spameri\ElasticQuery\Highlight\HighlightFieldCollection $fields;
/**
* @param array<int, string> $preTags
* @param array<int, string> $postTags
* @param \Spameri\ElasticQuery\Highlight\HighlightFieldCollection|array<int, string> $fields Either a typed collection or simple field-name list.
* @param array<int, string>|null $matchedFields
*/
public function __construct(
private array $preTags,
private array $postTags,
\Spameri\ElasticQuery\Highlight\HighlightFieldCollection|array $fields,
private string|null $type = null,
int|null $numberOfFragments = 0,
private int|null $fragmentSize = null,
private string|null $boundaryScanner = null,
private string|null $boundaryChars = null,
private int|null $boundaryMaxScan = null,
private string|null $boundaryScannerLocale = null,
private string|null $encoder = null,
private bool|null $forceSource = null,
private string|null $fragmenter = null,
private \Spameri\ElasticQuery\Query\LeafQueryInterface|null $highlightQuery = null,
private array|null $matchedFields = null,
private int|null $noMatchSize = null,
private string|null $order = null,
private int|null $phraseLimit = null,
private bool|null $requireFieldMatch = null,
private string|null $tagsSchema = null,
)
{
if ($fields instanceof \Spameri\ElasticQuery\Highlight\HighlightFieldCollection) {
$this->fields = $fields;
} else {
$this->fields = new \Spameri\ElasticQuery\Highlight\HighlightFieldCollection();
foreach ($fields as $fieldName) {
$this->fields->add(new \Spameri\ElasticQuery\Highlight\HighlightField(
field: $fieldName,
numberOfFragments: $numberOfFragments,
));
}
}
}
public function fields(): \Spameri\ElasticQuery\Highlight\HighlightFieldCollection
{
return $this->fields;
}
/**
* @return array<string, mixed>
*/
public function toArray(): array
{
$array = [
'pre_tags' => $this->preTags,
'post_tags' => $this->postTags,
];
$fieldsArray = $this->fields->toArray();
if ($fieldsArray !== []) {
$array['fields'] = $fieldsArray;
}
if ($this->type !== null) {
$array['type'] = $this->type;
}
// global number_of_fragments is mirrored into each field above; we don't emit it twice
if ($this->fragmentSize !== null) {
$array['fragment_size'] = $this->fragmentSize;
}
if ($this->boundaryScanner !== null) {
$array['boundary_scanner'] = $this->boundaryScanner;
}
if ($this->boundaryChars !== null) {
$array['boundary_chars'] = $this->boundaryChars;
}
if ($this->boundaryMaxScan !== null) {
$array['boundary_max_scan'] = $this->boundaryMaxScan;
}
if ($this->boundaryScannerLocale !== null) {
$array['boundary_scanner_locale'] = $this->boundaryScannerLocale;
}
if ($this->encoder !== null) {
$array['encoder'] = $this->encoder;
}
if ($this->forceSource !== null) {
$array['force_source'] = $this->forceSource;
}
if ($this->fragmenter !== null) {
$array['fragmenter'] = $this->fragmenter;
}
if ($this->highlightQuery !== null) {
$array['highlight_query'] = $this->highlightQuery->toArray();
}
if ($this->matchedFields !== null) {
$array['matched_fields'] = $this->matchedFields;
}
if ($this->noMatchSize !== null) {
$array['no_match_size'] = $this->noMatchSize;
}
if ($this->order !== null) {
$array['order'] = $this->order;
}
if ($this->phraseLimit !== null) {
$array['phrase_limit'] = $this->phraseLimit;
}
if ($this->requireFieldMatch !== null) {
$array['require_field_match'] = $this->requireFieldMatch;
}
if ($this->tagsSchema !== null) {
$array['tags_schema'] = $this->tagsSchema;
}
return $array;
}
}