内容搜索字段值也有一个字段
Contentful search for field which value also has a field
例如,我的内容丰富的后端列出了多部电影,我只想查询 return 部恐怖电影,所以我按类别过滤。
0:fields{
name: "Some horror movie"
category: {sys: {…}, fields: {…}} // fields contains in this example: {name: "horror"}
}
请注意,category 本身就是一种内容类型,它有一个 fields 属性,其中只有一个 name 属性。
所以我想我可以通过执行以下操作来过滤它:
client.getEntries({
'content_type': 'movie',
'fields.category.fields.name[match]': 'horror'
})
这 return 是一个 400 错误,我真的不知道为什么或如何解决这个问题。
查询是正确的,但在对字段值进行筛选时,还需要对引用的内容类型进行筛选。您可以在此处阅读更多相关信息:https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters/search-on-references
如果将此添加到查询中,它应该会起作用。
client.getEntries({
'content_type': 'movie',
'fields.category.fields.name[match]': 'horror',
'fields.category.sys.contentType.sys.id': 'horror-content-type-id'
})
记得用恐怖内容类型的实际 ID 替换 'horror-content-type-id'
。
例如,我的内容丰富的后端列出了多部电影,我只想查询 return 部恐怖电影,所以我按类别过滤。
0:fields{
name: "Some horror movie"
category: {sys: {…}, fields: {…}} // fields contains in this example: {name: "horror"}
}
请注意,category 本身就是一种内容类型,它有一个 fields 属性,其中只有一个 name 属性。 所以我想我可以通过执行以下操作来过滤它:
client.getEntries({
'content_type': 'movie',
'fields.category.fields.name[match]': 'horror'
})
这 return 是一个 400 错误,我真的不知道为什么或如何解决这个问题。
查询是正确的,但在对字段值进行筛选时,还需要对引用的内容类型进行筛选。您可以在此处阅读更多相关信息:https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters/search-on-references
如果将此添加到查询中,它应该会起作用。
client.getEntries({
'content_type': 'movie',
'fields.category.fields.name[match]': 'horror',
'fields.category.sys.contentType.sys.id': 'horror-content-type-id'
})
记得用恐怖内容类型的实际 ID 替换 'horror-content-type-id'
。