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
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).
......
......@@ -159,22 +159,21 @@ class ZCTextIndex(Persistent, Acquisition.Implicit, SimpleItem):
except: fields = [ self._fieldname ]
res = 0
all_texts = []
for attr in fields:
res += self._index_object(documentId, obj, threshold, attr)
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:
return 0
if safe_callable(text):
text = text()
if text is None:
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)
if all_texts:
return self.index.index_doc(documentId, ' '.join(all_texts))
else:
return 0
count = self.index.index_doc(docid, text)
return count
def unindex_object(self, docid):
if self.index.has_doc(docid):
......
......@@ -39,6 +39,11 @@ class Indexable:
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 @@ class ZCIndexTestsBase:
'lexicon')
self.index = self.zc_index.index
def parserFailure(self, query):
self.assertRaises(ParseError, self.zc_index.query, query)
......@@ -124,6 +130,27 @@ class ZCIndexTestsBase:
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 "
......
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