我正在尝试使用
@search
或 @querystring-search
端点来限制响应以仅包含 priority.token
= 1 的项目。
一个项目包含一个
priority
对象,如下所示:
"priority": {
"title": "1 Important",
"token": "1"
}
使用
@search
端点,我尝试添加 priority.token=1
,但这导致了此错误:
"Query for index <FieldIndex at priority> is missing a 'query' key!"
那么,是否可以使用对象内部的值来过滤结果?以及如何做到这一点?
假设,优先级是一个字典或 JSON 字段,您需要一个仅保存您感兴趣的值的索引。为此,您可能需要一个索引器,它提取名为“priority_token”的索引的令牌值。
ponecli 可以帮助您创建后端插件和索引器,或者手动:在 zcml 寄存器中:
<adapter factory=".priority_token.dummy"
name="priority_token" />
<adapter factory=".priority_token.priority_token"
name="priority_token" />
在Python中
priority_token.py
:
from mycustom.contenttypes.content.my_document import IYourDocument
from plone.dexterity.interfaces import IDexterityContent
from plone.indexer import indexer
@indexer(IDexterityContent)
def dummy(obj):
"""Dummy to prevent indexing other objects thru acquisition"""
raise AttributeError("This field should not indexed here!")
@indexer(IYourDocument)
def priority_token(obj):
"""Calculate and return the value for the indexer"""
return obj.priority.get('token')
对于 JSON 字段,您需要先将其转换为字典。
现在你可以像这样注册索引了: 在
./profiles/default/catalog.xml
:
<?xml version="1.0"?>
<object name="portal_catalog" xmlns:i18n="http://xml.zope.org/namespaces/i18n">
<index name="priority_token" meta_type="FieldIndex">
<indexed_attr value="priority_token" />
</index>
</object>