Search code examples
phpelasticsearchdrupaldrupal-8

Drupal 8 store node data alternative in store


Don't have much of a code example just a question and an idea.

I want to be able to save node data (from content creation) into an in-house datastore using elastic DSL queries.

I have heard/seen slides about Drupal supporting MongoDB to extent which makes me think this is doable. Even if I have to override the NodeForm/save handler is there a way to manipulate the node entities for this purpose?


Solution

  • This is perfectly doable. We're using Elasticsarch in all our Drupal projects, mainly to use it for full-text searches. Every node is saved twice:

    • one time in Drupal's database, this is the node use in every code line
    • one time in an Elasticsearch index, this one is never "used" ; we use full-text searches to find nids, then we retrieve the full object in Drupal DB

    You can use elasticsearch_connector module to get an easy-to-use manager like this:

    $cluster = Cluster::load('ES-CLUSTER');
    $clientManager = \Drupal::service('elasticsearch_connector.client_manager');
    $client = $clientManager->getClientForCluster($this->cluster);
    

    And then use this client to manage your nodes' indexation:

    function hook_node_insert($node) {
      $client->insert($node);
    }
    function hook_node_update($node) {
      $client->update($node);
    }
    function hook_node_insert($node) {
      $client->delete($node);
    }
    

    The biggest part of the work is to create your mapping (if you want it to be manageable via a module), but here again elasticsearch_connector will give you tools for it:

    $client->indices()->putMapping($params); 
    $client->indices()->putSettings($params); 
    $client->search($params);