将AWS Lambda与Elastic Search结合使用,从搜索客户端获取未定义
我在学习AWS平台的同时尝试学习Nodejs。将AWS Lambda与Elastic Search结合使用,从搜索客户端获取未定义
我正在构建一个使用AWS Elastic Search实例的Lambda函数的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函数?
我LAMBDA处理函数index.js:
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函数是异步的,它返回一个promise。
您的代码应该是这个样子:
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:
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);
});
});
}
我希望这有助于:)。
@Nltin VAJA,我认为这是正确的方向,但该代码是给我一个错误: 'index.js:16 。然后(结果=> { ^ 类型错误:search.searchQuestion(.. 。)然后是不是函数 在Object.exports.handler(index.js:16:10) 在Object。
请再次尝试上面的代码。此外,您可能也可以尝试此操作 - > return client.search ..然后应该返回该诺言。 –
@Nltin Vaja它做到了!谢谢你:) – astralbody888
你能记录事件内的数据吗?只是为了确定你的问题是定义的。除此之外,我会确保resp.hits.hits返回一个值,如果它不是 - 你在elasticsearch方面做了一些错误(可能没有正确建立索引),这看起来并不在范围内这个问题。 –
如果我理解正确,你的内容值应该是“”“”。我可能是错的。那是不确定的? –
@RamziC。好问题,但是当我从search.js中记录topHit的结果时,我得到了正确的JSON响应,所以它不是Elastic Search的问题,我认为这是我的代码的问题。 – astralbody888