最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Algolia search query deformed inside Lambda function(using Serverless Framework) but works fine in Express - Stack

programmeradmin2浏览0评论

I'm getting the Algolia search request/query from the frontend into my Lambda function which then executes the request and returns the result. The format of the request is an array like

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

After that I search using their API client

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

Then the search query happens but I get 0 hits and when I console.log(results) the query field is deformed

{
  "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
}

but the results should be like below (This is what I get when I console.log on express server and I get the desired hits. Notice how it sends an object with results field and the query attribute contains only the searched text)

{ 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 } ] 
} 

My issue is why it console.log 2 different things on lambda and express. I'm sending in the same requests array and using the same algolia API search in both occasions.

I'm getting the Algolia search request/query from the frontend into my Lambda function which then executes the request and returns the result. The format of the request is an array like

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

After that I search using their API client

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

Then the search query happens but I get 0 hits and when I console.log(results) the query field is deformed

{
  "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
}

but the results should be like below (This is what I get when I console.log on express server and I get the desired hits. Notice how it sends an object with results field and the query attribute contains only the searched text)

{ 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 } ] 
} 

My issue is why it console.log 2 different things on lambda and express. I'm sending in the same requests array and using the same algolia API search in both occasions.

Share Improve this question edited Jan 21, 2022 at 14:50 cmgchess asked Jan 21, 2022 at 14:24 cmgchesscmgchess 10.3k41 gold badges53 silver badges69 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 1

Ok it was a careless mistake
my connectToIndex returns an Algolia index

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

and I have done the search using index.search(requests) which implies client.initIndex().search(requests)

But for searching you don't call initIndex instead you call the search method of the client directly

client.search(requests)

I had correctly used this in express and somehow messed up inside the lambda
Github issue

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论