Created
June 13, 2011 16:48
-
-
Save francois2metz/1023154 to your computer and use it in GitHub Desktop.
indextank with Node and Spore
This file contains 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
// mkdir indextank | |
// wget https://github.com/SPORE/api-description/blob/master/services/indextank.json | |
// install node and npm | |
// npm install spore underscore futures | |
// edit baseUrl, password and index var | |
// node indextank.js | |
var baseUrl = 'http://xxx.api.indextank.com'; | |
var password = 'xxx'; | |
var index = 'test_spore'; | |
var spore = require('spore'); | |
var _ = require('underscore'); | |
var futures = require('futures'); | |
var client = spore.createClient(__dirname +'/indextank.json', {base_url: baseUrl}); | |
// json middleware | |
client.enable(spore.middlewares.json()); | |
// status middleware (throw error if http status is not expected) | |
client.enable(spore.middlewares.status()); | |
// authentication middleware | |
client.enable(spore.middlewares.basic('', password)); | |
function say(text) { | |
return function(next) { | |
console.log(text); | |
next(); | |
}; | |
} | |
function listIndexes() { | |
return function(next) { | |
client.list_indexes(function(err, result) { | |
if (err) throw err; | |
_(result.body).chain().keys().each(function(value) {console.log(' -> %s', value) }); | |
next(); | |
}); | |
} | |
} | |
function createIndex(name) { | |
return function(next) { | |
console.log('create a new index %s', name); | |
client.create_index({name: name}, function(err, result) { | |
if (err) throw err; | |
next(); | |
}); | |
}; | |
} | |
function getIndex(name) { | |
return function(next) { | |
console.log('get informations about index %s', name); | |
client.get_index({name : name}, function(err, result) { | |
if (err) throw err; | |
console.log('%s (size: %s)', name, result.body.size); | |
next(); | |
}); | |
}; | |
} | |
function createDocuments(name) { | |
return function(next) { | |
var docs = [ | |
{ | |
docid : 'Perl', | |
fields : { version : '5.14.0'} | |
}, | |
{ | |
docid : 'Node', | |
fields : { version : 'v0.4.8'} | |
} | |
]; | |
client.add_documents({name : name}, JSON.stringify(docs), function(err, result) { | |
if (err) throw err; | |
next(); | |
}); | |
} | |
} | |
function search(name, query) { | |
return function(next) { | |
client.search({name : name, q : query}, function(err, result) { | |
console.log('for search %s on %s, we got %d', query, name, result.body.matches); | |
_(result.body.results).chain().pluck('docid').each(function(value) { | |
console.log(value); | |
}); | |
next(); | |
}); | |
} | |
} | |
futures.sequence() | |
.then(say('listing indexes')) | |
.then(listIndexes()) | |
.then(createIndex(index)) | |
.then(say('listing (again) indexes')) | |
.then(listIndexes()) | |
.then(getIndex(index)) | |
.then(say('create documents')) | |
.then(createDocuments(index)) | |
.then(getIndex(index)) | |
.then(search('demoIndex', 'guitar')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment