[Checkins] SVN: Products.ZCatalog/trunk/ Restored preserving score values from ZCTextIndex indices. Closes https://bugs.launchpad.net/zope2/+bug/815469

Hanno Schlichting hannosch at hannosch.eu
Sun Jul 24 13:36:41 EDT 2011


Log message for revision 122334:
  Restored preserving score values from ZCTextIndex indices. Closes https://bugs.launchpad.net/zope2/+bug/815469
  

Changed:
  U   Products.ZCatalog/trunk/CHANGES.txt
  U   Products.ZCatalog/trunk/src/Products/PluginIndexes/common/ResultList.py
  U   Products.ZCatalog/trunk/src/Products/ZCatalog/Catalog.py
  U   Products.ZCatalog/trunk/src/Products/ZCatalog/tests/test_catalog.py

-=-
Modified: Products.ZCatalog/trunk/CHANGES.txt
===================================================================
--- Products.ZCatalog/trunk/CHANGES.txt	2011-07-24 14:50:56 UTC (rev 122333)
+++ Products.ZCatalog/trunk/CHANGES.txt	2011-07-24 17:36:40 UTC (rev 122334)
@@ -4,6 +4,8 @@
 2.13.16 (unreleased)
 --------------------
 
+- Restored preserving score values from ZCTextIndex indices.
+  https://bugs.launchpad.net/zope2/+bug/815469
 
 2.13.15 (2011-06-30)
 --------------------

Modified: Products.ZCatalog/trunk/src/Products/PluginIndexes/common/ResultList.py
===================================================================
--- Products.ZCatalog/trunk/src/Products/PluginIndexes/common/ResultList.py	2011-07-24 14:50:56 UTC (rev 122333)
+++ Products.ZCatalog/trunk/src/Products/PluginIndexes/common/ResultList.py	2011-07-24 17:36:40 UTC (rev 122334)
@@ -13,8 +13,8 @@
 
 from BTrees.IIBTree import difference
 from BTrees.IIBTree import IIBucket
-from BTrees.IIBTree import intersection
-from BTrees.IIBTree import union as ii_union
+from BTrees.IIBTree import weightedIntersection
+from BTrees.IIBTree import weightedUnion
 from BTrees.OOBTree import OOSet
 from BTrees.OOBTree import union
 
@@ -55,7 +55,7 @@
 
     def __and__(self, x):
         return self.__class__(
-            intersection(self._dict, x._dict),
+            weightedIntersection(self._dict, x._dict),
             union(self._words, x._words),
             self._index,
             )
@@ -69,7 +69,7 @@
 
     def __or__(self, x):
         return self.__class__(
-            ii_union(self._dict, x._dict),
+            weightedUnion(self._dict, x._dict),
             union(self._words, x._words),
             self._index,
             )

Modified: Products.ZCatalog/trunk/src/Products/ZCatalog/Catalog.py
===================================================================
--- Products.ZCatalog/trunk/src/Products/ZCatalog/Catalog.py	2011-07-24 14:50:56 UTC (rev 122333)
+++ Products.ZCatalog/trunk/src/Products/ZCatalog/Catalog.py	2011-07-24 17:36:40 UTC (rev 122334)
@@ -27,6 +27,7 @@
 
 import BTrees.Length
 from BTrees.IIBTree import intersection, IISet
+from BTrees.IIBTree import weightedIntersection
 from BTrees.OIBTree import OIBTree
 from BTrees.IOBTree import IOBTree
 from Lazy import LazyMap, LazyCat, LazyValues
@@ -540,7 +541,9 @@
                 # provide detailed info about the pure intersection time
                 intersect_id = i + '#intersection'
                 cr.start_split(intersect_id)
-                rs = intersection(rs, r)
+                # weightedIntersection preserves the values from any mappings
+                # we get, as some indexes don't return simple sets
+                _, rs = weightedIntersection(rs, r)
                 cr.stop_split(intersect_id)
 
                 # consider the time it takes to intersect the index result with

Modified: Products.ZCatalog/trunk/src/Products/ZCatalog/tests/test_catalog.py
===================================================================
--- Products.ZCatalog/trunk/src/Products/ZCatalog/tests/test_catalog.py	2011-07-24 14:50:56 UTC (rev 122333)
+++ Products.ZCatalog/trunk/src/Products/ZCatalog/tests/test_catalog.py	2011-07-24 17:36:40 UTC (rev 122334)
@@ -662,6 +662,51 @@
         self.assertEqual(merged_rids, expected)
 
 
+class TestScoring(CatalogBase, unittest.TestCase):
+
+    def _get_catalog(self):
+        return self._catalog.__of__(zdummy(16336))
+
+    def setUp(self):
+        self._catalog = self._makeOne()
+        self._catalog.lexicon = PLexicon('lexicon')
+        idx = ZCTextIndex('title', caller=self._catalog,
+                          index_factory=OkapiIndex, lexicon_id='lexicon')
+        self._catalog.addIndex('title', idx)
+        self._catalog.addIndex('true', FieldIndex('true'))
+        self._catalog.addColumn('title')
+        cat = self._get_catalog()
+        for i in (1, 2, 3, 10, 11, 110, 111):
+            obj = zdummy(i)
+            obj.true = True
+            if i == 110:
+                obj.true = False
+            cat.catalogObject(obj, str(i))
+
+    def test_simple_search(self):
+        cat = self._get_catalog()
+        brains = cat(title='10')
+        self.assertEqual(len(brains), 1)
+        self.assertEqual(brains[0].title, '10')
+
+    def test_or_search(self):
+        cat = self._get_catalog()
+        brains = cat(title='2 OR 3')
+        self.assertEqual(len(brains), 2)
+
+    def test_scored_search(self):
+        cat = self._get_catalog()
+        brains = cat(title='1*')
+        self.assertEqual(len(brains), 5)
+        self.assertEqual(brains[0].title, '111')
+
+    def test_combined_scored_search(self):
+        cat = self._get_catalog()
+        brains = cat(title='1*', true=True)
+        self.assertEqual(len(brains), 4)
+        self.assertEqual(brains[0].title, '111')
+
+
 def test_suite():
     suite = unittest.TestSuite()
     suite.addTest(unittest.makeSuite(TestAddDelColumn))
@@ -671,4 +716,5 @@
     suite.addTest(unittest.makeSuite(TestCatalogReturnAll))
     suite.addTest(unittest.makeSuite(TestCatalogSearchArgumentsMap))
     suite.addTest(unittest.makeSuite(TestMergeResults))
+    suite.addTest(unittest.makeSuite(TestScoring))
     return suite



More information about the checkins mailing list