searchForFacetValues is not filtering properly

I have an index containing a field with an object with shape { title: String, type: String }. I am trying to make separate queries with searchForFacetValues to create a custom sorting for the field.

My request is shown below:

    facetName: 'tracks.title',
    facetQuery: '',
    maxFacetHits: 100,
    filters: `tracks.type:'Major'`,

The issue is that the response doesn’t contain ONLY the names of the tracks which has type:Major, it is returning tracks of other types too.

Is this the intended behaviour or is there a way to fix this?

Hey Minoru,

Thanks for reaching out to us. I’m Dorian, software engineer at Algolia.

From what I understand from the structure of your data, you have an array called tracks in each of your records and you want to filter on only some tracks.

If that is correct I think this discussion can help you I think this thread can help you, especially the last answer by Léo.

The gist of it is when you have an array in each record, if your array contains at least one value matching the filter, the entire record is returned because the filtering is done at the record level not at the level of your array. Why? essentially because we do not make any assumption about the structure of your data, therefore we can’t return only a part of it.

As explained by Léo in the thread I would advise you instead to create one record per different track. You will then have the level of granularity you desire.

I hope this helps, don’t hesitate to ask further questions.

Happy coding!

1 Like

Thanks for the clarification! Unfortunately, creating one record per track doesn’t really work for my use case.