[Zope-Checkins] SVN: Zope/trunk/ - Collector #1784: fixed handling of multiple attributes in ZCTextIndex

Andreas Jung andreas at andreas-jung.com
Tue May 17 14:05:34 EDT 2005


Log message for revision 30378:
        - Collector #1784: fixed handling of multiple attributes in ZCTextIndex
  

Changed:
  U   Zope/trunk/doc/CHANGES.txt
  U   Zope/trunk/lib/python/Products/ZCTextIndex/ZCTextIndex.py
  U   Zope/trunk/lib/python/Products/ZCTextIndex/tests/testZCTextIndex.py

-=-
Modified: Zope/trunk/doc/CHANGES.txt
===================================================================
--- Zope/trunk/doc/CHANGES.txt	2005-05-17 16:57:47 UTC (rev 30377)
+++ Zope/trunk/doc/CHANGES.txt	2005-05-17 18:05:34 UTC (rev 30378)
@@ -31,6 +31,8 @@
 
     Bugs fixed
 
+      - Collector #1784: fixed handling of multiple attributes in ZCTextIndex
+
       - Don't copy '.svn' directories from skeleton into an instance
         (thanks to Dale Hirt for the patch).
 

Modified: Zope/trunk/lib/python/Products/ZCTextIndex/ZCTextIndex.py
===================================================================
--- Zope/trunk/lib/python/Products/ZCTextIndex/ZCTextIndex.py	2005-05-17 16:57:47 UTC (rev 30377)
+++ Zope/trunk/lib/python/Products/ZCTextIndex/ZCTextIndex.py	2005-05-17 18:05:34 UTC (rev 30378)
@@ -159,22 +159,21 @@
         except: fields  = [ self._fieldname ]
 
         res = 0
+        all_texts = []
         for attr in fields:
-            res += self._index_object(documentId, obj, threshold, attr) 
+            text = getattr(obj, attr, None)
+            if text is None:
+                continue
+            if safe_callable(text):
+                text = text()
+            if text is None:
+                continue
+            all_texts.append(text)
 
-        return res
-
-    def _index_object(self, docid, obj, threshold=None, attr=None):
-        # XXX We currently ignore subtransaction threshold
-        text = getattr(obj, self._fieldname, None)
-        if text is None:
+        if all_texts:        
+            return self.index.index_doc(documentId, ' '.join(all_texts))
+        else:
             return 0
-        if safe_callable(text):
-            text = text()
-        if text is None:
-            return 0
-        count = self.index.index_doc(docid, text)
-        return count
 
     def unindex_object(self, docid):
         if self.index.has_doc(docid):

Modified: Zope/trunk/lib/python/Products/ZCTextIndex/tests/testZCTextIndex.py
===================================================================
--- Zope/trunk/lib/python/Products/ZCTextIndex/tests/testZCTextIndex.py	2005-05-17 16:57:47 UTC (rev 30377)
+++ Zope/trunk/lib/python/Products/ZCTextIndex/tests/testZCTextIndex.py	2005-05-17 18:05:34 UTC (rev 30378)
@@ -39,6 +39,11 @@
     def __init__(self, text):
         self.text = text
 
+class Indexable2:
+    def __init__(self, text1, text2):
+        self.text1 = text1
+        self.text2 = text2
+
 class LexiconHolder(Acquisition.Implicit):
     def __init__(self, lexicon):
         self.lexicon = lexicon
@@ -115,6 +120,7 @@
                                     'lexicon')
         self.index = self.zc_index.index
 
+
     def parserFailure(self, query):
         self.assertRaises(ParseError, self.zc_index.query, query)
 
@@ -124,6 +130,27 @@
         if n:
             self.assertEqual(r[0][0], 1)
 
+    def testMultipleAttributes(self):
+        lexicon = PLexicon('lexicon', '',
+                            Splitter(),
+                            CaseNormalizer(),
+                            StopWordRemover())
+        caller = LexiconHolder(self.lexicon)
+        zc_index = ZCTextIndex('name',
+                                None,
+                                caller,
+                                self.IndexFactory,
+                               'text1,text2',
+                               'lexicon')
+        doc = Indexable2('foo bar', 'alpha omega')
+        zc_index.index_object(1, doc)
+        nbest, total = zc_index.query('foo')
+        self.assertEqual(len(nbest), 1)
+        nbest, total = zc_index.query('foo alpha')
+        self.assertEqual(len(nbest), 1)
+        nbest, total = zc_index.query('foo alpha gamma')
+        self.assertEqual(len(nbest), 0)
+
     def testStopWords(self):
         # the only non-stopword is question
         text = ("to be or not to be "



More information about the Zope-Checkins mailing list