|
| 1 | +--- |
| 2 | +html_meta: |
| 3 | + "description": "Using Plone's catalog to index content and make it searchable." |
| 4 | + "property=og:description": "Using Plone's catalog to index content and make it searchable." |
| 5 | + "property=og:title": "Indexing" |
| 6 | + "keywords": "indexing, indexes, indexer, catalog, searching, FieldIndex, SearchableText, textindexer" |
| 7 | +--- |
| 8 | + |
| 9 | +(backend-indexing-label)= |
| 10 | + |
| 11 | +# Indexing |
| 12 | + |
| 13 | +To make Plone content searchable, one can use different indexes to index content. |
| 14 | + |
| 15 | +The most important index types are: |
| 16 | + |
| 17 | +- `FieldIndex` |
| 18 | +- `KeywordIndex` |
| 19 | +- `DateIndex` |
| 20 | +- `DateRangeindex` |
| 21 | +- `BooleanIndex` |
| 22 | +- `ZCTextIndex` |
| 23 | + |
| 24 | +The most important indexes are: |
| 25 | + |
| 26 | +## `SearchableText` |
| 27 | + |
| 28 | +The `SearchableText` is a `ZCTextIndex` for indexing full text and is used by default for `Dublin core` fields like Title, Description and Text. |
| 29 | + |
| 30 | +### TextIndexer |
| 31 | + |
| 32 | +To add other fields to the `SearchableText`, one can use the `plone.app.dexterity.textindexer`. |
| 33 | + |
| 34 | +For enabling the indexer just add the behavior to the list of behaviors of your content types. |
| 35 | + |
| 36 | +In your *profiles/default/types/YOURTYPE.xml* add the behavior:: |
| 37 | + |
| 38 | + <?xml version="1.0"?> |
| 39 | + <object name="example.conference.presenter" meta_type="Dexterity FTI" |
| 40 | + xmlns:i18n="http://xml.zope.org/namespaces/i18n" |
| 41 | + i18n:domain="example.conference"> |
| 42 | + |
| 43 | + <!-- enabled behaviors --> |
| 44 | + <property name="behaviors" purge="false"> |
| 45 | + <element value="plone.textindexer" /> |
| 46 | + </property> |
| 47 | + |
| 48 | + </object> |
| 49 | + |
| 50 | + |
| 51 | +Now you need to mark the fields you want to have in your SearchableText. |
| 52 | +This can be done with the searchable directive: |
| 53 | + |
| 54 | +```python |
| 55 | +from plone.app.dexterity import textindexer |
| 56 | +from plone.supermodel.model import Schema |
| 57 | +from plone import schema |
| 58 | + |
| 59 | +class IMyBehavior(Schema): |
| 60 | + |
| 61 | + textindexer.searchable('specialfield') |
| 62 | + specialfield = schema.Text(title=u'Special field') |
| 63 | + |
| 64 | +``` |
| 65 | + |
| 66 | +If you want to mark fields of an existing 3rd party behavior, it can be done using this utility function: |
| 67 | + |
| 68 | +```python |
| 69 | +from plone.app.dexterity.behaviors.metadata import ICategorization |
| 70 | +from plone.app.dexterity.textindexer import utils |
| 71 | + |
| 72 | +utils.searchable(ICategorization, 'categorization') |
| 73 | +``` |
| 74 | + |
| 75 | +The `title` and `description` on `plone.app.dexterity`'s `IBasic` behavior are marked as searchable by default. |
| 76 | +For marking them as no longer searchable, there is a utility function: |
| 77 | + |
| 78 | +```python |
| 79 | +from plone.app.dexterity.behaviors.metadata import IBasic |
| 80 | +from plone.app.dexterity.textindexer import utils |
| 81 | + |
| 82 | +utils.no_longer_searchable(IBasic, 'title') |
| 83 | +``` |
| 84 | + |
| 85 | +Alternatively, if you specified your model as a plone.supermodel XML model, |
| 86 | +you can mark the field searchable by using `indexer:searchable="true"`: |
| 87 | + |
| 88 | +```xml |
| 89 | + |
| 90 | +<model xmlns="http://namespaces.plone.org/supermodel/schema" |
| 91 | + xmlns:indexer="http://namespaces.plone.org/supermodel/indexer"> |
| 92 | + <schema based-on="plone.supermodel.model.Schema"> |
| 93 | + |
| 94 | + <field name="specialfield" type="zope.schema.TextLine" |
| 95 | + indexer:searchable="true"> |
| 96 | + <title>Special field</title> |
| 97 | + </field> |
| 98 | + |
| 99 | + </schema> |
| 100 | +</model> |
| 101 | +``` |
| 102 | + |
| 103 | + |
| 104 | +Your SearchableText indexer includes now your custom field on your behavior. |
| 105 | +It is enabled on your content type, if the `plone.textindexer` behavior is enabled there too. |
| 106 | + |
| 107 | + |
| 108 | +#### Registering a custom field converter |
| 109 | + |
| 110 | + |
| 111 | +By default, a field is converted to a searchable text by rendering the widget in display mode and transforming the result to `text/plain`. |
| 112 | +However, if you need to convert your custom field in a different way, you only have to provide a more specific converter multi-adapter. |
| 113 | + |
| 114 | +Convert multi-adapter specification: |
| 115 | + |
| 116 | +:Interface: `plone.app.dexterity.textindexer.IDexterityTextIndexFieldConverter` |
| 117 | +:Discriminators: context, field, widget |
| 118 | + |
| 119 | +Example: |
| 120 | + |
| 121 | +```python |
| 122 | +from plone.app.dexterity.textindexer.converters import DefaultDexterityTextIndexFieldConverter |
| 123 | +from plone.app.dexterity.textindexer.interfaces import IDexterityTextIndexFieldConverter |
| 124 | +from my.package.interfaces import IMyFancyField |
| 125 | +from plone.dexterity.interfaces import IDexterityContent |
| 126 | +from z3c.form.interfaces import IWidget |
| 127 | +from zope.component import adapter |
| 128 | +from zope.interface import implementer |
| 129 | + |
| 130 | +@implementer(IDexterityTextIndexFieldConverter) |
| 131 | +@adapter(IDexterityContent, IMyFancyField, IWidget) |
| 132 | +class CustomFieldConverter(DefaultDexterityTextIndexFieldConverter): |
| 133 | + |
| 134 | + def convert(self): |
| 135 | + # implement your custom converter |
| 136 | + # which returns a string at the end |
| 137 | + return '' |
| 138 | +``` |
| 139 | + |
| 140 | +ZCML: |
| 141 | + |
| 142 | +```xml |
| 143 | +<configure xmlns="http://namespaces.zope.org/zope"> |
| 144 | + |
| 145 | + <adapter factory=".converters.CustomFieldConverter" /> |
| 146 | + |
| 147 | +</configure> |
| 148 | +``` |
| 149 | + |
| 150 | +There is already an adapter for converting files properly. |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | +#### Extending indexed data |
| 155 | + |
| 156 | + |
| 157 | +Sometimes you need to extend the SearchableText with additional data which is not stored in a field. |
| 158 | +It is possible to register a named adapter which provides additional data: |
| 159 | + |
| 160 | +```python |
| 161 | +from plone.app.dexterity import textindexer |
| 162 | +from zope.component import adapter |
| 163 | +from zope.interface import implementer |
| 164 | + |
| 165 | + |
| 166 | +@implementer(textindexer.IDynamicTextIndexExtender) |
| 167 | +@adapter(IMyBehavior) |
| 168 | +class MySearchableTextExtender(object): |
| 169 | + |
| 170 | + def __init__(self, context): |
| 171 | + self.context = context |
| 172 | + |
| 173 | + def __call__(self): |
| 174 | + """Extend the searchable text with a custom string""" |
| 175 | + return 'some more searchable words' |
| 176 | +``` |
| 177 | + |
| 178 | +ZCML: |
| 179 | + |
| 180 | +```xml |
| 181 | +<configure xmlns="http://namespaces.zope.org/zope"> |
| 182 | + |
| 183 | + <adapter factory=".indexer.MySearchableTextExtender" |
| 184 | + name="IMyBehavior" |
| 185 | + /> |
| 186 | + |
| 187 | +</configure> |
| 188 | +``` |
| 189 | + |
| 190 | +This is a **named** adapter! This makes it possible to register multiple |
| 191 | +extenders for the same object on different behavior interfaces. The name of |
| 192 | +the adapter does not matter, but it's recommended to use the name of the |
| 193 | +behavior (this may reduce conflicts). |
| 194 | + |
| 195 | +If your behavior has a defined factory (which is not attribute storage), then |
| 196 | +you need to define a marker interface and register the adapter on this marker |
| 197 | +interface (dexterity objects do not provide behavior interfaces of behaviors, |
| 198 | +which are not using attribute storage). |
| 199 | + |
| 200 | + |
| 201 | + |
| 202 | +## `portal_type` (`FieldIndex`) |
| 203 | + |
| 204 | +Indexes the `portal_type` field and contains values like `Folder`. |
| 205 | + |
| 206 | +## `path` (`PathIndex)` |
| 207 | + |
| 208 | +Indexes the object path, like `/news/news-item-1`. |
| 209 | + |
| 210 | +## `Subject` (`KeywordIndex`) |
| 211 | + |
| 212 | +Indexes the `Subject` field which contains a list of object categories. |
0 commit comments