将 AWS Lambda 与 Elastic Search 结合使用,从搜索客户端获取 Undefined

Using AWS Lambda with Elastic Search, getting Undefined from the search client

我正在尝试学习 Nodejs,同时学习 AWS 平台。

我正在构建一个将 Lambda 函数与 AWS Elastic Search 实例结合使用的 Lex 应用程序。

我的搜索是基本的,正在寻找它需要的东西,问题是当我测试我的处理程序时它没有收到数据。当我将结果记录到控制台时,搜索结果对象似乎直到处理程序打印出结果后才传回处理程序函数。使用一些控制台日志我得到这个输出:

Starting handler function
Starting search
{ dialogAction:
   { type: 'Close',
     fulfillmentState: 'Fulfilled',
     message: { contentType: 'PlainText', content: undefined  } } }
Top hit: [object Object]

内容项未定义,但应该是从 search.js 返回的热门对象。我可以看到正在找到 TopHit 对象,但为什么 index.handler 函数在返回搜索响应之前打印?

我在 index.js 中的 Lambda 处理程序函数:

const search = require("./search.js");
exports.handler = (event, context, callback) => {

console.log("Starting handler function");

const questionReq = event.currentIntent.slots.question;
//console.log(questionReq);
// call Exported function from search js.Pass in string as question
const results = search.searchQuestion(questionReq);

const eventResponse = {
    "dialogAction": {
    "type": "Close",
    "fulfillmentState": "Fulfilled",
    "message": {
      "contentType": "PlainText",
      "content": results
    }
  }
};

callback(null, eventResponse);
};

我在 search.js 中的弹性搜索:

const client = require('./connection.js');

exports.searchQuestion = function(question)
{
    var topHit = "";
    console.log("Starting search");
    client.search({
      index: 'library',
      type: 'dsa',
      body: {
        query: {
          match: { "q": question }
        },
      }
  }).then(function (resp){
      topHit = resp.hits.hits[0];
      console.log("Top hit: " + topHit);
  }, function(err){
      console.trace(err.message);
  })
    return JSON.stringify(topHit);
}

提前感谢您提出任何建议。

您的 searchQuestion 函数是异步的,它正在返回一个承诺。

您的代码应如下所示:

const search = require("./search.js");
exports.handler = (event, context, callback) => {
    console.log("Starting handler function");

    const questionReq = event.currentIntent.slots.question;
    //console.log(questionReq);
    // call Exported function from search js.Pass in string as question
    search.searchQuestion(questionReq)
        .then(result => {
            const eventResponse = {
                "dialogAction": {
                    "type": "Close",
                    "fulfillmentState": "Fulfilled",
                    "message": {
                        "contentType": "PlainText",
                        "content": results
                    }
                }
            };
          callback(null, eventResponse);
      });
};

您在 search.js 中的 Elastic Search:

const client = require('./connection.js');

exports.searchQuestion = function(question)
{
    return new Promise(function(resolve, reject) {
       var topHit = "";
       console.log("Starting search");
       client.search({
         index: 'library',
         type: 'dsa',
         body: {
           query: {
             match: { "q": question }
          },
        }
     }).then(function (resp){
         topHit = resp.hits.hits[0];
         return resolve(topHit);
     }, function(err){
         console.trace(err.message);
    });
  });
}

希望对您有所帮助:)。