Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions Annotation/Suggester/AbstractSuggesterProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?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\Annotation\Suggester;

/**
* Abstract class for various suggester annotations.
*/
abstract class AbstractSuggesterProperty
{
/**
* @var string
*/
public $type = 'completion';

/**
* @var string
*
* @Required
*/
public $name;

/**
* @var string
*
* @Required
*/
public $objectName;

/**
* @var string
*/
public $index_analyzer;

/**
* @var string
*/
public $search_analyzer;

/**
* @var int
*/
public $preserve_separators;

/**
* @var bool
*/
public $preserve_position_increments;

/**
* @var int
*/
public $max_input_length;

/**
* @var bool
*/
public $payloads;

/**
* Returns required properties.
*
* @param array $extraExclude Extra object variables to exclude.
*
* @return array
*/
public function filter($extraExclude = [])
{
return array_diff_key(
array_filter(get_object_vars($this)),
array_flip(array_merge(['name', 'objectName', 'classObjectName'], $extraExclude))
);
}
}
22 changes: 22 additions & 0 deletions Annotation/Suggester/CompletionSuggesterProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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\Annotation\Suggester;

/**
* Class for completion suggester.
*
* @Annotation
* @Target("PROPERTY")
*/
class CompletionSuggesterProperty extends AbstractSuggesterProperty
{
}
59 changes: 59 additions & 0 deletions Annotation/Suggester/Context/AbstractContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?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\Annotation\Suggester\Context;

/**
* Abstract class for various context annotations.
*/
abstract class AbstractContext
{
/**
* @var array
*/
public $default;

/**
* @var string
*
* @Required
*/
public $name;

/**
* @var string
*/
public $path;

/**
* Returns context type.
*
* @return string
*/
abstract public function getType();

/**
* Returns filtered object data.
*
* @return array
*/
public function filter()
{
$vars = array_diff_key(
array_filter(get_object_vars($this)),
array_flip(['name'])
);

$vars['type'] = $this->getType();

return $vars;
}
}
29 changes: 29 additions & 0 deletions Annotation/Suggester/Context/CategoryContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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\Annotation\Suggester\Context;

/**
* Class for geo category context annotations used in context suggester.
*
* @Annotation
* @Target("ANNOTATION")
*/
class CategoryContext extends AbstractContext
{
/**
* {@inheritdoc}
*/
public function getType()
{
return 'category';
}
}
39 changes: 39 additions & 0 deletions Annotation/Suggester/Context/GeoLocationContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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\Annotation\Suggester\Context;

/**
* Class for geo location context annotations used in context suggester.
*
* @Annotation
* @Target("ANNOTATION")
*/
class GeoLocationContext extends AbstractContext
{
/**
* @var array
*/
public $precision;

/**
* @var bool
*/
public $neighbors;

/**
* {@inheritdoc}
*/
public function getType()
{
return 'geo';
}
}
34 changes: 34 additions & 0 deletions Annotation/Suggester/ContextSuggesterProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace ONGR\ElasticsearchBundle\Annotation\Suggester;

use ONGR\ElasticsearchBundle\Annotation\Suggester\Context\AbstractContext;

/**
* Class for context suggester annotations.
*
* @Annotation
* @Target("PROPERTY")
*/
class ContextSuggesterProperty extends AbstractSuggesterProperty
{
/**
* @var array<\ONGR\ElasticsearchBundle\Annotation\Suggester\Context\AbstractContext>
*/
public $context;

/**
* {@inheritdoc}
*/
public function filter($extraExclude = [])
{
$data = parent::filter(['context']);

/** @var AbstractContext $singleContext */
foreach ($this->context as $singleContext) {
$data['context'][$singleContext->name] = $singleContext->filter();
}

return $data;
}
}
107 changes: 107 additions & 0 deletions DSL/Suggester/Context.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?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';
}
}
Loading