var argv = require('optimist').argv;
var url = require('url');
var request = require('request');

module.exports = function(req, res) {
  try {
    if (req.url.toLowerCase().indexOf('/query') === 0) {
      var query = argv.es + '/_search?' + url.parse(req.url).query;
      console.log('http search!', query);
      res.setHeader('Access-Control-Allow-Origin', '*');
    
      var r = request.get(query);
      r.pipe(res);
      r.on('error', function(e) {
        console.log("http search ERROR:", e);
      });

      r.on('end', function() {
        console.log("http search OK");
      });
      return true;
    }

    res.writeHead(404);
    res.end('');

    console.log('unhandled request', req.url);
  } catch (e) {
    console.log(e.stack);
    res.writeHead(500);
    res.end('');
  }
}
var argv = require('optimist').argv;
var url = require('url');
var request = require('request');
var qs = require('querystring');

module.exports = function(req, res) {
  try {
    if (req.url.toLowerCase().indexOf('/query') === 0) {

      var params = qs.parse(url.parse(req.url).query);

      if (params) {
        if (params.sort) {
          params.sort = params.sort.replace(/[ \+]/g, ':');
        }

        if (params.fl) {
          params.fields = params.fl;
          delete params.fl;
        }

        if (typeof params.rows !== 'undefined') {
          params.size = parseInt(params.rows, 10);
        }
      }

      var query = argv.es + '/_search?' + qs.stringify(params);
      console.log('http search!', query);
      res.setHeader('Access-Control-Allow-Origin', '*');
    
      var r = request.get(query);
      r.pipe(res);
      r.on('error', function(e) {
        console.log("http search ERROR:", e);
      });

      r.on('end', function() {
        console.log("http search OK");
      });
      return true;
    }

    res.writeHead(404);
    res.end('');

    console.log('unhandled request', req.url);
  } catch (e) {
    console.log(e.stack);
    res.writeHead(500);
    res.end('');
  }
}
normalize query syntax for /query route