Matt Chaput avatar Matt Chaput committed 3df999f

Fixed bugs in Regex private methods. Thanks Thomas!
Fixes issue #206.

Comments (0)

Files changed (2)

src/whoosh/query.py

         raise NotImplementedError
 
     def _find_prefix(self, text):
+        # Subclasses/instances should set the SPECIAL_CHARS attribute to a set
+        # of characters that mark the end of the literal prefix
         specialchars = self.SPECIAL_CHARS
         for i, char in enumerate(self.text):
             if char in specialchars:
         else:
             return self
 
+    # _words() implemented in PatternQuery
+
 
 class Regex(PatternQuery):
     """Matches documents that contain any terms that match a regular
     def _get_pattern(self):
         return self.text
 
-    def _get_prefix(self, text):
+    def _find_prefix(self, text):
         if "|" in text:
             return ""
         if text.startswith("^"):
 
         return PatternQuery._find_prefix(self, text)
 
+    # _words() implemented in PatternQuery
+
 
 class ExpandingTerm(MultiTerm):
     """Intermediate base class for queries such as FuzzyTerm and Variations

tests/test_queries.py

+from __future__ import with_statement
+
 from nose.tools import assert_equal, assert_not_equal  #@UnresolvedImport
 
 import copy
     r = s.search(DateRange('released', datetime(2007, 1, 1), None))
     assert_equal(len(r), 1)
     assert_equal(r[0].highlights("content"), '')
+
+def test_patterns():
+    domain = u("aaron able acre adage aether after ago ahi aim ajax akimbo "
+               "alembic all amiga amount ampere").split()
+    schema = fields.Schema(word=fields.KEYWORD(stored=True))
+    ix = RamStorage().create_index(schema)
+    with ix.writer() as w:
+        for word in domain:
+            w.add_document(word=word)
+
+    with ix.reader() as r:
+        assert_equal(list(r.lexicon("word")), domain)
+
+        assert_equal(list(r.expand_prefix("word", "al")), ["alembic", "all"])
+        q = query.Prefix("word", "al")
+        assert_equal(q.simplify(r).__unicode__(), "(word:alembic OR word:all)")
+
+        q = query.Wildcard("word", "a*[ae]")
+        assert_equal(q.simplify(r).__unicode__(),
+                     "(word:able OR word:acre OR word:adage OR word:amiga OR word:ampere)")
+        assert_equal(q._find_prefix(q.text), "a")
+
+        q = query.Regex("word", "am.*[ae]")
+        assert_equal(q.simplify(r).__unicode__(), "(word:amiga OR word:ampere)")
+        assert_equal(q._find_prefix(q.text), "am")
+
+        q = query.Regex("word", "able|ago")
+        assert_equal(q.simplify(r).__unicode__(), "(word:able OR word:ago)")
+        assert_equal(q._find_prefix(q.text), "")
+
+
+
+
+
+
+
+
+
+
+
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.