Filtering then grouping results

Hi all, lets see if I can explain this well enough…

Say I have an index:

[
  {
    "body": "some text....",
    "recipient": "1",
    "tags": ["tag 1", "tag 2"],
    "demographics": ["job title: CEO", "company size: 10-100", "city: San Francisco"],
    "ideas": ["idea 1", "idea 2"]
  },
  {
    "body": "some text....",
    "recipient": "1",
    "tags": ["tag 2"],
    "demographics": ["job title: CEO", "company size: 10-100", "city: San Francisco"],
    "ideas": ["idea 1"]
  },
  {
    "body": "some text....",
    "recipient": "2",
    "tags": ["tag 1"],
    "demographics": ["job title: CPO", "company size: 100-250", "city: San Francisco"],
    "ideas": ["idea 2"]
  },
  {
    "body": "some text....",
    "recipient": "3",
    "tags": ["tag 1", "tag 2"],
    "demographics": ["job title: CPO", "company size: 10-100", "city: Oakland"],
    "ideas": ["idea 2"]
  }
]

Say I then want to group the results by each tag, demographic, idea, or recipient.

Ultimately, we have three different UIs that we’d like to use this index for:

  • The Tags page shows a list of tags and some filters for ideas, and demographics.
  • The Demographics page shows a list of demographics and some filters for ideas, and tags.
  • The Ideas page shows a list of ideas and some filters for demographics, and tags.

When any of these pages are filtered, I’d like to show the number of recipients in each result.

For example:

When I filter the tags page down to just “job title: CPO”, I would want to see tag one and tag two listed, each with a recipient count of 1.

And when I filter the demographics page down to just “tag 1”, I would want to see “job title: CPO” with a count of 2, “job title: CEO” with a count of 1, etc.

Is this possible? If so, how?

Hi @dan5, there are different ways to do this. It depends on what you’re currently using.
If you’re using InstantSearch, you may get some of this functionality: as you can see on my GIF when I filter by a specific brand, I can still see the other brands in the list:


(codesandbox for this example: https://codesandbox.io/s/compassionate-burnell-p1rjm?file=/src/app.js)

What’s happening under the hood is the following:

when you click on Apple, InstantSearch performs 2 requests :

  1. one request with the Apple refinement applied, meaning filters: "brand: Apple"
  2. one request with the Apple refinement removed

The first request is used to populate the results, the second one is used to show the values with the correct count.

Do you think this mechanism can work for you? Did I understand your problem correctly?

Can’t wait to help!

That’s not quite what I’m looking for - I want the results themselves to be grouped, with a count on them that changes as filters are applied.

Hi @dan5,

I’m not sure that I completely understand what you mean, but would something like the ‘distinct’ parameter work for grouping?

You could then use the ‘facetingAfterDistinct’ parameter for your facet counts.

Would that work for your use case?