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

allow for adding custom analyzers to mapping definition #2282

Open
wants to merge 1 commit into
base: 2.10.x
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ public function getMappingProperty($analyzer = self::ANALYZER_UNTOUCHED);
*/
public function getDefaultSearchAnalyzer();

/**
* Return custom search analyzers used for fulltext searches.
*
* @return array
*/
public function getCustomSearchAnalyzers();

/**
* Merge field config and return a new instance with the updated config.
*
Expand Down
35 changes: 31 additions & 4 deletions src/module-elasticsuite-core/Index/Mapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,41 @@ public function getWeightedSearchProperties(
}

foreach ($fields as $field) {
$currentAnalyzer = $analyzer;
$canAddField = $defaultField === null || $field->getSearchWeight() !== 1;
$canAddField = $defaultField === null || $field->getSearchWeight() !== 1;
$analyzers = [];

if ($analyzer === null) {
$currentAnalyzer = $field->getDefaultSearchAnalyzer();
$canAddField = $canAddField || ($currentAnalyzer !== FieldInterface::ANALYZER_STANDARD);
$defaultAnalyzer = $field->getDefaultSearchAnalyzer();
$analyzers = $field->getCustomSearchAnalyzers();
$analyzers[] = $defaultAnalyzer;
$canAddField = $canAddField || ($defaultAnalyzer !== FieldInterface::ANALYZER_STANDARD);
}

if ($analyzer !== null) {
$analyzers[] = $analyzer;
}

$weightedFields = array_merge($weightedFields, $this->addWeightedFields($analyzers, $canAddField, $field, $boost));
}

return $weightedFields;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need additional test cases in unit testing to deal with the fact that we can have custom_analyzers or not.

Actually the existing tests are not failing, which is good, but there is no clue about the expected result if the field is meant to have custom_analyzers

}

/**
* Add weighted fields for each analyzer
*
* @param array $analyzers Analyzers
* @param bool $canAddField Field can be added
* @param FieldInterface $field Fild
* @param int $boost Boost applieds
*
* @return array
*/
private function addWeightedFields($analyzers, $canAddField, $field, $boost)
{
$weightedFields = [];

foreach ($analyzers as $currentAnalyzer) {
$property = $field->getMappingProperty($currentAnalyzer);

if ($property && $canAddField) {
Expand Down
14 changes: 14 additions & 0 deletions src/module-elasticsuite-core/Index/Mapping/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class Field implements FieldInterface
'is_used_in_spellcheck' => false,
'search_weight' => 1,
'default_search_analyzer' => self::ANALYZER_STANDARD,
'custom_search_analyzers' => [],
'filter_logical_operator' => self::FILTER_LOGICAL_OPERATOR_OR,
'norms_disabled' => false,
];
Expand Down Expand Up @@ -240,6 +241,18 @@ public function getDefaultSearchAnalyzer()
return $this->config['default_search_analyzer'];
}

/**
* {@inheritDoc}
*/
public function getCustomSearchAnalyzers()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need some unit testing here, this file is already covered so any new method should also be.

{
if (is_array($this->config['custom_search_analyzers'])) {
return $this->config['custom_search_analyzers'];
}

return json_decode($this->config['custom_search_analyzers'], true);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a huge fan of having json_encoded data being set inside XML file, couldn't it be an XML array either ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, I think the xsd schema should be updated with this new config node probably.

}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -358,6 +371,7 @@ private function getFieldAnalyzers(): array
if ($this->isSearchable() && $this->getSearchWeight() > 1) {
$analyzers[] = self::ANALYZER_WHITESPACE;
$analyzers[] = self::ANALYZER_SHINGLE;
$analyzers = array_merge($analyzers, $this->getCustomSearchAnalyzers());
}

if (empty($analyzers) || $this->isFilterable()) {
Expand Down