Fix thesaurus regression from f25bdf4171

- Field class is now immutable, mergeWith() returns a new instance
- Thesaurus roots are recursively merged
- In case of field merge, Structure class do not index the previous field
- Added failing test case for previous bugs
- Added merge tests on Field class
- Added tests for all indexed stuff inside Structure class
This commit is contained in:
Mathieu Darse
2015-06-19 21:17:01 +02:00
parent 1dd4bc5c5f
commit a74d0cd7bd
6 changed files with 291 additions and 40 deletions

View File

@@ -4,7 +4,9 @@ namespace Alchemy\Phrasea\SearchEngine\Elastic\Structure;
use Alchemy\Phrasea\SearchEngine\Elastic\Exception\MergeException;
use Alchemy\Phrasea\SearchEngine\Elastic\Mapping;
use Alchemy\Phrasea\SearchEngine\Elastic\Thesaurus\Concept;
use Alchemy\Phrasea\SearchEngine\Elastic\Thesaurus\Helper as ThesaurusHelper;
use Assert\Assertion;
use databox_field;
/**
@@ -32,14 +34,12 @@ class Field
$roots = null;
}
return new self(
$field->get_name(),
$type,
$field->is_indexable(),
$field->isBusiness(),
$field->isAggregable(),
$roots
);
return new self($field->get_name(), $type, [
'searchable' => $field->is_indexable(),
'private' => $field->isBusiness(),
'facet' => $field->isAggregable(),
'thesaurus_roots' => $roots
]);
}
private static function getTypeFromLegacy(databox_field $field)
@@ -58,14 +58,21 @@ class Field
}
}
public function __construct($name, $type, $searchable = true, $private = false, $facet = false, array $thesaurus_roots = null)
public function __construct($name, $type, array $options = [])
{
$this->name = (string) $name;
$this->type = (string) $type;
$this->is_searchable = (bool) $searchable;
$this->is_private = (bool) $private;
$this->is_facet = (bool) $facet;
$this->thesaurus_roots = $thesaurus_roots;
$this->type = $type;
$this->is_searchable = \igorw\get_in($options, ['searchable'], true);
$this->is_private = \igorw\get_in($options, ['private'], false);
$this->is_facet = \igorw\get_in($options, ['facet'], false);
$this->thesaurus_roots = \igorw\get_in($options, ['thesaurus_roots'], null);
Assertion::boolean($this->is_searchable);
Assertion::boolean($this->is_private);
Assertion::boolean($this->is_facet);
if ($this->thesaurus_roots !== null) {
Assertion::allIsInstanceOf($this->thesaurus_roots, Concept::class);
}
}
public function getName()
@@ -112,6 +119,13 @@ class Field
return $this->thesaurus_roots;
}
/**
* Merge with another field, returning the new instance
*
* @param Field $other
* @return Field
* @throws MergeException
*/
public function mergeWith(Field $other)
{
if (($name = $other->getName()) !== $this->name) {
@@ -131,11 +145,26 @@ class Field
}
if ($other->isSearchable() !== $this->is_searchable) {
throw new MergeException(sprintf("Field %s can't be merged, incompatible searchable state", $name));
throw new MergeException(sprintf("Field %s can't be merged, incompatible searchablility", $name));
}
if ($other->isFacet() !== $this->is_facet) {
throw new MergeException(sprintf("Field %s can't be merged, incompatible to_aggregate state", $name));
throw new MergeException(sprintf("Field %s can't be merged, incompatible facet eligibility", $name));
}
$thesaurus_roots = null;
if ($this->thesaurus_roots !== null || $other->thesaurus_roots !== null) {
$thesaurus_roots = array_merge(
(array) $this->thesaurus_roots,
(array) $other->thesaurus_roots
);
}
return new self($this->name, $this->type, [
'searchable' => $this->is_searchable,
'private' => $this->is_private,
'facet' => $this->is_facet,
'thesaurus_roots' => $thesaurus_roots
]);
}
}