Filter on a count of nested fields

Hi ! I’m trying to use Algolia to do some “complex” filter.
Let’s say I have records of persons who have seen a certain number of movies. I defined those records (structure can be changed if needed, this is a try I made) like so :

{
  "person_id": "some-person-id",
  "nb_movies_seen": 4,
  "next_movies_genre": [ // this would be defined by the person as the type of movie they want to see next
    "drama",
    "comedy"
  ],
  "past_movies": [ // this is a history of movies they have already seen, it could include other genre that those defined earlier
    { "genre": [ "drama", "horror" ] },
    { "genre": [ "drama" ] },
    { "genre": [ "horror" ] },
    { "genre": [ "comedy" ] }
  ],
  "objectID": "110062491"
}

First of all, I would like a “Next movies genre” facet, which will allow me to filter on what kind of movies people want to see next, which is pretty easy to add.

But then, and here is the complicated part, if I selected “drama” as next_movies_genre, I would like to answer the question “Which people have seen between 2 and 3 drama movies in the past ?”.

I also tried this structure :

{
  "person_id": "some-person-id",
  "nb_movies_seen": 4,
  "next_movies_genre": [ // this would be defined by the person as the type of movie they want to see next
    "drama",
    "comedy"
  ],
  "past_movies": {
    "drama": 2,
    "horror": 2,
    "comedy": 1
  },
  "objectID": "110062491"
}

I really struggle to know how to deal with it, and I’m not even sure this is possible to do with Algolia, so if anyone can help me with it that would be great!

Thanks!

Hello @laure,

Answering the kind of query “between 2 and 3” is only possible with numeric filtering which applies directly to attribute value. There are no ways to dynamically count the values inside a list: you need to maintain these count in the documents themselves and the count must be computed on your side, before pushing/updating the documents.

The query “for users who have drama in next_movie_genre keep the ones that have seen between 2 and 3 drama movies” could be answered with:

{
    "filters": "next_movie_genre:drama AND past_movies:2 TO 3"
}

Let me know if that helps.

Best.