2

I have an elasticsearch Server which I would like to query, but before showing the result to the User I want to filter the results (looking up the rights for the User in a Database etc.)

So I thought I write a proxy Server which recieves JSON POST Search request and redirect this to the Elasticsearch Server. The response with the results now be sent to the "filter server". This Server looks the recieved json-data up in a Database and removes the results which the User isn't allowed to see. This filtered content should be presented to the user.

Ok - this is what i've done:

var proxy = http.createServer(function (req, res){
  if(req.method == 'OPTIONS'){
   res.writeHead(200, {'Access-Control-Allow-Origin': '*', 'Content-Type':   'application/json; charset=UTF-8'});
   res.end();
 }

if(req.method == 'POST'){

  var searchOptions = {
    host: '10.0.10.1',
    port: 9200,
    method: 'POST',
    path: '/ltg_5096/_search'
  }

    var searchRequest = http.request(searchOptions, function(searchResponse){

    // this is the Request to the Elasticsearch Server...

    var filterOptions = {
      host: '127.0.0.1',
      port: 8080,
      method: 'POST',
      path: '/',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    }

    var filterRequest = http.request(filterOptions, function(filterResponse){
      // ?! This should be the request to the filter Server
    })

    searchResponse.pipe(res)
    res.writeHead(200, {'Access-Control-Allow-Origin': '*', 'Content-Type': 'application/json; charset=UTF-8'})
  })
  req.pipe(searchRequest)
 }
})

proxy.listen(9000)

This is the proxy Server but without the part where the results are filtered by the filtering instance. I tried lots of things, but couldn't get it to work as I want it to. I hope somebody can help me with this!

4

1 回答 1

5

这就是你需要的:

https://github.com/lukas-vlcek/node.es

这是一个简单但有用的elasticsearch代理,建立在node.js上,代理允许拦截和修改请求和响应,通过定义两个函数:

var preRequest = function(request) {};
var postRequest = function(request, response, responseData){};
var proxyServer = proxyFactory.getProxy(preRequest, postRequest);
proxyServer.start();

看看这个例子:

https://github.com/lukas-vlcek/node.es/blob/master/proxy/proxy-example.js

于 2013-08-02T04:33:24.683 回答