Newer
Older
<?php
/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ONGR\ElasticsearchBundle\DSL\Highlight;
use ONGR\ElasticsearchBundle\DSL\BuilderInterface;
use ONGR\ElasticsearchBundle\DSL\NamedBuilderInterface;
/**
* This class holds data for highlighting field.
*/
class Field implements NamedBuilderInterface
{
const TYPE_PLAIN = 'plain';
const TYPE_POSTINGS = 'postings';
const TYPE_FVH = 'fvh';
/**
* @var string Highlighter type. By default 'plain'.
* @var int Size of the highlighted fragment in characters. By default 100.
* @var int Maximum number of fragments to return. By default 5.
* @var array Combine matches on multiple fields to highlight a single field.
* @var array BuilderInterface query to highlight.
* @var int Show part of string even if there are no matches to highlight. Defaults to 0.
* @var bool Highlight fields based on the source.
* Creates a highlight for a field.
*
* @param string $name Field name.
*/
public function __construct($name)
{
$this->name = $name;
$this->setMatchedFields([$name]);
}
/**
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
*/
public function getName()
{
return $this->name;
}
/**
* Sets highlighter type (forces). Available options 'plain', 'postings', 'fvh'.
*
* @param string $type
*
* @return Field
*/
public function setHighlighterType($type)
{
$reflection = new \ReflectionClass(__CLASS__);
if (in_array($type, $reflection->getConstants())) {
$this->type = $type;
}
return $this;
}
/**
* Sets field fragment size.
*
* @param int $fragmentSize
*
* @return Field
*/
public function setFragmentSize($fragmentSize)
{
$this->fragmentSize = $fragmentSize;
return $this;
}
/**
* Sets maximum number of fragments to return.
*
* @param int $numberOfFragments
*
* @return Field
*/
public function setNumberOfFragments($numberOfFragments)
{
$this->numberOfFragments = $numberOfFragments;
return $this;
}
/**
* Set fields to match.
*
* @param array $matchedFields
*
* @return Field
*/
public function setMatchedFields($matchedFields)
{
$this->matchedFields = $matchedFields;
return $this;
}
/**
* Set query to highlight.
*
* @param BuilderInterface $query
*
* @return Field
*/
public function setHighlightQuery(BuilderInterface $query)
{
$this->highlightQuery = [$query->getType() => $query->toArray()];
return $this;
}
/**
* Shows set length of a string even if no matches found.
*
* @param int $noMatchSize
*
* @return Field
*/
public function setNoMatchSize($noMatchSize)
{
$this->noMatchSize = $noMatchSize;
return $this;
}
/**
* Set to force highlighting from source.
*
* @param bool $forceSource
*
* @return Field
*/
public function setForceSource($forceSource)
{
$this->forceSource = $forceSource;
return $this;
}
/**
*/
public function toArray()
{
return array_filter(
[
'fragment_size' => $this->fragmentSize,
'number_of_fragments' => $this->numberOfFragments,
'type' => $this->type,
'matched_fields' => $this->matchedFields,
'highlight_query' => $this->highlightQuery,
'no_match_size' => $this->noMatchSize,
'force_source' => $this->forceSource,
]
);
}
/**
* {@inheritdoc}
*/
public function getType()
{
return $this->type;
}