Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Algolia search 'query' deformed when used inside AWS lambda function #1382

Closed
cmgchess opened this issue Jan 21, 2022 · 3 comments
Closed

Algolia search 'query' deformed when used inside AWS lambda function #1382

cmgchess opened this issue Jan 21, 2022 · 3 comments

Comments

@cmgchess
Copy link

cmgchess commented Jan 21, 2022

Hello 👋
Ran into a weird issue today.
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 the 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. I assume 0 hits due to the query being 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 ideally the results should be like below (This is what I get when I console.log on an express server and I get the desired hits. Notice how it sends an object with a 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 } ] 
} 

Any idea what I did wrong 🤔 since I'm getting different results using the same method on lambda and express.

@Haroenv
Copy link
Contributor

Haroenv commented Jan 21, 2022

The first argument to index.search is the query, however you probably meant to do client.search(requests), not client.initIndex().search(query, params)

@cmgchess
Copy link
Author

Contributor

Oof
yeah that was the issue. I'm dumb
I had correctly used it in my express server and somehow forgotten it inside lambda

Thank you 🙏

@Haroenv
Copy link
Contributor

Haroenv commented Jan 21, 2022

no problem!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants