Instantsearch stats

Hi there,

I am currently looking for a solution to get the statistics from Instantsearch, instead of simply displaying them using the Stats widget. I would like to be able to access the data directly as searches happen. As an example, I am imagining something like this:

<InstantSearch
   onSearchStateChange={(result) => this._setSearch(result.query)}
   onStatsChange={(stats) => doStuff(stats)}
   appId={config.search.appId}
   apiKey={config.search.apiKey}
   indexName={config.search.index}>
</InstantSearch>

Does anyone know of a solution for this?

Thank you,
Joshua

Is there a reason why you would prefer an event rather than getting the props? If you don’t want to use the stats widget, you can also do a custom connector

see:

My goal is to simply get the data, so if I can do that via props then that would work well. I am looking into the connector now.

1 Like

Could you perhaps show a working example of the connectStats connector that is able to capture the hits passed down via props? This is what I am currently trying without any results:

    const myStats = ({nbHits, processingTimeMS}) => {
      console.log(nbHits, processingTimeMS)
    };

    connectStats(myStats);

With connect stats, I am able to return jsx, but that is all. I have not been able to simply capture the data as of yet.

Does anyone have a solution for this? My goal is to be able to capture the total number of search results as the search as the search takes place.

Here’s an example using connectStats that is working: https://jsfiddle.net/vvoyer/b05Lf4af/1/

1 Like