[Zope-CVS] CVS: Products/FieldedTextIndex - README.txt:1.3
index.py:1.5 test.py:1.4
Casey Duncan
casey at zope.com
Fri Dec 12 00:09:26 EST 2003
Update of /cvs-repository/Products/FieldedTextIndex
In directory cvs.zope.org:/tmp/cvs-serv15356
Modified Files:
README.txt index.py test.py
Log Message:
Implement UniqueValueIndex interface as a way for app code to get at the searchable field names. Add tests.
Add sample query forms to docs
=== Products/FieldedTextIndex/README.txt 1.2 => 1.3 ===
--- Products/FieldedTextIndex/README.txt:1.2 Wed Dec 10 00:03:05 2003
+++ Products/FieldedTextIndex/README.txt Fri Dec 12 00:09:26 2003
@@ -7,7 +7,7 @@
What problems does it solve?
In Zope sites it is common to have many different types of content objects
- whos data is stored in various attributes (or fields) in the object.
+ whose data is stored in various attributes (or fields) in the object.
Schema driven content types are also becoming more and more common making
it easier to create myriad content types with different data fields in
a single site.
@@ -17,7 +17,7 @@
'SearchableText' which aggregates the content fields into a single
source which can be fed to a text index. Although this works well for
a simple search across all textual fields of the objects in the site,
- you cannot narrow the search to specifc fields, you can only search
+ you cannot narrow the search to specific fields, you can only search
all fields using the SearchableText index.
The obvious solution to this is to create new text indexes for each field
@@ -84,7 +84,7 @@
and returns the requisite mapping, you can index those fields using the
index.
- The above script doesn't really take advantage of the full capabilites of
+ The above script doesn't really take advantage of the full capabilities of
the index, however, since every object has the same indexed fields. The
real power of the index is in its ability to index an unlimited number of
different fields of different objects which have arbitrary schemas. A
@@ -118,3 +118,48 @@
This would return only objects where the query terms occurred in the
fields 'Title' or 'Description'.
+ Creating a query form
+
+ Queries can also be generated directly from the web request like other
+ indexes. A query string or post-data can provide the query data structure
+ by using Zope's 'record' marshaling. Here is an example which lets
+ you search any combination of 'Title', 'Description' or 'Creator'::
+
+ <form action="search_results">
+ <input name="SearchableFields.query:record" /><br />
+ <div tal:repeat="name python:('Title', 'Description', 'Creator')">
+ <input type="checkbox" name="SearchableFields.fields:record:list"
+ tal:attributes="value name; id name;" />
+ <label tal:attributes="for name" tal:content="name">Name</label>
+ </div>
+ <input type="submit" />
+ </form>
+
+ Note that 'fields' must always be a list, hence the ':list' at the end
+ of the checkbox names. The 'search_results' template can use a standard
+ ZCatalog query, which simply calls ZCatalog passing it the web request
+ formatting the result set as desired.
+
+ You can also determine the names of the fields that the index has
+ encountered by using ZCatalog's 'uniqueValuesFor()' method. Here is
+ a variation of the form which automatically creates checkboxes for all of
+ the searchable fields::
+
+ <form action="search_results"
+ tal:define="fields python:here.portal_catalog.uniqueValuesFor('SearchableFields')">
+ <input name="SearchableFields.query:record" /><br />
+ <select name="SearchableFields.fields:record:list" multiple="multiple">
+ <option
+ tal:repeat="name fields"
+ tal:attributes="value name"
+ tal:content="name">Name</option>
+ </select><br />
+ <input type="submit" />
+ </form>
+
+ Conclusion
+
+ I hope you find this software this software. If you have a question,
+ comment, feature request or find a bug please contact me at casey at zope.com.
+
+Copyright (c) 2003, Casey Duncan and Zope Corporation
=== Products/FieldedTextIndex/index.py 1.4 => 1.5 ===
--- Products/FieldedTextIndex/index.py:1.4 Thu Dec 11 22:27:58 2003
+++ Products/FieldedTextIndex/index.py Fri Dec 12 00:09:26 2003
@@ -20,7 +20,7 @@
from Persistence import Persistent
from OFS.SimpleItem import SimpleItem
from Products.PluginIndexes.common.PluggableIndex \
- import PluggableIndexInterface
+ import UniqueValueIndex
from Products.PluginIndexes.common.util import parseIndexRequest
from AccessControl import Permissions
from AccessControl.SecurityInfo import ClassSecurityInfo
@@ -38,7 +38,7 @@
class FieldedTextIndex(OkapiIndex, SimpleItem):
"""Multiple field text index"""
- __implements__ = PluggableIndexInterface
+ __implements__ = UniqueValueIndex
meta_type = 'Fielded Text Index'
@@ -285,6 +285,19 @@
wids = WidCode.decode(words)
result[self._fields[fieldid]] = [get_word(wid) for wid in wids]
return result
+
+ def hasUniqueValuesFor(self, name):
+ return name == self.source_name
+
+ def uniqueValues(self, name=None, withLengths=0):
+ # Return the field names of everything we have indexed
+ # withLengths is ignored
+ if name is None or name == self.source_name:
+ names = self.fieldNames()
+ names.sort()
+ return names
+ else:
+ return []
## ZCatalog ZMI methods ##
=== Products/FieldedTextIndex/test.py 1.3 => 1.4 ===
--- Products/FieldedTextIndex/test.py:1.3 Thu Dec 11 22:27:58 2003
+++ Products/FieldedTextIndex/test.py Fri Dec 12 00:09:26 2003
@@ -239,7 +239,17 @@
self.assert_(self.index.getEntryForObject(1) is None)
stufs = []
self.assert_(self.index.getEntryForObject(1, stufs) is stufs)
-
+
+ def test_uniqueValues(self):
+ self.index_one(1)
+ self.index_two(2)
+ self.assertEqual(self.index.uniqueValues('fields'),
+ ['clyde', 'izzy', 'title'])
+
+ def test_hasUniqueValuesFor(self):
+ self.failUnless(self.index.hasUniqueValuesFor('fields'))
+ self.failIf(self.index.hasUniqueValuesFor('frootloops'))
+
def test_suite():
return TestSuite((makeSuite(FieldedTextIndexTest),))
More information about the Zope-CVS
mailing list