Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option 'ignore_above' to mapping and refactoring code #1487

Merged
merged 1 commit into from
Aug 6, 2019
Merged
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
85 changes: 48 additions & 37 deletions src/module-elasticsuite-core/Index/Mapping/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
*/
class Field implements FieldInterface
{
/**
* @var int
*/
private const IGNORE_ABOVE_COUNT = 256;

/**
* @var string
*/
Expand Down Expand Up @@ -89,71 +94,71 @@ public function __construct($name, $type = self::FIELD_TYPE_KEYWORD, $nestedPath
/**
* {@inheritdoc}
*/
public function getName()
public function getName(): string
{
return $this->name;
}

/**
* {@inheritdoc}
*/
public function getType()
public function getType(): string
{
return $this->type;
}

/**
* {@inheritdoc}
*/
public function isSearchable()
public function isSearchable(): bool
{
return (bool) $this->config['is_searchable'];
}

/**
* {@inheritdoc}
*/
public function isFilterable()
public function isFilterable(): bool
{
return (bool) $this->config['is_filterable'];
}

/**
* {@inheritDoc}
*/
public function isUsedForSortBy()
public function isUsedForSortBy(): bool
{
return (bool) $this->config['is_used_for_sort_by'];
}

/**
* {@inheritdoc}
*/
public function isUsedInSpellcheck()
public function isUsedInSpellcheck(): bool
{
return (bool) $this->config['is_used_in_spellcheck'] && (bool) $this->config['is_searchable'];
}

/**
* {@inheritdoc}
*/
public function getSearchWeight()
public function getSearchWeight(): int
{
return (int) $this->config['search_weight'];
}

/**
* {@inheritdoc}
*/
public function isNested()
public function isNested(): bool
{
return is_string($this->nestedPath) && !empty($this->nestedPath);
}

/**
* {@inheritdoc}
*/
public function getNestedPath()
public function getNestedPath(): ?string
{
return $this->nestedPath;
}
Expand All @@ -176,11 +181,11 @@ public function getNestedFieldName()
/**
* {@inheritdoc}
*/
public function getMappingPropertyConfig()
public function getMappingPropertyConfig(): array
{
$property = $this->getPropertyConfig();

if ($this->getType() == self::FIELD_TYPE_TEXT) {
if ($this->getType() === self::FIELD_TYPE_TEXT) {
$analyzers = $this->getFieldAnalyzers();
$property = $this->getPropertyConfig(current($analyzers));

Expand All @@ -200,14 +205,14 @@ public function getMappingProperty($analyzer = self::ANALYZER_UNTOUCHED)
$fieldName = $this->getName();
$propertyName = $fieldName;
$property = $this->getMappingPropertyConfig();
$isDefaultAnalyzer = $analyzer == $this->getDefaultSearchAnalyzer();
$isDefaultAnalyzer = $analyzer === $this->getDefaultSearchAnalyzer();

if (isset($property['fields']) && !$isDefaultAnalyzer) {
if (!$isDefaultAnalyzer && isset($property['fields'])) {
$propertyName = null;

if (isset($property['fields'][$analyzer])) {
$property = $property['fields'][$analyzer];
$propertyName = $isDefaultAnalyzer ? $fieldName : sprintf("%s.%s", $fieldName, $analyzer);
$propertyName = $isDefaultAnalyzer ? $fieldName : sprintf('%s.%s', $fieldName, $analyzer);
}
}

Expand Down Expand Up @@ -257,7 +262,7 @@ public function getSortMissing($direction = SortOrderInterface::SORT_ASC)
/**
* {@inheritDoc}
*/
public function getConfig()
public function getConfig(): array
{
return $this->config ?? [];
}
Expand All @@ -270,14 +275,14 @@ public function getConfig()
*
* @return boolean
*/
private function checkAnalyzer($property, $expectedAnalyzer)
private function checkAnalyzer($property, $expectedAnalyzer): bool
{
$isAnalyzerCorrect = true;

if ($property['type'] == self::FIELD_TYPE_TEXT || $property['type'] == self::FIELD_TYPE_KEYWORD) {
if ($property['type'] === self::FIELD_TYPE_TEXT || $property['type'] === self::FIELD_TYPE_KEYWORD) {
$isAnalyzed = $expectedAnalyzer !== self::ANALYZER_UNTOUCHED;

if ($isAnalyzed && (!isset($property['analyzer']) || $property['analyzer'] != $expectedAnalyzer)) {
if ($isAnalyzed && (!isset($property['analyzer']) || $property['analyzer'] !== $expectedAnalyzer)) {
$isAnalyzerCorrect = false;
} elseif (!$isAnalyzed && $property['type'] !== self::FIELD_TYPE_KEYWORD) {
$isAnalyzerCorrect = false;
Expand All @@ -299,13 +304,13 @@ private function checkAnalyzer($property, $expectedAnalyzer)
*
* @return array
*/
private function getMultiFieldMappingPropertyConfig($analyzers)
private function getMultiFieldMappingPropertyConfig($analyzers): array
{
// Setting the field type to "multi_field".
$property = [];

foreach ($analyzers as $analyzer) {
if ($analyzer == $this->getDefaultSearchAnalyzer()) {
if ($analyzer === $this->getDefaultSearchAnalyzer()) {
$property = array_merge($property, $this->getPropertyConfig($analyzer));
} else {
$property['fields'][$analyzer] = $this->getPropertyConfig($analyzer);
Expand All @@ -320,21 +325,20 @@ private function getMultiFieldMappingPropertyConfig($analyzers)
*
* @return array
*/
private function getFieldAnalyzers()
private function getFieldAnalyzers(): array
{
$analyzers = [];

if ($this->isSearchable() || $this->isUsedForSortBy()) {
// Default search analyzer.
$analyzers = [$this->getDefaultSearchAnalyzer()];

if ($this->isSearchable() && $this->getSearchWeight() > 1) {
$analyzers[] = self::ANALYZER_WHITESPACE;
$analyzers[] = self::ANALYZER_SHINGLE;
}
}
if ($this->isSearchable() && $this->getSearchWeight() > 1) {
$analyzers[] = self::ANALYZER_WHITESPACE;
$analyzers[] = self::ANALYZER_SHINGLE;
}

if ($this->isFilterable() || empty($analyzers)) {
if (empty($analyzers) || $this->isFilterable()) {
// For filterable fields or fields without analyzer : append the untouched analyzer.
$analyzers[] = self::ANALYZER_UNTOUCHED;
}
Expand All @@ -354,19 +358,26 @@ private function getFieldAnalyzers()
*
* @return array
*/
private function getPropertyConfig($analyzer = self::ANALYZER_UNTOUCHED)
private function getPropertyConfig($analyzer = self::ANALYZER_UNTOUCHED): array
{
$fieldMapping = ['type' => $this->getType()];

if ($this->getType() == self::FIELD_TYPE_TEXT && $analyzer == self::ANALYZER_UNTOUCHED) {
$fieldMapping['type'] = self::FIELD_TYPE_KEYWORD;
} elseif ($this->getType() == self::FIELD_TYPE_TEXT) {
$fieldMapping['analyzer'] = $analyzer;
if ($analyzer === self::ANALYZER_SORTABLE) {
$fieldMapping['fielddata'] = true;
}
} elseif ($this->getType() == self::FIELD_TYPE_DATE) {
$fieldMapping['format'] = implode('||', $this->dateFormats);
switch ($this->getType()) {
case self::FIELD_TYPE_TEXT:
if ($analyzer === self::ANALYZER_UNTOUCHED) {
$fieldMapping['type'] = self::FIELD_TYPE_KEYWORD;
$fieldMapping['ignore_above'] = self::IGNORE_ABOVE_COUNT;
}
if ($analyzer !== self::ANALYZER_UNTOUCHED) {
$fieldMapping['analyzer'] = $analyzer;
if ($analyzer === self::ANALYZER_SORTABLE) {
$fieldMapping['fielddata'] = true;
}
}
break;
case self::FIELD_TYPE_DATE:
$fieldMapping['format'] = implode('||', $this->dateFormats);
break;
}

return $fieldMapping;
Expand Down