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\NamedBuilderBag;
use ONGR\ElasticsearchBundle\DSL\NamedBuilderInterface;
class Highlight extends NamedBuilderBag implements BuilderInterface
{
const TYPE_PLAIN = 'plain';
const TYPE_POSTINGS = 'postings';
const TYPE_FVH = 'fvh';
/**
* @var array Holds html tag name and class that highlight will be put in (default 'em' tag).
* @var string Holds tag schema name. 'styled' is the only option yet.
* @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.
private $numberOfFragments = null;
public function add(NamedBuilderInterface $builder)
public function set(array $builders)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
return $this;
}
/**
* Sets html tag and its class used in highlighting.
*
* @param string $tag
* @param string $class
*
* @return Highlight
*/
public function setTag($tag, $class = null)
{
$this->tags[] = array_filter(
[
'tag' => $tag,
'class' => $class,
]
);
return $this;
}
/**
* Sets html tag and its class used in highlighting.
*
* @param string $tagsSchema
*
* @return Highlight
*/
public function setTagsSchema($tagsSchema)
{
$this->tagsSchema = $tagsSchema;
return $this;
}
/**
* Sets fragments sort order.
*
* @param string $order
*
* @return Highlight
*/
public function setOrder($order)
{
$this->order = $order;
return $this;
}
/**
* Sets highlighter type (forces). Available options plain, postings, fvh.
*
* @param string $type
*
* @return Highlight
*/
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 Highlight
*/
public function setFragmentSize($fragmentSize)
{
$this->fragmentSize = $fragmentSize;
return $this;
}
/**
* Sets maximum number of fragments to return.
*
* @param int $numberOfFragments
*
* @return Highlight
*/
public function setNumberOfFragments($numberOfFragments)
{
$this->numberOfFragments = $numberOfFragments;
return $this;
}
/**
* {@inheritdoc}
*/
public function getType()
{
return 'highlight';
}
/**
* {@inheritdoc}
*/
public function toArray()
{
$highlight = array_filter(
[
'order' => $this->order,
'type' => $this->type,
'fragment_size' => $this->fragmentSize,
'number_of_fragments' => $this->numberOfFragments,
'tags_schema' => $this->tagsSchema,
'fields' => $this->getFields(),
]
);
foreach ($this->tags as $tag) {
if (isset($tag['tag'])) {
$highlight['post_tags'][] = sprintf('</%s>', $tag['tag']);
if (isset($tag['class'])) {
$highlight['pre_tags'][] = sprintf('<%s class="%s">', $tag['tag'], $tag['class']);
} else {
$highlight['pre_tags'][] = sprintf('<%s>', $tag['tag']);
}
}
}
return $highlight;
}
/**
* Returns fields as array.
*
* @return array
*/
private function getFields()
{
$out = [];
foreach ($this->all() as $builder) {
$out = array_merge($out, [$builder->getName() => $builder->toArray()]);