Created
April 11, 2011 22:56
-
-
Save lxbarth/914558 to your computer and use it in GitHub Desktop.
Quick and dirty node script I used for throwing queries and Elastic Search
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
var client = require('http').createClient(9200, 'localhost'), | |
argv = require('./node-optimist').argv; | |
var query = { | |
"from": 0, | |
"size": 5, | |
"query" : { | |
"match_all" : { } | |
}, | |
"sort": { | |
"date": { | |
"order": "desc" | |
} | |
}, | |
"facets" : { | |
"country" : { | |
"terms" : { "field" : "country" } | |
} | |
} | |
} | |
search(query, function(response) { | |
console.log(response.hits.hits); | |
console.log('Facets:'); | |
console.log(JSON.stringify(response.facets, null, 2)); | |
console.log(response.took + 'ms / ' + response.hits.total + ' results'); | |
}); | |
function search(query, callback) { | |
query = JSON.stringify(query); | |
var headers = { | |
'Content-Length': query.length, | |
'charset': 'UTF-8', | |
'Content-Type': 'application/json' | |
}; | |
var request = client.request('GET', 'reliefweb/reports/_search', headers); | |
request.write(query, 'utf8'); | |
request.on('response', function(response) { | |
var body = ''; | |
response.on('data', function(data) { | |
body += data; | |
}); | |
response.on('end', function() { | |
callback(JSON.parse(body)); | |
}); | |
}); | |
request.end(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment