From 14f677dc059cebd7fdff4780556b09083e32556f Mon Sep 17 00:00:00 2001 From: Gus deMayo Date: Fri, 8 Oct 2021 14:31:42 -0500 Subject: [PATCH] allow for adding custom analyzers to mapping definition --- .../Api/Index/Mapping/FieldInterface.php | 7 ++++ .../Index/Mapping.php | 35 ++++++++++++++++--- .../Index/Mapping/Field.php | 14 ++++++++ 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/module-elasticsuite-core/Api/Index/Mapping/FieldInterface.php b/src/module-elasticsuite-core/Api/Index/Mapping/FieldInterface.php index d43bd3fcd..97481751f 100644 --- a/src/module-elasticsuite-core/Api/Index/Mapping/FieldInterface.php +++ b/src/module-elasticsuite-core/Api/Index/Mapping/FieldInterface.php @@ -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. * diff --git a/src/module-elasticsuite-core/Index/Mapping.php b/src/module-elasticsuite-core/Index/Mapping.php index 87d319951..a84260f3a 100644 --- a/src/module-elasticsuite-core/Index/Mapping.php +++ b/src/module-elasticsuite-core/Index/Mapping.php @@ -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; + } + + /** + * 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) { diff --git a/src/module-elasticsuite-core/Index/Mapping/Field.php b/src/module-elasticsuite-core/Index/Mapping/Field.php index b0f6ed9e4..2505a85dc 100644 --- a/src/module-elasticsuite-core/Index/Mapping/Field.php +++ b/src/module-elasticsuite-core/Index/Mapping/Field.php @@ -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, ]; @@ -240,6 +241,18 @@ public function getDefaultSearchAnalyzer() return $this->config['default_search_analyzer']; } + /** + * {@inheritDoc} + */ + public function getCustomSearchAnalyzers() + { + if (is_array($this->config['custom_search_analyzers'])) { + return $this->config['custom_search_analyzers']; + } + + return json_decode($this->config['custom_search_analyzers'], true); + } + /** * {@inheritDoc} */ @@ -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()) {