Welcome, guest | Sign In | My Account | Store | Cart

Notice! PyPM is being replaced with the ActiveState Platform, which enhances PyPM’s build and deploy capabilities. Create your free Platform account to download ActivePython or customize Python with the packages you require and get automatic updates.

pypm install z3c.multifieldindex

How to install z3c.multifieldindex

  1. Download and install ActivePython
  2. Open Command Prompt
  3. Type pypm install z3c.multifieldindex
 Python 2.7Python 3.2Python 3.3
Windows (32-bit)
3.4.0 Available View build log
Windows (64-bit)
3.4.0 Available View build log
Mac OS X (10.5+)
3.4.0 Available View build log
Linux (32-bit)
3.4.0 Available View build log
Linux (64-bit)
3.4.0 Available View build log
ZPL 2.1
Lastest release
version 3.4.0 on Jan 5th, 2011


This package provides an index for zope catalog that can index multiple fields. It is useful in cases when field set are dynamic (for example with customizable persistent fields).

Actually, this package provides a base class for custom multi-field indexes and to make it work, you need to override some methods in it. But first, let's create a content schema and interface we will use:

>>> from zope.interface import Interface, implements
>>> from zope.schema import Text, Int, List, TextLine
>>> class IPerson(Interface):
...     age = Int()
...     info = Text()
...     skills = List(value_type=TextLine())
>>> class Person(object):
...     implements(IPerson)
...     def __init__(self, age, info, skills):
...         self.age = age
...         self.info = info
...         self.skills = skills

Let's create a set of person objects:

>>> dataset = [
...     (1, Person(20, u'Sweet and cute', ['dancing', 'singing'])),
...     (2, Person(33, u'Smart and sweet', ['math', 'dancing'])),
...     (3, Person(6, u'Young and cute', ['singing', 'painting'])),
... ]

We have choose exactly those different types of fields to illustrate that the index is smart enough to know how to index each type of value. We'll return back to this topic later in this document.

Now, we need to create an multi-field index class that will be used to index our person objects. We'll override two methods in it to make it functional:

>>> from z3c.multifieldindex.index import MultiFieldIndexBase
>>> from zope.schema import getFields
>>> class PersonIndex(MultiFieldIndexBase):
...     def _fields(self):
...         return getFields(IPerson).items()
...     def _getData(self, object):
...         return {
...             'age': object.age,
...             'info': object.info,
...             'skills': object.skills,
...         }

The "_fields" method should return an iterable of (name, field) pairs of fields that should be indexed. The sub-indexes will be created for those fields.

The "_getData" method returns a dictionary of data to be indexed using given object. The keys of the dictionary should match field names.

Sub-indexes are created automatically by looking up an index factory for each field. Three most-used factories are provided by this package. Let's register them to continue (it's also done in this package's configure.zcml file):

>>> from z3c.multifieldindex.subindex import DefaultIndexFactory
>>> from z3c.multifieldindex.subindex import CollectionIndexFactory
>>> from z3c.multifieldindex.subindex import TextIndexFactory
>>> from zope.component import provideAdapter
>>> provideAdapter(DefaultIndexFactory)
>>> provideAdapter(CollectionIndexFactory)
>>> provideAdapter(TextIndexFactory)

The default index factory creates zc.catalog's ValueIndex, the collection index factory creates zc.catalog's SetIndex and the text index factory creates zope.index's TextIndex. This is needed to know when you'll be doing queries.

Okay, now let's create an instance of index and prepare it to be used.

>>> index = PersonIndex()
>>> index.recreateIndexes()

The "recreateIndexes" does re-creation of sub-indexes. It is normally called by a subscriber to IObjectAddedEvent, provided by this package, but we simply call it by hand for this test.

Now, let's finally index our person objects:

>>> for docid, person in dataset:
...     index.index_doc(docid, person)

Let's do a query now. The query format is quite simple. It is a dictionary, where keys are names of fields and values are queries for sub-indexes.

>>> results = index.apply({
...     'skills': {'any_of': ('singing', 'painting')},
... })
>>> list(results)
[1, 3]
>>> results = index.apply({
...     'info': 'sweet',
... })
>>> list(results)
[1, 2]
>>> results = index.apply({
...     'age': {'between': (1, 30)},
... })
>>> list(results)
[1, 3]
>>> results = index.apply({
...     'age': {'between': (1, 30)},
...     'skills': {'any_of': ('dancing', )},
... })
>>> list(results)


3.4.0 (15-10-2009)
  • Initial release (using Zope 3.4 dependencies).

Subscribe to package updates

Last updated Jan 5th, 2011

Download Stats

Last month:1

What does the lock icon mean?

Builds marked with a lock icon are only available via PyPM to users with a current ActivePython Business Edition subscription.

Need custom builds or support?

ActivePython Enterprise Edition guarantees priority access to technical support, indemnification, expert consulting and quality-assured language builds.

Plan on re-distributing ActivePython?

Get re-distribution rights and eliminate legal risks with ActivePython OEM Edition.