Facet filters with array attribute

Hi,

I’m a rookie in algolia usage; so far I used its very nice plug and play features.

Now I changed my records and I have different kinds of data. In particular I have an attribute that is an array, so with multiple values at the same time. Here is an excerpt:

{
  "hits": [
    {
      "categories": "[\"Tablets\", \"Computers\"]",
      "objectID": "2",
      ...
    },
    {
      "categories": "[\"Tablets\"]",
      "objectID": "1",
      ...
  ],
  "nbHits": 2,
  "page": 0,
  "nbPages": 1,
  "hitsPerPage": 10,
  "processingTimeMS": 1,
  "facets": {
    "categories": {
      "[\"Tablets\", \"Computers\"]": 1,
      "[\"Tablets\"]": 1
    }
  }
  ...
}

What I want is filtering results based on the content of the attribute “categories”. So I put it as an attribute for faceting in the algolia dashboard (in general I use the algoliasearch-helper-js): what I got is that I have one result for each array in my records, as you can see in the “facets” attribute in the json above. It seems to me that algolia sees each array attribute as a single value, at least for facets; instead, in this example, I would like to obtain both records when I filter results based on the word “Tablets”, given that both arrays in the corresponding records have this value.

I did the search on the internet but I didn’t find a solution for this problem; hope that you can help me. Thanks in advance and let me know if I was not clear in explaining my problem or if I missed some details that were necessary to answer this question.

Hi @it1,

Thanks for contacting us. I can see from your excerpt that there is a mistake in your json data. If you take a close look at “categories”: "[“Tablets”, “Computers”]", the " makes the categories a string and not an array. Fixing that will solve your issue!

Guy

1 Like

Hi Guy,

thanks a lot for your help, I didn’t really notice that mistake. Now I fixed it and it works as you said.

Thank you again for your time.

1 Like

@it1Great to hear that :slight_smile: Anytime!

Hello - I’m running into this same issue. I can’t filter against an array of strings - basically, I want to exclude hits if they match the array of values I’m passing. eg: filterArray = [1,2], hits have values 1,2,3,4,5, only return 3,4,5. But I can’t for the life of me get this to work following this guide: https://www.algolia.com/doc/guides/searching/filtering/?language=instantsearchjs#filter-by-an-array-of-values. I’m using JSON.stringify, but still getting this error:

filters: Unexpected token string(d95e5ae7-eb5e-4bd0-92f7-1bc6df527f80) expected end of filter at col 161

This is the configure query:

<Configure
distinct={1}
filters={NOT "algolia_candidate.algolia_current_job.job_company_name": "${this.props.active_user.recruiter.recruiter_organization.company.name}" AND NOT "algolia_job.job_company_name": "${this.props.active_user.recruiter.recruiter_organization.company.name}" AND NOT "algolia_candidate.id": ${JSON.stringify(this.props.active_user.recruiter.skipped_candidates)}}
facetingAfterDistinct
/>

Please help!

Hi @patrick2, welcome on Algolia forums!

Your situation is a bit different: the OP wanted to filter records on an array attribute with a single value, while you’re trying to filter records on a string attribute with an array of values.

In your case, the recommended way is to use a negative filter for each value:
filters: 'NOT value:1 AND NOT value:2'

Let me know if this helps!

1 Like