Skip to content

Commit

Permalink
Make some necessary changes to meet the requirements of elasticsearch…
Browse files Browse the repository at this point in the history
…_connector 8.x
  • Loading branch information
vincent-gao committed Dec 11, 2024
1 parent 7bdd2d0 commit 3ba0896
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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:
- ''
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace Drupal\tide_search\EventSubscriber;

use Drupal\elasticsearch_connector\Event\AlterSettingsEvent;
use Drupal\elasticsearch_connector\Event\FieldMappingEvent;
use Drupal\elasticsearch_connector\Event\SupportsDataTypeEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* ElasticSearch test event subscribers.
*/
class ElasticSearchEventSubscribers implements EventSubscriberInterface {

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array {
return [
AlterSettingsEvent::class => '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);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Drupal\tide_search\Plugin\search_api\data_type;

use Drupal\search_api\Plugin\search_api\data_type\TextDataType;

/**
* Defines a class for a Text Field with Keyword and Prefix Subfields data type.
*
* @SearchApiDataType(
* id = "tide_search_text_field_with_keyword_and_prefix",
* label = @Translation("Text Field with Keyword and Prefix Subfields"),
* description = @Translation("Text Field with Keyword and Prefix Subfields"),
* fallback_type = "text",
* )
*/
final class TextFieldWithKeywordAndPrefix extends TextDataType {

}
19 changes: 19 additions & 0 deletions modules/tide_search/tide_search.install
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Install file.
*/

use Drupal\search_api\Entity\Index;
use Drupal\tide_search\TideSearchOperation;

/**
Expand All @@ -21,6 +22,7 @@ function tide_search_install() {
function tide_search_update_dependencies() {
$dependencies = [];
$dependencies['tide_search'][10004] = ['tide_core' => 10005];
$dependencies['tide_search'][10005] = ['elasticsearch_connector' => 9802];
return $dependencies;
}

Expand Down Expand Up @@ -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();
}
5 changes: 5 additions & 0 deletions modules/tide_search/tide_search.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
tide_search.event_subscriber:
class: Drupal\tide_search\EventSubscriber\ElasticSearchEventSubscribers
tags:
- { name: event_subscriber }

0 comments on commit 3ba0896

Please sign in to comment.