function searchDocumentToES()

in infrastructure/function/src/searchLambdaFunction/index.js [26:60]


function searchDocumentToES(body, index, type, output, context) {
    const req = new AWS.HttpRequest(endpoint);
    req.method = 'POST';
    req.path += index + '/_search';
    req.region = esDomain.region;
    req.body = JSON.stringify(body);
    req.headers['presigned-expires'] = false;
    req.headers['Host'] = endpoint.host;
    req.headers['Content-Type'] = 'application/json';
    req.headers['Content-Length'] = req.body.length;
    const signer = new AWS.Signers.V4(req, 'es');
    const creds = new AWS.EnvironmentCredentials('AWS');
    signer.addAuthorization(creds, new Date());
    const client = new AWS.HttpClient();
    client.handleRequest(req, null, function(httpResp) {
      let body = '';
      httpResp.on('data', function (chunk) {
          body += chunk;
      });
      httpResp.on('end', function () {
          const res = {'Records': []};
          JSON.parse(body).hits.hits.forEach(h => {
              if(output.length === 0) {
                res.Records.push(h._source);
              } else {
                res.Records.push(pick(h._source, output));
              }
          });
          context.succeed({'status': 200, 'body': res});
          console.log(body);
      }, function(err) {
          console.error(err);
      });
    });
}