Skip to content
Snippets Groups Projects
Commit b6cf5eb6 authored by Simonas Šerlinskas's avatar Simonas Šerlinskas
Browse files

simplifying highlight functionality

parent c0ec9f99
No related branches found
No related tags found
No related merge requests found
<?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\ElasticsearchDSL\Highlight;
use ONGR\ElasticsearchDSL\BuilderInterface;
use ONGR\ElasticsearchDSL\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 Field name.
*/
private $name;
/**
* @var string Highlighter type. By default 'plain'.
*/
private $type;
/**
* @var int Size of the highlighted fragment in characters. By default 100.
*/
private $fragmentSize;
/**
* @var int Maximum number of fragments to return. By default 5.
*/
private $numberOfFragments;
/**
* @var array Combine matches on multiple fields to highlight a single field.
*/
private $matchedFields;
/**
* @var array BuilderInterface query to highlight.
*/
private $highlightQuery;
/**
* @var int Show part of string even if there are no matches to highlight. Defaults to 0.
*/
private $noMatchSize;
/**
* @var bool Highlight fields based on the source.
*/
private $forceSource;
/**
* Creates a highlight for a field.
*
* @param string $name Field name.
*/
public function __construct($name)
{
$this->name = $name;
$this->setMatchedFields([$name]);
}
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*/
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;
}
}
...@@ -12,68 +12,34 @@ ...@@ -12,68 +12,34 @@
namespace ONGR\ElasticsearchDSL\Highlight; namespace ONGR\ElasticsearchDSL\Highlight;
use ONGR\ElasticsearchDSL\BuilderInterface; use ONGR\ElasticsearchDSL\BuilderInterface;
use ONGR\ElasticsearchDSL\NamedBuilderBag; use ONGR\ElasticsearchDSL\ParametersTrait;
use ONGR\ElasticsearchDSL\NamedBuilderInterface;
/** /**
* Data holder for highlight api. * Data holder for highlight api.
*/ */
class Highlight extends NamedBuilderBag implements BuilderInterface class Highlight implements BuilderInterface
{ {
const TYPE_PLAIN = 'plain'; use ParametersTrait;
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 array Holds fields for highlight.
*/ */
private $tags = []; private $fields = [];
/** /**
* @var string Holds tag schema name. 'styled' is the only option yet. * @var array
*/ */
private $tagsSchema = null; private $tags;
/** /**
* @var string Fragments sort type. * @param $name
*/ * @param array $params
private $order = null;
/**
* @var string Highlighter type. By default plain.
*/
private $type = null;
/**
* @var int Size of the highlighted fragment in characters. By default 100.
*/
private $fragmentSize = null;
/**
* @var int Maximum number of fragments to return. By default 5.
*/
private $numberOfFragments = null;
/**
* {@inheritdoc}
*
* @return Highlight
*/
public function add(NamedBuilderInterface $builder)
{
parent::add($builder);
return $this;
}
/**
* {@inheritdoc}
* *
* @return Highlight * @return $this
*/ */
public function set(array $builders) public function addField($name, array $params = [])
{ {
parent::set($builders); $this->fields[$name] = $params;
return $this; return $this;
} }
...@@ -81,92 +47,15 @@ class Highlight extends NamedBuilderBag implements BuilderInterface ...@@ -81,92 +47,15 @@ class Highlight extends NamedBuilderBag implements BuilderInterface
/** /**
* Sets html tag and its class used in highlighting. * Sets html tag and its class used in highlighting.
* *
* @param string $tag * @param array $preTags
* @param string $class * @param array $postTags
* *
* @return Highlight * @return $this
*/ */
public function setTag($tag, $class = null) public function setTags(array $preTags, array $postTags)
{ {
$this->tags[] = array_filter( $this->tags['pre_tags'] = $preTags;
[ $this->tags['post_tags'] = $postTags;
'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; return $this;
} }
...@@ -184,44 +73,18 @@ class Highlight extends NamedBuilderBag implements BuilderInterface ...@@ -184,44 +73,18 @@ class Highlight extends NamedBuilderBag implements BuilderInterface
*/ */
public function toArray() public function toArray()
{ {
$highlight = array_filter( $output = [];
[
'order' => $this->order, if (is_array($this->tags)) {
'type' => $this->type, $output = $this->tags;
'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; $output = $this->processArray($output);
}
/** foreach ($this->fields as $field => $params) {
* Returns fields as array. $output['fields'][$field] = count($params) ? $params : new \stdClass();
*
* @return array
*/
private function getFields()
{
$out = [];
foreach ($this->all() as $builder) {
$out = array_merge($out, [$builder->getName() => $builder->toArray()]);
} }
return $out; return $output;
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment