Algolia 搜索查询在 Lambda 函数中变形(使用无服务器框架),但在 Express 中工作正常

我从前端获取 Algolia 搜索请求/查询到我的 Lambda 函数中,然后执行请求并返回结果。请求的格式是一个数组,如

      [
        {
          "indexName": "indexname",
          "params": {
            "query": "querytext",
            "hitsPerPage": 7,
            "maxValuesPerFacet": 3,
            "page": 0,
            "facets": [
              "type"
            ],
            "tagFilters": "",
            "facetFilters": [
              "account_id:1"
            ]
          }
        }
      ]

之后我使用他们的 API 客户端进行搜索

      const index = connectToIndex(ALGOLIA_APP,ALGOLIA_KEY,INDEX_NAME);
      const results = await index.search(requests);

然后发生搜索查询,但我得到 0 次点击,当我 console.log(results) 时, query 字段变形

{
  "hits": [],
  "nbHits": 0,
  "page": 0,
  "nbPages": 0,
  "hitsPerPage": 20,
  "exhaustiveNbHits": true,
  "exhaustiveTypo": true,
  "query": "[{"indexName":"indexname","params":{"query":"querytext","hitsPerPage":7,"maxValuesPerFacet":3,"page":0,"facets":["type"],"tagFilters":"","facetFilters":["account_id:1"]}}]",
  "params": "query=%5B%7B%22indexName%22%3A%indexname%22%2C%22params%22%3A%7B%22query%22%3A%22querytext%22%2C%22hitsPerPage%22%3A7%2C%22maxValuesPerFacet%22%3A3%2C%22page%22%3A0%2C%22facets%22%3A%5B%22type%22%5D%2C%22tagFilters%22%3A%22%22%2C%22facetFilters%22%3A%5B%22account_id%3A1%22%5D%7D%7D%5D",
  "renderingContent": {},
  "processingTimeMS": 1
}

但是 results 应该如下所示(这是我在快速服务器上 console.log 时得到的结果,并且我得到了所需的命中。注意它如何发送带有 results 字段和 query 的对象属性仅包含搜索到的文本)

{ results:
   [ { hits: [Array],
       nbHits: 20,
       page: 0,
       nbPages: 3,
       hitsPerPage: 7,
       facets: [Object],
       exhaustiveFacetsCount: true,
       exhaustiveNbHits: true,
       exhaustiveTypo: true,
       query: 'querytext',
       params:
        'query=querytext&hitsPerPage=7&maxValuesPerFacet=3&page=0&facets=%5B%22type%22%5D&tagFilters=&facetFilters=%5B%22account_id%3A1%22%5D',
       index: 'indexname',
       renderingContent: {},
       processingTimeMS: 1 } ] 
} 

我的问题是为什么它在 lambda 和 express 上有 console.log 2 个不同的东西。我在相同的 requests 数组中发送并在两种情况下使用相同的 algolia API 搜索。

stack overflow Algolia search query deformed inside Lambda function(using Serverless Framework) but works fine in Express
原文答案
author avatar

接受的答案

好的,这是一个粗心的错误
我的 connectToIndex 返回Algolia索引

const connectToIndex = (appId,apiKey,index) => {
  const client = algoliasearch(appId,apiKey);
  return client.initIndex(index);
};

我已经使用 index.search(requests) 进行了搜索,这意味着 client.initIndex().search(requests)

但是要搜索您不调用 initIndex 而不是调用 search client 的方法直接调用

client.search(requests)

我在Express中正确使用了此功能,并以某种方式搞砸了Lambda
Github issue


答案: