Commit 75964156 authored by Andreas Jung's avatar Andreas Jung

- Collector #1784: fixed handling of multiple attributes in ZCTextIndex

parent 06e3b28b
...@@ -31,6 +31,8 @@ Zope Changes ...@@ -31,6 +31,8 @@ Zope Changes
Bugs fixed Bugs fixed
- Collector #1784: fixed handling of multiple attributes in ZCTextIndex
- Don't copy '.svn' directories from skeleton into an instance - Don't copy '.svn' directories from skeleton into an instance
(thanks to Dale Hirt for the patch). (thanks to Dale Hirt for the patch).
......
...@@ -159,22 +159,21 @@ class ZCTextIndex(Persistent, Acquisition.Implicit, SimpleItem): ...@@ -159,22 +159,21 @@ class ZCTextIndex(Persistent, Acquisition.Implicit, SimpleItem):
except: fields = [ self._fieldname ] except: fields = [ self._fieldname ]
res = 0 res = 0
all_texts = []
for attr in fields: for attr in fields:
res += self._index_object(documentId, obj, threshold, attr) text = getattr(obj, attr, None)
if text is None:
return res continue
if safe_callable(text):
def _index_object(self, docid, obj, threshold=None, attr=None): text = text()
# XXX We currently ignore subtransaction threshold if text is None:
text = getattr(obj, self._fieldname, None) continue
if text is None: all_texts.append(text)
return 0
if safe_callable(text): if all_texts:
text = text() return self.index.index_doc(documentId, ' '.join(all_texts))
if text is None: else:
return 0 return 0
count = self.index.index_doc(docid, text)
return count
def unindex_object(self, docid): def unindex_object(self, docid):
if self.index.has_doc(docid): if self.index.has_doc(docid):
......
...@@ -39,6 +39,11 @@ class Indexable: ...@@ -39,6 +39,11 @@ class Indexable:
def __init__(self, text): def __init__(self, text):
self.text = text self.text = text
class Indexable2:
def __init__(self, text1, text2):
self.text1 = text1
self.text2 = text2
class LexiconHolder(Acquisition.Implicit): class LexiconHolder(Acquisition.Implicit):
def __init__(self, lexicon): def __init__(self, lexicon):
self.lexicon = lexicon self.lexicon = lexicon
...@@ -115,6 +120,7 @@ class ZCIndexTestsBase: ...@@ -115,6 +120,7 @@ class ZCIndexTestsBase:
'lexicon') 'lexicon')
self.index = self.zc_index.index self.index = self.zc_index.index
def parserFailure(self, query): def parserFailure(self, query):
self.assertRaises(ParseError, self.zc_index.query, query) self.assertRaises(ParseError, self.zc_index.query, query)
...@@ -124,6 +130,27 @@ class ZCIndexTestsBase: ...@@ -124,6 +130,27 @@ class ZCIndexTestsBase:
if n: if n:
self.assertEqual(r[0][0], 1) 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): def testStopWords(self):
# the only non-stopword is question # the only non-stopword is question
text = ("to be or not to be " text = ("to be or not to be "
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment