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=[{"indexName":%indexname","params":{"query":"querytext","hitsPerPage":7,"maxValuesPerFacet":3,"page":0,"facets":["type"],"tagFilters":"","facetFilters":["account_id:1"]}}]",
"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=["type"]&tagFilters=&facetFilters=["account_id:1"]',
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.
CodePudding user response:
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
