Skip to content
Snippets Groups Projects
Commit fec0e17c authored by Martynas Sudintas's avatar Martynas Sudintas
Browse files

Highlight implemented friendly builder

parent 61dd80ec
No related branches found
No related tags found
No related merge requests found
......@@ -12,11 +12,12 @@
namespace ONGR\ElasticsearchBundle\DSL\Highlight;
use ONGR\ElasticsearchBundle\DSL\BuilderInterface;
use ONGR\ElasticsearchBundle\DSL\FriendlyBuilderInterface;
/**
* This class holds data for highlighting field.
*/
class Field
class Field implements FriendlyBuilderInterface
{
const TYPE_PLAIN = 'plain';
const TYPE_POSTINGS = 'postings';
......@@ -25,42 +26,42 @@ class Field
/**
* @var string Field name.
*/
protected $name;
private $name;
/**
* @var string Highlighter type. By default 'plain'.
*/
protected $type;
private $type;
/**
* @var int Size of the highlighted fragment in characters. By default 100.
*/
protected $fragmentSize;
private $fragmentSize;
/**
* @var int Maximum number of fragments to return. By default 5.
*/
protected $numberOfFragments;
private $numberOfFragments;
/**
* @var array Combine matches on multiple fields to highlight a single field.
*/
protected $matchedFields;
private $matchedFields;
/**
* @var BuilderInterface Query to highlight.
*/
protected $highlightQuery;
private $highlightQuery;
/**
* @var int Show part of string even if there are no matches to highlight. Defaults to 0.
*/
protected $noMatchSize;
private $noMatchSize;
/**
* @var bool Highlight fields based on the source.
*/
protected $forceSource;
private $forceSource;
/**
* Creates a highlight for a field.
......@@ -74,7 +75,7 @@ class Field
}
/**
* @return string
* {@inheritdoc}
*/
public function getName()
{
......@@ -183,9 +184,7 @@ class Field
}
/**
* Returns an array of field parameters.
*
* @return array
* {@inheritdoc}
*/
public function toArray()
{
......@@ -201,4 +200,12 @@ class Field
]
);
}
/**
* {@inheritdoc}
*/
public function getType()
{
return $this->type;
}
}
......@@ -11,115 +11,72 @@
namespace ONGR\ElasticsearchBundle\DSL\Highlight;
use ONGR\ElasticsearchBundle\DSL\FriendlyBuilderBag;
use ONGR\ElasticsearchBundle\DSL\FriendlyBuilderInterface;
/**
* Data holder for highlight api.
*/
class Highlight
class Highlight extends FriendlyBuilderBag
{
const TYPE_PLAIN = 'plain';
const TYPE_POSTINGS = 'postings';
const TYPE_FVH = 'fvh';
/**
* @var array Holds fields to highlight.
*/
protected $fields = [];
/**
* @var array Holds html tag name and class that highlight will be put in (default 'em' tag).
*/
protected $tags = [];
private $tags = [];
/**
* @var string Holds tag schema name. 'styled' is the only option yet.
*/
protected $tagsSchema = null;
private $tagsSchema = null;
/**
* @var string Fragments sort type.
*/
protected $order = null;
private $order = null;
/**
* @var string Highlighter type. By default plain.
*/
protected $type = null;
private $type = null;
/**
* @var int Size of the highlighted fragment in characters. By default 100.
*/
protected $fragmentSize = null;
private $fragmentSize = null;
/**
* @var int Maximum number of fragments to return. By default 5.
*/
protected $numberOfFragments = null;
private $numberOfFragments = null;
/**
* Adds field to highlight.
*
* @param Field $field
* {@inheritdoc}
*
* @return Highlight
*/
public function addField(Field $field)
public function add(FriendlyBuilderInterface $builder)
{
if (!$this->hasField($field->getName())) {
$this->fields[] = $field;
}
parent::add($builder);
return $this;
}
/**
* Checks if field already will be highlighted.
*
* @param string $fieldName
*
* @return bool
*/
public function hasField($fieldName)
{
/** @var Field $field */
foreach ($this->fields as $field) {
if ($field->getName() == $fieldName) {
return true;
}
}
return false;
}
/**
* Removes field from highlighting.
*
* @param string $fieldName
* {@inheritdoc}
*
* @return Highlight
*/
public function removeField($fieldName)
public function set(array $builders)
{
/** @var Field $field */
foreach ($this->fields as $key => $field) {
if ($field->getName() == $fieldName) {
unset($this->fields[$key]);
break;
}
}
parent::set($builders);
return $this;
}
/**
* Returns all fields to highlight.
*
* @return array
*/
public function getFields()
{
return $this->fields;
}
/**
* Sets html tag and its class used in highlighting.
*
......@@ -240,8 +197,8 @@ class Highlight
}
}
/** @var Field $field */
foreach ($this->getFields() as $field) {
/** @var FriendlyBuilderInterface $field */
foreach ($this->all() as $field) {
$highlight['fields'][$field->getName()] = $field->toArray();
}
......
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