Skip to content. | Skip to navigation

Personal tools

Navigation

You are here: Home / Tips / Catalog, Index, Metadata

Catalog, Index, Metadata

儲存在系統的內容資料,想要篩選存取的話,通常要透過 Catalog 和 Index 提供的搜尋功能,全文搜尋的場合常用到 SearchableText 索引,預設 Dublin Core 欄位值會進入這個索引,最好善用這些內建機制。

兩大類作法: 1. 指定欄位值進入 SearchableText 索引 2. 建立新的索引

Indexes

plone/app/contenttypes/indexers.py SearchableText safe_unicode: id, title, description, text, subject

FieldIndex KeywordIndex: 在 @indexer 常遇到

  indexRow = self._index.get(entry, _marker)
 File "/home/marr/Plone/buildout-cache/eggs/z3c.relationfield/relation.py", line 79, in __cmp__
    return cmp(slef._sort_key(), other._sort_key())
AttributeError: 'str' object has no attribute '_sort_key'

從 portal_catalog Indexes 把它 clean 有改善

Document Items for Custom Portal Type

@indexer(IATDocument)
def portal_type(obj):
    if obj.__parent__.getId() == 'myfolder':
        return 'MyType'

Document Text for Custom String Keyword Index

@indexer(IATDocument)
def my_idx(obj):
    k,v = bs(obj.getText()).find('p', {'class': 'ct'}).text.split(': ')
    if k == u'Field_NAME': return v

Indexer 遇到 AttributeError 時,不會執行 index 設定,例如 ATContentTypes 使用 context.start() 和 context.end() 而不是 context.getStartDate() 和 context.getEndDate()。

以 Plone5 Event Item 的 Subject 欄位值為例,使用 Tuple 型別 String 元素格式儲存,但 subject 使用 Tuple 型別 Unicode 元素格式儲存。

>>> my-event-item.Title()
'\xe7\x8e\x8b\xe7\x88\xba\xe4\xb8\xad\xe6\x96\x87'
>>> my-event-item.title
u'\u738b\u723a\u4e2d\u6587'

>>> my-event-item.Subject()
('\xe7\x8e\x8b\xe7\x88\xba', 'WangYe')
>>> my-event-item.subject
(u'\u738b\u723a', u'WangYe')

>>> p_catalog = app.mysite.portal_catalog
>>> p_catalog.indexes()
['total_comments', 'Title', ... 'Subject']
>>> p_catalog.values()
[<PLexicon at /mysite/portal_catalog/htmltext_lexicon>,
 <PLexicon at /mysite/portal_catalog/plaintext_lexicon>,
 <PLexicon at /mysite/portal_catalog/plone_lexicon>]

Subject 採 Keyword 索引方式,顯示方式可參考 plone.app.layout/viewlets/keywords.pt 程式碼。

以 Plone4 Event Item (Archetypes) 的 Location 欄位為例,使用 String 型別儲存,但 Plone5 (Dexterity) 使用 Unicode 型別儲存。

# Plone4 Archetypes
>>> my-event-item.getLocation()
'\xe5\x8f\xb0\xe5\x8d\x97\xe5\xb8\x82'

# Plone5 Dexterity
>>> my-event-item.location
u'\u53f0\u5317\u5e02\u5357\u6e2f\u5340'

Indexing a Field of Subform: collective.z3cform.datagridfield

@indexer(IMember)
def school(obj):
    if not obj.education:
        return []
    return [ edu['school'] for edu in obj.education ]

Metadata

Metadata 是儲存在 Catalog 裡的資料,用來顯示搜尋結果的值。以

SearchableText 索引機制和 ElasticSearch 功能衝突?

Catalog

搜尋建立的文件