Skip to content

fabrix-app/spool-elasticsearch

Repository files navigation

spool-elasticsearch

📦 Elasticsearch Spool https://www.elastic.co/products/elasticsearch

Gitter NPM version Build Status Test Coverage Dependency Status Follow @FabrixApp on Twitter

Provides a simple integration with elasticsearch

Usage

###Configure

// config/main.ts
import { ElasticsearchSpool } from '@fabirx/spool-elasticsearch'
export const main = {

  // ...
  spools: [
    ElasticsearchSpool
  ]  
}

Configure connection

Configuration file for Elasticsearch spool is: config/elasticsearch.ts Otherwise you could use config/env/{env}.ts files with elasticsearch property

// config/elasticsearch.ts

export const elasticsearch = {

  connection: {
    // List of hosts for elastic cluster
    // hosts: [],

    // One elastic instance host
    host: 'localhost:9200',
    // Log level
    log: 'trace'
  },

  // Will validate if elastic connection is alive on Fabrix app start
  validateConnection: true
}

Using Elasticsearch API

This spool creates an app propertry with elasticseach client. app.elasticClient So you could use it whatever you want

// api/controller/SomeController.ts

export class SomeController extends Controller {

  someAction (request, reply) {
    // Perform an action
    this.app.elasticClient
      .search({
        q: 'something'
      })
      .then(function (body) {
        const hits = body.hits.hits;
      }, function (error) {
        console.trace(error.message);
      })
  }
}

More information about Elasticsearch client could be found here: Elasticsearch-js

License

MIT