Skip to content
Snippets Groups Projects
Commit 1437a624 authored by Zigmas Satkevičius's avatar Zigmas Satkevičius
Browse files

Added context suggester DSL.

parent 6aac12e3
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\ElasticsearchBundle\DSL\Suggester;
use ONGR\ElasticsearchBundle\DSL\Suggester\Context\AbstractContext;
/**
* Context suggester.
*/
class Context extends AbstractSuggester
{
/**
* @var AbstractContext[]
*/
private $context;
/**
* Size of completion.
*
* @var int
*/
private $size;
/**
* Returns context.
*
* @return AbstractContext[]
*/
public function getContext()
{
return $this->context;
}
/**
* Sets context array.
*
* @param AbstractContext[] $context
*/
public function setContext($context)
{
$this->context = $context;
}
/**
* Sets context.
*
* @param AbstractContext $context
*/
public function addContext($context)
{
$this->context[] = $context;
}
/**
* @return int
*/
public function getSize()
{
return $this->size;
}
/**
* @param int $size
*/
public function setSize($size)
{
$this->size = $size;
}
/**
* {@inheritdoc}
*/
public function toArray()
{
$completion = ['field' => $this->getField()];
foreach ($this->context as $context) {
$completion['context'][$context->getName()] = $context->toArray();
}
if ($this->getSize() !== null) {
$completion['size'] = $this->getSize();
}
return [
$this->getName() => [
'text' => $this->getText(),
'completion' => $completion,
]
];
}
/**
* {@inheritdoc}
*/
public function getType()
{
return 'completion';
}
}
<?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\Suggester\Context;
/**
* Abstract context to be used by geo context and category context.
*/
abstract class AbstractContext
{
/**
* Name of the context used.
*
* @var string
*/
private $name;
/**
* Value of the context.
*
* @var string|array
*/
private $value;
/**
* Constructor.
*
* @param string $name Context name.
* @param array|string $value Context value.
*/
public function __construct($name, $value)
{
$this->name = $name;
$this->value = $value;
}
/**
* Converts context to an array.
*
* @return array
*/
abstract public function toArray();
/**
* Returns name of the context.
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Sets type of the context.
*
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* @return array|string
*/
public function getValue()
{
return $this->value;
}
/**
* @param array|string $value
*/
public function setValue($value)
{
$this->value = $value;
}
}
<?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\Suggester\Context;
/**
* Category context to be used by context suggester.
*/
class CategoryContext extends AbstractContext
{
/**
* {@inheritdoc}
*/
public function toArray()
{
return $this->getValue();
}
}
<?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\Suggester\Context;
/**
* Geo context to be used by context suggester.
*/
class GeoContext extends AbstractContext
{
/**
* @var string
*/
private $precision;
/**
* @return mixed
*/
public function getPrecision()
{
return $this->precision;
}
/**
* @param mixed $precision
*/
public function setPrecision($precision)
{
$this->precision = $precision;
}
/**
* {@inheritdoc}
*/
public function toArray()
{
$out = ['value' => $this->getValue()];
if ($this->getPrecision() !== null) {
$out['precision'] = $this->getPrecision();
}
return $out;
}
}
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