[Zope-CVS] CVS: Products/FieldedTextIndex - index.py:1.9 test.py:1.7
Casey Duncan
casey at zope.com
Sat Jan 17 00:21:06 EST 2004
Update of /cvs-repository/Products/FieldedTextIndex
In directory cvs.zope.org:/tmp/cvs-serv6805
Modified Files:
index.py test.py
Log Message:
Add APIs and management screens to set default field weights.
=== Products/FieldedTextIndex/index.py 1.8 => 1.9 ===
--- Products/FieldedTextIndex/index.py:1.8 Mon Jan 12 01:09:42 2004
+++ Products/FieldedTextIndex/index.py Sat Jan 17 00:20:35 2004
@@ -51,15 +51,19 @@
manage_options = (
{'label': 'Overview', 'action': 'manage_main'},
+ {'label': 'Default Field Weights', 'action':'manage_weights'},
)
manage_main = DTMLFile('www/manageIndex', globals())
+ manage_weights = DTMLFile('www/manageWeights', globals())
query_options = ['query', 'operator', 'fields', 'field_weights']
security = ClassSecurityInfo()
security.declareObjectProtected(Permissions.manage_zcatalog_indexes)
+ _default_weights = {}
+
def __init__(
self, id, source_name=None, lexicon=None, extra=None, caller=None, ):
"""Fielded text index constructor
@@ -103,10 +107,34 @@
"""Return a list of the field names in the index"""
return self._fields[:]
- security.declareProtected(Permissions.search_zcatalog, 'fieldNames')
+ security.declareProtected(Permissions.search_zcatalog, 'lexiconId')
def lexiconId(self):
"""Return the id of the lexicon for this index"""
return self._lexicon.getId()
+
+ security.declareProtected(
+ Permissions.manage_zcatalog_indexes, 'setDefaultWeights')
+ def setDefaultWeights(self, weights):
+ """Set the default weights where weights is a mapping of
+ field name => integer weight. weights may also be a record object
+ for convenience when setting from a form. Note that fields can be
+ assigned weights before objects with those fields have been indexed.
+ """
+ defaults = {}
+ for name, weight in weights.items():
+ fieldid = self._field_id(name)
+ weight = int(weight)
+ if weight != 1:
+ defaults[fieldid] = weight
+ self._default_weights = defaults
+
+ security.declareProtected(Permissions.search_zcatalog, 'getDefaultWeights')
+ def getDefaultWeights(self):
+ """Return a dictionary of the default weights for every field"""
+ defaults = {}
+ for fieldid, fname in enumerate(self._fields):
+ defaults[fname] = self._default_weights.get(fieldid, 1)
+ return defaults
## Pluggable Index API ##
@@ -388,7 +416,10 @@
for t in wids:
d2f = self._wordinfo[t] # map {docid -> f(docid, t)}
if fields is not None:
- fielddocs = self._wordfields[t]
+ try:
+ fielddocs = self._wordfields[t]
+ except KeyError:
+ continue
docsets = []
if not weights and operator == 'or':
# We have fields, but no field weights and the set op
=== Products/FieldedTextIndex/test.py 1.6 => 1.7 ===
--- Products/FieldedTextIndex/test.py:1.6 Mon Jan 12 01:09:42 2004
+++ Products/FieldedTextIndex/test.py Sat Jan 17 00:20:35 2004
@@ -211,6 +211,15 @@
self.assertEqual(self.index._apply_index(
{'shmields':{'query':'field'}}), None)
+ def test_query_empty_field(self):
+ self.index_one(1)
+ self.index_two(2)
+ import pdb; pdb.set_trace()
+ self.index.unindex_object(1)
+ results, used = self.index._apply_index(
+ {'fields':{'query':'field', 'fields':['izzy']}})
+ self.assertEqual(list(results.keys()), [])
+
def test_query_op_defaults_to_or(self):
self.index_one(1)
self.index_two(2)
@@ -319,7 +328,40 @@
def test_hasUniqueValuesFor(self):
self.failUnless(self.index.hasUniqueValuesFor('fields'))
self.failIf(self.index.hasUniqueValuesFor('frootloops'))
-
+
+ def test_default_weights_empty_index(self):
+ self.assertEqual(self.index.getDefaultWeights(), {})
+
+ def test_default_weights_default_to_one(self):
+ self.index_one(1)
+ self.index_two(2)
+ expected = {}
+ for f in self.index.fieldNames():
+ expected[f] = 1
+ self.assertEqual(self.index.getDefaultWeights(), expected)
+
+ def test_set_default_weights(self):
+ self.index_one(1)
+ self.index_two(2)
+ weights = {'title':3, 'clyde':0}
+ self.index.setDefaultWeights(weights)
+ expected = {}
+ for f in self.index.fieldNames():
+ expected[f] = 1
+ expected.update(weights)
+ self.assertEqual(self.index.getDefaultWeights(), expected)
+
+ def test_set_default_weights_resets_existing(self):
+ self.index_one(1)
+ self.index_two(2)
+ self.index.setDefaultWeights({'title':3, 'clyde':0})
+ weights = {'izzy':2, 'clyde':2}
+ self.index.setDefaultWeights(weights)
+ expected = {}
+ for f in self.index.fieldNames():
+ expected[f] = 1
+ expected.update(weights)
+ self.assertEqual(self.index.getDefaultWeights(), expected)
def test_suite():
return TestSuite((makeSuite(FieldedTextIndexTest),))
More information about the Zope-CVS
mailing list