Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Commit

Permalink
Merge branch 'release/1.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-malkoun committed May 7, 2019
2 parents 7a3f3de + 8fd772c commit 97427fd
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 20 deletions.
77 changes: 60 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,67 @@
# tide_search
Search configuration for Tide distribution
# Tide Search
Search functionality for [Tide](https://github.com/dpc-sdp/tide) distribution for [Drupal 8](https://github.com/dpc-sdp)

[![CircleCI](https://circleci.com/gh/dpc-sdp/tide_search.svg?style=svg&circle-token=548e7d78f68716b9ab432396d9a046f1f9836fef)](https://circleci.com/gh/dpc-sdp/tide_search)
Tide is a Drupal 8 distribution focused on delivering an API first, headless Drupal content administration site.

# CONTENTS OF THIS FILE
[![CircleCI](https://circleci.com/gh/dpc-sdp/tide_search.svg?style=shield&circle-token=2a0e49166724ac193636fba5b458024e00342dce)](https://circleci.com/gh/dpc-sdp/tide_search)
[![Release](https://img.shields.io/github/release/dpc-sdp/tide_search.svg)](https://github.com/dpc-sdp/tide_search/releases/latest)
![https://www.drupal.org/8](https://img.shields.io/badge/Drupal-8-blue.svg)
[![Licence: GPL 2](https://img.shields.io/badge/licence-GPL2-blue.svg)](https://github.com/dpc-sdp/tide_search/blob/master/LICENSE.txt)
[![Pull Requests](https://img.shields.io/github/issues-pr/dpc-sdp/tide_page.svg)](https://github.com/dpc-sdp/tide_search/pulls)

* Introduction
* Requirements
* Installation
## What is in this package
- Search API configuration for Elasticsearch connector

# INTRODUCTION
The Tide Search module provides the search configuration for Tide.
## Installation
To install this package, add this custom repository to `repositories` section of
your `composer.json`:

# REQUIREMENTS
* [Tide Core](https://github.com/dpc-sdp/tide_core)
* [Search API](http://www.drupal.org/project/search_api)
* [Elasticsearch Connector](http://www.drupal.org/project/elasticsearch_connector)
```json
{
"repositories": {
"dpc-sdp/tide_search": {
"type": "vcs",
"no-api": true,
"url": "https://github.com/dpc-sdp/tide_search.git"
}
}
}
```

# INSTALLATION
Include the Tide Search module in your composer.json file
Require this package as any other Composer package:
```bash
composer require dpc-sdp/tide_search
```
composer require dpc/tide_search
```

## Support
[Digital Engagement, Department of Premier and Cabinet, Victoria, Australia](https://github.com/dpc-sdp)
is a maintainer of this package.

## Contribute
[Open an issue](https://github.com/dpc-sdp) on GitHub or submit a pull request with suggested changes.

## Development and maintenance
Development is powered by [Dev-Tools](https://github.com/dpc-sdp/dev-tools). Please refer to Dev-Tools'
page for [system requirements](https://github.com/dpc-sdp/dev-tools/#prerequisites) and other details.

To start local development stack:
1. Checkout this project
2. Run `./dev-tools.sh`
3. Run `ahoy build`

## Related projects
- [tide](https://github.com/dpc-sdp/tide)
- [tide_api](https://github.com/dpc-sdp/tide_api)
- [tide_core](https://github.com/dpc-sdp/tide_core)
- [tide_event](https://github.com/dpc-sdp/tide_event)
- [tide_landing_page](https://github.com/dpc-sdp/tide_landing_page)
- [tide_media](https://github.com/dpc-sdp/tide_media)
- [tide_monsido](https://github.com/dpc-sdp/tide_monsido)
- [tide_news](https://github.com/dpc-sdp/tide_news)
- [tide_page](https://github.com/dpc-sdp/tide_page)
- [tide_site](https://github.com/dpc-sdp/tide_site)
- [tide_test](https://github.com/dpc-sdp/tide_test)
- [tide_webform](https://github.com/dpc-sdp/tide_webform)

## License
This project is licensed under [GPL2](https://github.com/dpc-sdp/tide_search/blob/master/LICENSE.txt)
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"require": {
"drupal/search_api": "^1.11",
"drupal/elasticsearch_connector": "^5.0-alpha3",
"dpc-sdp/tide_core": "^1.0",
"drupal/metatag": "~1.7.0"
"dpc-sdp/tide_core": "1.1.0"
},
"repositories": {
"drupal": {
Expand Down
2 changes: 1 addition & 1 deletion dev-init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@

# Development only: uncomment and set the commit value to fetch Dev Tools at
# specific commit.
#export GH_COMMIT=COMMIT_SHA
export GH_COMMIT=e7c3b83e650e69b16aa5ba6db2c21f7ad3257769

curl https://raw.githubusercontent.com/dpc-sdp/dev-tools/master/install | bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
diff --git a/src/ElasticSearch/Parameters/Factory/IndexFactory.php b/src/ElasticSearch/Parameters/Factory/IndexFactory.php
index 11f7b9c..4114a9e 100644
--- a/src/ElasticSearch/Parameters/Factory/IndexFactory.php
+++ b/src/ElasticSearch/Parameters/Factory/IndexFactory.php
@@ -6,11 +6,14 @@ use Drupal\search_api\IndexInterface;
use Drupal\elasticsearch_connector\Event\PrepareIndexEvent;
use Drupal\elasticsearch_connector\Event\PrepareIndexMappingEvent;
use Drupal\search_api_autocomplete\Suggester\SuggesterInterface;
+use Drupal\elasticsearch_connector\Entity\Cluster;
+use Drupal\search_api\Entity\Server;

/**
* Create Elasticsearch Indices.
*/
class IndexFactory {
+ const HASH_LENGTH = 32;

/**
* Build parameters required to index.
@@ -84,6 +87,18 @@ class IndexFactory {
*/
public static function bulkDelete(IndexInterface $index, array $ids) {
$params = IndexFactory::index($index, TRUE);
+
+ // This convoluted path to the host domain is due to
+ // https://www.drupal.org/project/search_api/issues/2976339 not populating
+ // `search_api.server.server_id` config with the correct values.
+ $cluster_id = Server::load($index->getServerId())->getBackend()->getCluster();
+ $host = parse_url(Cluster::load($cluster_id)->url, PHP_URL_HOST);
+ $hash = strstr($host, '.', TRUE);
+
+ if (isset($hash) && strlen($hash) === self::HASH_LENGTH) {
+ $params['index'] = $hash . '--' . $params['index'];
+ }
+
foreach ($ids as $id) {
$params['body'][] = [
'delete' => [
@@ -224,15 +239,29 @@ class IndexFactory {
*/
public static function getIndexName(IndexInterface $index) {

- $options = \Drupal::database()->getConnectionOptions();
- $site_database = $options['database'];
-
+ // Get index machine name.
$index_machine_name = is_string($index) ? $index : $index->id();

+ // Get prefix and suffix form the cluster if present.
+ $cluster_id = $index->getServerInstance()->getBackend()->getCluster();
+ $cluster_options = Cluster::load($cluster_id)->options;
+
+ // If prefix is not set ensure we set it to db name by default.
+ if (isset($cluster_options['rewrite']['rewrite_index'])) {
+ $index_prefix = isset($cluster_options['rewrite']['index']['prefix']) ? $cluster_options['rewrite']['index']['prefix'] : '';
+ }
+ else {
+ $options = \Drupal::database()->getConnectionOptions();
+ $index_prefix = 'elasticsearch_index_' . $options['database'] . '_';
+ }
+
+ // Get the index suffix.
+ $index_suffix = isset($cluster_options['rewrite']['index']['suffix']) ? $cluster_options['rewrite']['index']['suffix'] : '';
+
return strtolower(preg_replace(
'/[^A-Za-z0-9_]+/',
'',
- 'elasticsearch_index_' . $site_database . '_' . $index_machine_name
+ $index_prefix . $index_machine_name . $index_suffix
));
}

diff --git a/src/Form/ClusterForm.php b/src/Form/ClusterForm.php
index 0138a68..d1ccbca 100644
--- a/src/Form/ClusterForm.php
+++ b/src/Form/ClusterForm.php
@@ -231,6 +231,51 @@ class ClusterForm extends EntityForm {
'#default_value' => (!empty($this->entity->options['timeout']) ? $this->entity->options['timeout'] : Cluster::ELASTICSEARCH_CONNECTOR_DEFAULT_TIMEOUT),
'#weight' => 5.6,
);
+
+ $form['options']['rewrite'] = [
+ '#tree' => TRUE,
+ '#type' => 'details',
+ '#title' => $this->t('Alter the ES index name.'),
+ '#open' => FALSE,
+ '#weight' => 6,
+ ];
+
+ $form['options']['rewrite']['rewrite_index'] = [
+ '#title' => $this->t('Alter the Elasticsearch index name.'),
+ '#type' => 'checkbox',
+ '#default_value' => (!empty($this->entity->options['rewrite']['rewrite_index']) ? 1 : 0),
+ '#description' => t('Alter the name of the Elasticsearch index by optionally adding a prefix and suffix to the Search API index name.')
+ ];
+
+ $form['options']['rewrite']['index']['prefix'] = [
+ '#type' => 'textfield',
+ '#title' => t('Index name prefix'),
+ '#default_value' => (!empty($this->entity->options['rewrite']['index']['prefix']) ? $this->entity->options['rewrite']['index']['prefix'] : ''),
+ '#description' => t(
+ 'If a value is provided it will be prepended to the index name.'
+ ),
+ '#states' => [
+ 'visible' => [
+ ':input[name="options[rewrite][rewrite_index]"]' => ['checked' => TRUE],
+ ],
+ ],
+ '#weight' => 6.1,
+ ];
+
+ $form['options']['rewrite']['index']['suffix'] = [
+ '#type' => 'textfield',
+ '#title' => t('Index name suffix'),
+ '#default_value' => (!empty($this->entity->options['rewrite']['index']['suffix']) ? $this->entity->options['rewrite']['index']['suffix'] : ''),
+ '#description' => t(
+ 'If a value is provided it will be appended to the index name.'
+ ),
+ '#states' => [
+ 'visible' => [
+ ':input[name="options[rewrite][rewrite_index]"]' => ['checked' => TRUE],
+ ],
+ ],
+ '#weight' => 6.2,
+ ];
}

/**

0 comments on commit 97427fd

Please sign in to comment.