Skip to content

Instantly share code, notes, and snippets.

@StephanHoyer
Created September 29, 2015 10:56
Show Gist options
  • Save StephanHoyer/b9cd6cbc4cc93cee8ea6 to your computer and use it in GitHub Desktop.
Save StephanHoyer/b9cd6cbc4cc93cee8ea6 to your computer and use it in GitHub Desktop.

Revisions

  1. StephanHoyer revised this gist Sep 29, 2015. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions elasticsearch-example.js
    Original file line number Diff line number Diff line change
    @@ -22,9 +22,7 @@ function createIndex() {
    mapping: {
    house: {
    name: {
    store: 'yes',
    type: 'string',
    index: 'analyzed'
    type: 'string'
    }
    }
    }
  2. StephanHoyer created this gist Sep 29, 2015.
    79 changes: 79 additions & 0 deletions elasticsearch-example.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,79 @@
    'use strict';

    var elasticsearch = require('elasticsearch');
    var Promise = require('bluebird');

    var log = console.log.bind(console);

    var client = new elasticsearch.Client({
    host: 'localhost:9200',
    log: 'trace'
    });

    function dropIndex() {
    return client.indices.delete({
    index: 'test',
    });
    }

    function createIndex() {
    return client.indices.create({
    index: 'test',
    mapping: {
    house: {
    name: {
    store: 'yes',
    type: 'string',
    index: 'analyzed'
    }
    }
    }
    });
    }

    function addToIndex() {
    return client.index({
    index: 'test',
    type: 'house',
    id: '1',
    body: {
    name: 'huhu'
    }
    });
    }

    function search() {
    return client.search({
    index: 'test',
    q: 'huhu'
    }).then(log);
    }

    function closeConnection() {
    client.close();
    }

    function getFromIndex() {
    return client.get({
    id: 1,
    index: 'test',
    type: 'house',
    }).then(log);

    }

    function waitForIndexing() {
    log('Wait for indexing ....');
    return new Promise(function(resolve) {
    setTimeout(resolve, 2000);
    });
    }

    Promise.resolve()
    .then(dropIndex)
    .then(createIndex)
    .then(addToIndex)
    .then(getFromIndex)
    .then(waitForIndexing)
    .then(search)
    .then(closeConnection);