Skip to content

Commit beece81

Browse files
committed
Add indexing chapter
1 parent 9f5e687 commit beece81

File tree

5 files changed

+233
-0
lines changed

5 files changed

+233
-0
lines changed

docs/backend/content-types.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,8 @@ html_meta:
1010

1111
# Content Types
1212

13+
14+
```{todo}
15+
Describe how to add a content type (Python/XML) including FTI settings.
16+
Fields, Widgets, Vocabularies aso are descripted in detail in there own chapter and will be referenced frome examples here.
17+
```

docs/backend/fields.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ html_meta:
1111
# Fields
1212

1313

14+
1415
(backend-fields-schema-label)=
1516

1617
## schema

docs/backend/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ users-groups
2525
security
2626
workflows
2727
search
28+
indexing
2829
zodb
2930
../plone.api/index.rst
3031
plone-restapi

docs/backend/indexing.md

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

docs/classic-ui/forms.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ html_meta:
1010

1111
# Forms
1212

13+
```{todo}
14+
Describe how create forms with Plone's default form framework z3c.form.
15+
Fields, Widgets, Vocabularies aso are descripted in detail in there own chapter and will be referenced frome examples here.
16+
```

0 commit comments

Comments
 (0)