How to retrieve the total count of records for a specific attribute without updating filters on the UI

Hello friend,

I’m making a web app that shows nearby business cards based on the user geo location, I’ve already implemented the search using the js api client directly but I have a few doubts regarding filtering/faceting.

This app will use city, region, country and others attributes which are all attributesForFaceting for filtering records.

My doubt is when using facets to get the count for a city, region or country, like so:

index.search({
    query: '',
    facets: ['city', 'region', 'country']
})

I will get an entire object for each of these facets which is the expected behavior, but for my case I’m using 2 instances of the same index, one for the search bar which saves the query and shows autocomplete results and the second which retrieves the actual records that get rendered in the UI when the app loads based on aroundLatLng and potentially from the previous saved query of the search bar.

The thing that I need is the total number count of records for each of my facets within a specific city, region and country derived from the geo location coordinates of the user which in this case it’s not the case since the facets object for each facet change based on what the user is typing in the search bar.

In other words, how my UI filters can kept this total number count of records static without being affected for each user keystrokes when using facets?

e.g.

When using:

filters: "city:'Medellin' AND region:'Antioquia' AND country:'Colombia'"

The above doesn’t work for me since the number of hits per filter is dynamic based on how many are in a particular attributesForFaceting

What should I do? should I get this numbers from the database? create more single purpose index instances in the app that solely deals with this?

Thank you

Hello @ahsath,

In this particular use case, I think it will be hard to have the facets count correct for all the facets.
I think the easiest would be:

  • do a different query
  • store the results in the browser so it won’t trigger again
    (or use Algoliasearchjs’ cache, not sure how well it works with geoloc).
  • update your UI, not sure what technoligy you are using but this is trivial in any case

This would result in 2 initial queries, but only 1 updating on each key strokes.

Best regards,

Hello @samuel.bodin

Thank you for your response.

Yes, that’s what I thought initially although that would mean that I have to create various instances of my index in the front end to get the numbers, don’t think it’s a good approach.

Think I’ll refactor my UI. BTW I’m using Vue