How to exclude non matching nested attributes?

Hello guys. I’m new to Algolia. I have a List indice with a messages attribute.

Example data set:

{
  subject: “Food List”
  messages: [
    {
      id: 1
      date: “3 days ago”
      body: "Indescribable oppression, which seemed to generate in some unfamiliar part of her consciousness,"
    },
    {
      id: 2
      date: “2 days ago”
      body: "Indescribable oppression, at the person in the train stopped at the station going in the opposite direction. She sat staring ahead"
    },
    {
      id: 3
      date: “1 day ago”
      body: "Both trains began to move and he knew that in another timeline or in another universe, they had been happy together."
    }
  ]
}

restrictSearchableAttributes is set to messages.body

If I search "Indescribable oppression", I would like to get the Food List, but only the very first message (id: 1) should be included in the messages, while the rest of the messages (id: 2, id: 3) should be excluded.

What setting or rule should I configure?

Hi @jibril_4 In my knowledge, that is not possible because algolia can only limit the returned attributes, it cannot limit if its an array and you need the first element.

I would recommend you change your data structure in the following way if possible.


[{
  subject: “Food List”
  message: {
      id: 1
      date: “3 days ago”
      body: "Indescribable oppression, which seemed to generate in some unfamiliar part of her consciousness,"
  }
},{
  subject: “Food List”
  message: {
      id: 2
      date: “3 days ago”
      body: "some other body,"
  }
} ]

2 Likes

Thanks for the helping me out harshmaur! Sure thing, I’ll see what i can do to change the data structure!

1 Like