From 3ba089681cccb26ff59f52fc79ff04a057f7c2bd Mon Sep 17 00:00:00 2001 From: Vincent Gao Date: Thu, 12 Dec 2024 00:33:05 +1100 Subject: [PATCH] Make some necessary changes to meet the requirements of elasticsearch_connector 8.x --- .../search_api.server.elasticsearch_bay.yml | 23 +++++ .../ElasticSearchEventSubscribers.php | 99 +++++++++++++++++++ .../TextFieldWithKeywordAndPrefix.php | 21 ++++ modules/tide_search/tide_search.install | 19 ++++ modules/tide_search/tide_search.services.yml | 5 + 5 files changed, 167 insertions(+) create mode 100644 modules/tide_search/config/optional/search_api.server.elasticsearch_bay.yml create mode 100644 modules/tide_search/src/EventSubscriber/ElasticSearchEventSubscribers.php create mode 100644 modules/tide_search/src/Plugin/search_api/data_type/TextFieldWithKeywordAndPrefix.php create mode 100644 modules/tide_search/tide_search.services.yml diff --git a/modules/tide_search/config/optional/search_api.server.elasticsearch_bay.yml b/modules/tide_search/config/optional/search_api.server.elasticsearch_bay.yml new file mode 100644 index 000000000..2b8fb0c11 --- /dev/null +++ b/modules/tide_search/config/optional/search_api.server.elasticsearch_bay.yml @@ -0,0 +1,23 @@ +uuid: ff7ac093-067c-4a15-98f4-2ab4dfda7b9c +langcode: en +status: true +dependencies: + module: + - elasticsearch_connector +_core: + default_config_hash: 4ulEtCC_6q-TNQFpsg_DJsn4keGx1hpp6ZlcMtKggzo +id: elasticsearch_bay +name: 'Elasticsearch on Bay' +description: '' +backend: elasticsearch +backend_config: + connector: standard + connector_config: + url: 'http://elasticsearch:9200' + enable_debug_logging: true + advanced: + fuzziness: auto + prefix: elasticsearch_index_drupal_ + suffix: '' + synonyms: + - '' diff --git a/modules/tide_search/src/EventSubscriber/ElasticSearchEventSubscribers.php b/modules/tide_search/src/EventSubscriber/ElasticSearchEventSubscribers.php new file mode 100644 index 000000000..51cc3facc --- /dev/null +++ b/modules/tide_search/src/EventSubscriber/ElasticSearchEventSubscribers.php @@ -0,0 +1,99 @@ + 'onAlterSettingsEvent', + FieldMappingEvent::class => 'onFieldMappingEvent', + SupportsDataTypeEvent::class => 'onSupportsDataTypeEvent', + ]; + } + + /** + * Event handler for SupportsDataTypeEvent. + */ + public function onSupportsDataTypeEvent(SupportsDataTypeEvent $event): void { + if ($event->getType() === 'tide_search_text_field_with_keyword_and_prefix') { + $event->setIsSupported(TRUE); + } + } + + /** + * Event handler for FieldMappingEvent. + */ + public function onFieldMappingEvent(FieldMappingEvent $event): void { + $param = $event->getParam(); + $field = $event->getField(); + $type = $field->getType(); + $param = match ($type) { + 'tide_search_text_field_with_keyword_and_prefix' => [ + "type" => "text", + "fields" => [ + "keyword" => [ + "type" => "keyword", + "ignore_above" => 256, + ], + "prefix" => [ + "type" => "text", + "index_options" => "docs", + "analyzer" => "i_prefix", + "search_analyzer" => "q_prefix", + ], + ], + ], + default => [], + }; + $event->setParam($param); + } + + /** + * Event handler for AlterSettingsEvent. + */ + public function onAlterSettingsEvent(AlterSettingsEvent $event): void { + $settings = $event->getSettings(); + $settings['analysis'] = [ + "filter" => [ + "front_ngram" => [ + "type" => "edge_ngram", + "min_gram" => "1", + "max_gram" => "12", + ], + ], + "analyzer" => [ + "i_prefix" => [ + "filter" => [ + "cjk_width", + "lowercase", + "asciifolding", + "front_ngram", + ], + "tokenizer" => "standard", + ], + "q_prefix" => [ + "filter" => [ + "cjk_width", + "lowercase", + "asciifolding", + ], + "tokenizer" => "standard", + ], + ], + ]; + $event->setSettings($settings); + } + +} diff --git a/modules/tide_search/src/Plugin/search_api/data_type/TextFieldWithKeywordAndPrefix.php b/modules/tide_search/src/Plugin/search_api/data_type/TextFieldWithKeywordAndPrefix.php new file mode 100644 index 000000000..fa076a924 --- /dev/null +++ b/modules/tide_search/src/Plugin/search_api/data_type/TextFieldWithKeywordAndPrefix.php @@ -0,0 +1,21 @@ + 10005]; + $dependencies['tide_search'][10005] = ['elasticsearch_connector' => 9802]; return $dependencies; } @@ -117,3 +119,20 @@ function tide_search_update_10004() { _tide_core_field_content_category_default_value('tide_search_listing', 'Search listing'); _tide_core_content_category_form_display('tide_search_listing'); } + +/** + * Update for elasticsearch php package version 8. + */ +function tide_search_update_10005() { + $update_helper = \Drupal::service('tide_core.entity_update_helper'); + $update_helper->updateFromOptional('search_api_server', 'elasticsearch_bay'); + $index = Index::load('node'); + $field_settings = $index->get('field_settings'); + if (!empty($field_settings['title'])) { + $field_settings['title']['type'] = 'tide_search_text_field_with_keyword_and_prefix'; + } + if (!empty($field_settings['summary_processed'])) { + $field_settings['summary_processed']['type'] = 'tide_search_text_field_with_keyword_and_prefix'; + } + $index->set('field_settings', $field_settings)->save(); +} diff --git a/modules/tide_search/tide_search.services.yml b/modules/tide_search/tide_search.services.yml new file mode 100644 index 000000000..350027ccd --- /dev/null +++ b/modules/tide_search/tide_search.services.yml @@ -0,0 +1,5 @@ +services: + tide_search.event_subscriber: + class: Drupal\tide_search\EventSubscriber\ElasticSearchEventSubscribers + tags: + - { name: event_subscriber }