Searching on multiple-reference fields

The documentation for searching on references notes the following restriction:

You can only search on references on fields which link to a single entry. Fields which hold references to many entries or fields with references to assets are not supported.

To work around this we can fetch all entries, then filter by references within our application, but this is inefficient and precludes pagination.

Given this limitation, this knowledgebase article is misleading. It would be reasonable assumption that a Movie content type would allow multiple Actor references. However, the example provided would only work with a single-reference actors field.

Are there plans to remove this limitation in a future version?

5 Likes

Hi @matt.burtch,

Thatā€™s right. Need to think of a better exampleā€¦
Could you please share your case? Is searching by multiple-reference fields the only approach?

Hey @andrey,

Our current model includes Categories and Posts: posts can belong to multiple categories, and categories have additional content (e.g. description, images, etc). Fetching posts by category references becomes problematic due to the restriction.

Another example would be adding authors to posts, then finding posts by an author reference.

There are other approaches, but none as straightforward from a content-authoring standpoint. We could use an intermediate Text/List checkbox field whose hard-coded values match category slugs, but adding/renaming categories would result in discrepancies.

Hey @matt.burtch ,

Thanks for the explanation!
You listed one of the approaches which should address this problem:

Another example would be adding authors to posts, then finding posts by an author reference.

Finding by author is something we suggested in similar cases. The scenario itself looks more natural, from how itā€™s setup point of view: each entry is linked with its author > searching by author shows you all entries belong to certain author.

What do you think?

Hey @andrey, @matt.burtch,

Just tried what Andrey proposed and it works perfectly. But lets say I want to get the actors infos in the same request for the files so I donā€™t have to make a second request. Is it currently possible ?
The point would be to have an object like so:

{ // the actor object
  name: 'dany trejo',
  birth: '01/ 01/ 1900'
  movies: [ { // the related movies objects
      name: 'machete'
    }, {
      name: 'machete in space'
    }, ...
  ]
}

@andrey,

Iā€™m very confused by your response on Oct 20. @matt.burtch gave 2 examplesā€¦ in one example, there was an author/post relationship, and in the other example, there was a category/post relationship. A post is likely to only have one author, but it could have multiple categories. I have the exact same situation where I want to search by category, where category is a multiple-reference field. Can you confirm that this is impossible, or is there some way around this limitation?

@matt.burtch @andrey

I agree, super misleading. Basically, in the example Matt points out, if the ā€œactorsā€ field on a ā€œMovieā€ is a multi-reference, instead of a single reference, the api would error out with the following message:

You are trying to search on a reference to an unsupported type. You can only search on references to single entries

However, one thing I would point outā€¦ if you actually know the ID of the Actor, you can still search for on multi-reference fields by id.

Meaning, if all youā€™re trying to do is find all Movies that Danny Trejo was in, and you know what his ID is, you could adapt the query to the following, which should work:

 https://cdn.contentful.com/spaces/my-space-id/entries/
     ?content_type=movies
     &fields.actors.sys.id={dannys_id_here}
6 Likes

Another idea to consider is to add an intermediate content type between Movie and Actor; call it MovieActor. This content type consists of a single reference to a Movie and a single reference to an Actor. With this type of hierarchy it is possible to search for all MovieActor entries which reference Danny Trejo and the resulting API response will include all the movies. Think of it as a join table for content types. In this situation, the Actor content type as well as the Movie content type both would not have references to the other.

This is certainly not a recommended approach for most situations and would be quite cumbersome in the editorial experience, but we have had some customers use this successfully.

Hi, did anyone find a solution to this issue? It seems strange that we cant search with multi references.

2 Likes

As @jason1 suggested, you can search for multi-reference fields by id.
Here is a code example to search all blog articles with the ā€œbillingā€ tag:

    async asyncData ({ env, params, error }) {
      let tag = await client.getEntries({
        'content_type': 'tags',
        'fields.slug': 'billing'
      })

      if (!tag.total) return error({ statusCode: 404, message: 'Tag not found' })

      let posts = await client.getEntries({
        'content_type': 'blogPost',
        'fields.tags.sys.id': tag.items[0].sys.id
      })

      return {
        posts: posts.items,
        tag: tag.items[0]
      }
    }
2 Likes

Took me a while to find this post. The linked ā€œFilter API Resultsā€ documentation did not help me understand this limitation.

Unfortunately, this is a bit of a deal-breaker for me. I donā€™t want to have to make multiple queries just to get a filtered result - thatā€™s unnecessary overhead.

Only option I see is to start a lengthy process of switching to a different data source backend that supports this functionality. Itā€™s absolutely integral to our app. Thanks for the info, community peeps! Saved me a lot of time and headaches.

4 Likes

Yes this seems like a very necessary feature. Filtering is a hacky solution because you can go through a huge list just to get the little info you need.

3 Likes

The mentioned document on filtering results with IMDB is really misleading. It takes multiple trials and errors to realize youā€™ll not be able to do this and finally find this thread.

Iā€™m pretty disappointed it is not possible. It should be either added or mentioned more clearly in filtering documentation, that it is not possible.

It may affect project architecture to noticeable extend and developers shouldnā€™t find out about this from trial and error.

3 Likes

Any news regarding this feature? Are there any plans to implement it? I agree with OP and the others, being able to search on multi-reference fields would make things much easier!

This could really be trivial trying to filter entry/s through multiple reference fields. Got the same problem here where I have a Blog entry that has a tags field linked to multiple Tag entry.

It seems that the CT have no intention of allowing this type of query yet and it leaves us here hangingā€¦ trying to figure out a workaround for this problem.

Conclusion: there are workarounds for this but not ideal.

I think that @lionel & @charlieā€™s idea are the least adequate solutions to this problem.

  • lionelā€™s: get the sys.id of target reference entry(tag of the entry e.g. life) and execute another query to the target collection(e.g. Blogs) accompanied by a filter of the retrieved sys.id

    Downsides:

    1. most certainly, youā€™ll need to execute one more query prior to the main query
  • charlieā€™s: create a separate intermediary Collection(e.g. BlogRecords) that has single reference on both tag and blog entry. This is to achieved the single query approach to retrieved all blogs belonged to a certain tag.
    Downsides:

    1. Creating a separate collection to solve this would also mean that you have to update that collection every so often.
    2. Itā€™d be a tedious work to update that specific collection each time an update is made on either Blog or Tag entries

We donā€™t knowā€¦ there might be some discussed topics about this on contentful documentation about how we should model our collection so that we donā€™t encounter this problem

poor usšŸ˜­

Hi. Iā€™ve fallen into this trap as well. I have Services with multiple Locations and multiple LocationTypes. It would have been so easy if the query allowed a search on multiple reference fields.

I know nothing about GraphQL. Can I ask, is this possible with the GraphQL instead?

1 Like

I have hit the same roadblock using GraphQL.

1 Like

Is there any plan on the Contentful side to implement this? This seems like a very common requirement and Iā€™m wondering if this is on the roadmap. Given that this is one of the most viewed threads in the community, I imagine this should be a priority, but it doesnā€™t appear thereā€™s been an update from Contentful and whether this is even being considered.

1 Like

Hi

Is there any update on searching many to many. I have also hit this roadblock. I want to search many categories for many products etc and have all the sorting and pagination taken care of. Very surprised this is not possible.

Is this is the roadmap?

Thanks

Hi,

we also have a roadblock here.
We have blog-posts and they can have multiple topic-tags(references to topic-tag content type) and we need to filter by 1 or more topic-tags.

Is there any update on this feature? Itā€™s so essential.