[Zope-CVS] CVS: Products/FieldedTextIndex - index.py:1.7 test.py:1.5
Casey Duncan
casey at zope.com
Mon Dec 15 12:19:46 EST 2003
Update of /cvs-repository/Products/FieldedTextIndex
In directory cvs.zope.org:/tmp/cvs-serv12760
Modified Files:
index.py test.py
Log Message:
Fix phrase matching and add tests
=== Products/FieldedTextIndex/index.py 1.6 => 1.7 ===
--- Products/FieldedTextIndex/index.py:1.6 Sat Dec 13 23:18:13 2003
+++ Products/FieldedTextIndex/index.py Mon Dec 15 12:19:45 2003
@@ -27,6 +27,7 @@
from Products.ZCTextIndex.OkapiIndex import OkapiIndex
from Products.ZCTextIndex.BaseIndex \
import BaseIndex, inverse_doc_frequency, scaled_int, unique
+from Products.ZCTextIndex.SetOps import mass_weightedIntersection
from Products.ZCTextIndex.okascore import score
from BTrees.IOBTree import IOBTree
from BTrees.IIBTree import IIBTree, IITreeSet, IIBucket
@@ -381,6 +382,35 @@
score(result, d2f.items(), docid2len, idf, meandoclen)
L.append((result, 1))
return L
+
+ def search_phrase(self, phrase):
+ """Override from BaseIndex to search fielded docwords mapping"""
+ wids = self._lexicon.termToWordIds(phrase)
+ cleaned_wids = self._remove_oov_wids(wids)
+ if len(wids) != len(cleaned_wids):
+ # At least one wid was OOV: can't possibly find it.
+ return IIBTree()
+ scores = self._search_wids(wids)
+ hits = mass_weightedIntersection(scores)
+ if not hits:
+ return hits
+ code = WidCode.encode(wids)
+ search_fields = getattr(
+ self.REQUEST, '_fielded_text_index_search_fields', None)
+ if search_fields is None:
+ search_fields = range(len(self._fields))
+ result = IIBTree()
+ for docid, weight in hits.items():
+ docwords = self._docwords[docid]
+ for field in search_fields:
+ try:
+ words = docwords[field]
+ except KeyError:
+ pass # Word not found in this field for any doc
+ else:
+ if words.find(code) >= 0:
+ result[docid] = weight
+ return result
InitializeClass(FieldedTextIndex)
addIndexForm = DTMLFile('www/addIndex', globals())
=== Products/FieldedTextIndex/test.py 1.4 => 1.5 ===
--- Products/FieldedTextIndex/test.py:1.4 Fri Dec 12 00:09:26 2003
+++ Products/FieldedTextIndex/test.py Mon Dec 15 12:19:45 2003
@@ -211,6 +211,27 @@
self.assertEqual(self.index._apply_index(
{'shmields':{'query':'field'}}), None)
+ def test_phrase_match_all_fields(self):
+ self.index_one(1)
+ self.index_two(2)
+ results, used = self.index._apply_index(
+ {'fields':{'query':'"A title"'}})
+ self.assertEqual(list(results.keys()), [1])
+
+ def test_phrase_match_one_field(self):
+ self.index_one(1)
+ self.index_two(2)
+ results, used = self.index._apply_index(
+ {'fields':{'query':'"different title"', 'fields':['title']}})
+ self.assertEqual(list(results.keys()), [2])
+
+ def test_phrase_match_one_field_no_match(self):
+ self.index_one(1)
+ self.index_two(2)
+ results, used = self.index._apply_index(
+ {'fields':{'query':'"different title"', 'fields':['izzy']}})
+ self.assertEqual(list(results.keys()), [])
+
def test_numObjects(self):
self.index_one(1)
self.assertEqual(self.index.numObjects(), self.index.length())
More information about the Zope-CVS
mailing list