Commit 0f66b776 authored by Nikolay Ignatov's avatar Nikolay Ignatov
Browse files

Issue #3061167: Changing the index and mappings to work without depricated type.

parent ea6d4b8f
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -69,7 +69,7 @@ class SearchBuilder {
  public function build() {
    // Query options.
    $indexFactory = \Drupal::service('elasticsearch_connector.index_factory');
    $params = $indexFactory->index($this->index, TRUE);
    $params = $indexFactory->index($this->index);

    $query_options = $this->getSearchQueryOptions();

+5 −13
Original line number Diff line number Diff line
@@ -27,22 +27,15 @@ class IndexFactory {
   *
   * @param \Drupal\search_api\IndexInterface $index
   *   Index to create.
   * @param bool $with_type
   *   Should the index be created with a type.
   *
   * @return array
   *   Associative array with the following keys:
   *   - index: The name of the index on the Elasticsearch server.
   *   - type(optional): The name of the type to use for the given index.
   */
  public static function index(IndexInterface $index, $with_type = FALSE) {
  public static function index(IndexInterface $index) {
    $params = [];
    $params['index'] = static::getIndexName($index);

    if ($with_type) {
      $params['type'] = $index->id();
    }

    return $params;
  }

@@ -84,12 +77,11 @@ class IndexFactory {
   * @return array
   */
  public static function bulkDelete(IndexInterface $index, array $ids) {
    $params = IndexFactory::index($index, TRUE);
    $params = IndexFactory::index($index);
    foreach ($ids as $id) {
      $params['body'][] = [
        'delete' => [
          '_index' => $params['index'],
          '_type' => $params['type'],
          '_id' => $id,
        ],
      ];
@@ -111,7 +103,7 @@ class IndexFactory {
   *   index.
   */
  public static function bulkIndex(IndexInterface $index, array $items) {
    $params = static::index($index, TRUE);
    $params = static::index($index);

    foreach ($items as $id => $item) {
      $data = [
@@ -172,7 +164,7 @@ class IndexFactory {
   *   Parameters required to create an index mapping.
   */
  public static function mapping(IndexInterface $index) {
    $params = static::index($index, TRUE);
    $params = static::index($index);

    $properties = [
      'id' => [
@@ -213,7 +205,7 @@ class IndexFactory {
      'type' => 'keyword',
    ];

    $params['body'][$params['type']]['properties'] = $properties;
    $params['body']['properties'] = $properties;

    // Allow other modules to alter index mapping before we create it.
    $dispatcher = \Drupal::service('event_dispatcher');
+6 −44
Original line number Diff line number Diff line
@@ -414,26 +414,10 @@ class SearchApiElasticsearchBackend extends BackendPluginBase implements PluginF
   *   TRUE on success, FALSE otherwise.
   */
  public function fieldsUpdated(IndexInterface $index) {
    $params = $this->indexFactory->index($index, TRUE);

    try {
      if ($this->client->indices()->existsType($params)) {
        $current_mapping = $this->client->indices()->getMapping($params);
        if (!empty($current_mapping)) {
    try {
            // If the mapping exits, delete it to be able to re-create it.
            $this->client->indices()->deleteMapping($params);
          }
          catch (ElasticsearchException $e) {
            // If the mapping exits, delete the index and recreate it.
            // In Elasticsearch 2.3 it is not possible to delete a mapping,
            // so don't use $this->client->indices()->deleteMapping as doing so
            // will throw an exception.
            $this->removeIndex($index);
      if (!$this->client->indices()->exists($this->indexFactory->index($index))) {
        $this->addIndex($index);
      }
        }
      }

      $response = $this->client->indices()->putMapping(
        $this->indexFactory->mapping($index)
@@ -443,7 +427,7 @@ class SearchApiElasticsearchBackend extends BackendPluginBase implements PluginF
        \Drupal::messenger()->addError(t('Cannot create the mapping of the fields!'));
      }
    }
    catch (ElasticsearchException $e) {
    catch (\Exception $e) {
      \Drupal::messenger()->addError($e->getMessage());
      return FALSE;
    }
@@ -471,9 +455,7 @@ class SearchApiElasticsearchBackend extends BackendPluginBase implements PluginF
   * {@inheritdoc}
   */
  public function indexItems(IndexInterface $index, array $items) {
    $elastic_type_exists = $this->doesTypeExists($index);

    if (empty($elastic_type_exists) || empty($items)) {
    if (empty($items)) {
      return array();
    }

@@ -580,10 +562,10 @@ class SearchApiElasticsearchBackend extends BackendPluginBase implements PluginF
    // Get index.
    $index = $query->getIndex();

    $params = $this->indexFactory->index($index, TRUE);
    $params = $this->indexFactory->index($index);

    // Check Elasticsearch index.
    if (!$this->client->indices()->existsType($params)) {
    if (!$this->client->indices()->exists($params)) {
      return $search_result;
    }

@@ -736,26 +718,6 @@ class SearchApiElasticsearchBackend extends BackendPluginBase implements PluginF
    $results->setExtraData('search_api_facets', $attach);
  }

  /**
   * Helper function, check if the given index and type exists.
   *
   * @param \Drupal\search_api\IndexInterface $index
   *   Index object.
   *
   * @return bool
   *   TRUE if the given index exists in Elasticsearch, otherwise FALSE.
   */
  protected function doesTypeExists(IndexInterface $index) {
    $params = $this->indexFactory->index($index, TRUE);
    try {
      return $this->client->indices()->existsType($params);
    }
    catch (ElasticsearchException $e) {
      \Drupal::messenger()->addError($e->getMessage());
      return FALSE;
    }
  }

  /**
   * Prefixes an index ID as configured.
   *