Finish refactoring mapping creation

This commit is contained in:
Thibaud Fabre
2016-10-18 20:06:49 +02:00
parent 7a71886dc9
commit f2cfe93f8c
13 changed files with 552 additions and 226 deletions

View File

@@ -11,9 +11,7 @@
namespace Alchemy\Phrasea\SearchEngine\Elastic\Mapping;
use Alchemy\Phrasea\SearchEngine\Elastic\FieldMapping;
class StringFieldMapping extends FieldMapping
class StringFieldMapping extends ComplexFieldMapping
{
/**
* @var bool
@@ -30,9 +28,57 @@ class StringFieldMapping extends FieldMapping
*/
private $searchAnalyzer = null;
/**
* @var string|null
*/
private $termVector = null;
/**
* @param string $name
*/
public function __construct($name)
{
parent::__construct($name, self::TYPE_STRING);
}
public function addAnalyzedChild($name, $analyzer)
{
$child = new self($name);
$child->setAnalyzer($analyzer);
$this->addChild($child);
return $this;
}
public function addAnalyzedChildren(array $locales)
{
$child = new StringFieldMapping('light');
$child->setAnalyzer('general_light');
$this->addChild($child);
$this->addLocalizedChildren($locales);
return $this;
}
public function addLocalizedChildren(array $locales)
{
foreach ($locales as $locale) {
/** @var StringFieldMapping $child */
$child = new StringFieldMapping($locale);
$child->setAnalyzer(sprintf('%s_full', $locale));
$this->addChild($child);
}
return $this;
}
/**
* @param string $analyzer
* @param string|null $type
* @return $this
*/
public function setAnalyzer($analyzer, $type = null)
{
@@ -56,37 +102,63 @@ class StringFieldMapping extends FieldMapping
default:
throw new \LogicException(sprintf('Invalid analyzer type "%s".', $type));
}
return $this;
}
public function disableAnalysis()
{
$this->enableAnalysis = false;
return $this;
}
public function enableAnalysis()
{
$this->enableAnalysis = true;
return $this;
}
public function enableTermVectors($applyToChildren = false)
{
$this->termVector = 'with_positions_offsets';
if ($applyToChildren) {
/** @var self $child */
foreach ($this->getChildren() as $child) {
if ($child instanceof StringFieldMapping) {
$child->enableTermVectors(false);
}
}
}
return $this;
}
/**
* @return array
*/
public function toArray()
protected function getProperties()
{
$configuration = [];
$properties = [];
if ($this->analyzer) {
$configuration['analyzer'] = $this->analyzer;
$properties['analyzer'] = $this->analyzer;
}
if ($this->searchAnalyzer) {
$configuration['search_analyzer'] = $this->searchAnalyzer;
$properties['search_analyzer'] = $this->searchAnalyzer;
}
if (! $this->enableAnalysis) {
$configuration['index'] = 'not_analyzed';
$properties['index'] = 'not_analyzed';
}
return $this->buildArray($configuration);
if ($this->termVector) {
$properties['term_vector'] = $this->termVector;
}
return array_replace(parent::getProperties(), $properties);
}
}