algolia / algoliasearch-client-javascript

⚡️ A fully-featured and blazing-fast JavaScript API client to interact with Algolia.

Home Page:https://www.algolia.com/doc/api-client/javascript/getting-started/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

is it possible to set `responseFields` with `algoliasearch(APP,KEY).search(requestsObject)`

cmgchess opened this issue · comments

https://codepen.io/cmgchess/pen/oNGmqWE

is it possible to set the response field via this setup. the requests is what is generated and sent from the algoliasearch-helper library

const algoliasearch = require('algoliasearch');
const client = algoliasearch(ALGOLIA_APP,ALGOLIA_KEY);
const responseFields = ['hits','facets','nbHits','page','nbPages'];

//inside async function 
results = await client.search(requests,{responseFields}); //didn't work

the docs show it for the initIndex and then search but not for the above setup in https://www.algolia.com/doc/api-reference/api-parameters/responseFields/

is there a way to set responseFields for the above setup or should i write my own logic to remove fields

responseFields is a search parameter, so it's part of requests[number].params. I don't know what your requests array is, but inside it you need to add responseFields to the params object.

algoliasearch('','').search([{indexName, params: {responseFields: []}}])

ah so when i select filters then the requests array has multiple elements. (https://codepen.io/cmgchess/pen/oNGmqWE) so if i add the responseFields to each element it should work?
or is it enough to add it only to the 0th

example skeleton thats being send

{
    "requests": [
        {
            "indexName": "bestbuy",
            "params": {
                "query": "apple",
                "hitsPerPage": 7,
                "maxValuesPerFacet": 3,
                "page": 0,
                "facets": [
                    "category"
                ],
                "tagFilters": "",
                "facetFilters": [
                    [
                        "category:Cell Phone Cases & Clips",
                        "category:iPad Cases, Covers & Sleeves"
                    ]
                ]
            }
        },
        {
            "indexName": "bestbuy",
            "params": {
                "query": "apple",
                "hitsPerPage": 1,
                "maxValuesPerFacet": 3,
                "page": 0,
                "attributesToRetrieve": [],
                "attributesToHighlight": [],
                "attributesToSnippet": [],
                "tagFilters": "",
                "analytics": false,
                "clickAnalytics": false,
                "facets": "category"
            }
        }
    ]
}

Every index in the requests array is its own "search query", so you indeed pass it to every query. If you want to set the responseFields globally, you can do that with an index setting too.

one last question
any idea why it creates multiple queries with the helper when i add facets. since i can see every facet filter that is being set via the facetFilters of the 0th element
can i limit it to just 1 or is this how it behaves

When you use the helper and have a disjunctive refinement, it sends that second query to get the correct facet counts. If you look at the results of the first facet, you'll see that they are conjunctive (only the facets remaining in that result set), and not disjunctive as requested. That's what the second query is for.

As you're using the helper, you can also use helper.setQueryParameter('responseFields', [...]) and don't need to patch the queries sent