Created
September 29, 2015 10:56
-
-
Save StephanHoyer/b9cd6cbc4cc93cee8ea6 to your computer and use it in GitHub Desktop.
Simple example how to use elastic search with node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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: { | |
type: 'string' | |
} | |
} | |
} | |
}); | |
} | |
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); |
Hi Stephan,
I need to run multiple ElasticSearch Query in single page application, can you please suggest how to achieve that.
Thanks,
Abhishek
how to filter the search by a field(s)?
I expected something like this :
function search() { return client.search({ index: 'test', q: 'huhu', filter: { user_id: 27 } }).then(log); }
but how to make that work?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I need this scripts renewed using newest elasticsearch-js version.