Permalink
Browse files

Non working. For some reason the elasticsearch require doesn't work. …

…Looking into it.

Signed-off-by: Nick Campbell <nicholas.j.campbell@gmail.com>
  • Loading branch information...
1 parent 0ac8b21 commit 60315c9e1b204f833b4dac80e7c0fc44f9fd511f @ncb000gt committed Mar 1, 2011
Showing with 40 additions and 11 deletions.
  1. 0 README.md
  2. +3 −3 index.js
  3. +36 −7 lib/elasticsearch.js
  4. +1 −1 lib/query.js
  5. 0 package.json
View
No changes.
View
@@ -1,5 +1,5 @@
-var es = require('./lib/elasticsearch'),
-query = require('./lib/query');
+var elasticsearch = require('./lib/elasticsearch'),
+ query = require('./lib/query');
-module.exports['search'] = es;
+module.exports['search'] = elasticsearch;
module.exports['query'] = query;
View
@@ -1,25 +1,54 @@
var http = require('http');
+function mapConfig(origin, mapper) {
+ for (var p in mapper) {
+ origin[p] = mapper[p];
+ }
+ return origin;
+}
+
function ElasticSearch(opts) {
- this.config = {
+ var def = {
host: 'localhost',
port: '9200'
};
if (!(opts.index)) {
throw new Error('Must specify an index for ES.');
}
- for (var p in opts) {
- this.config[p] = opts[p];
- }
+ this.config = mapConfig(def, opts);
}
module.exports = ElasticSearch;
-ElasticSearch.prototype.query = function(query, cb) {
- var options = {
+ElasticSearch.prototype.query = function query(opts, cb) {
+ var def = {
host: this.config.host,
port: this.config.port,
method: 'POST'
};
- http.request();
+ var query = opts.query;
+ delete opts.query;
+
+ var _config = mapConfig(def, opts);
+
+ var req = http.request(_config, function response_cb(res) {
+ var chunks = [],
+ length = 0;
+ res.on('data', function data_cb(chunk) {
+ chunks.push(chunk);
+ length += chunk.length; //can't reliably use headers
+ });
+ res.on('end', function end_cb() {
+ var buf = new Buffer(length),
+ total = 0;
+ chunks.forEach(function chunk_cb(chunk) {
+ chunk.copy(buf, total, 0);
+ total += buf.length;
+ });
+ console.log('data: ' + buf.toString());
+ });
+ });
+
+ req.write(JSON.stringify(query));
+ req.end();
}
View
@@ -4,7 +4,7 @@ function Query() {
module.exports = Query;
-Query.prototype.addField = function(name, field) {
+Query.prototype.addField = function addField(name, field) {
if (!(this.query.query.field)) {
this.query.query.field = {};
}
View
No changes.

0 comments on commit 60315c9

Please sign in to comment.