1 # ====================================================================
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13 # ====================================================================
15 from unittest import TestCase, main
19 class TestRegexQuery(TestCase):
25 directory = RAMDirectory()
27 writer = IndexWriter(directory, SimpleAnalyzer(), True,
28 IndexWriter.MaxFieldLength.LIMITED)
30 doc.add(Field(self.FN, "the quick brown fox jumps over the lazy dog", Field.Store.NO, Field.Index.ANALYZED))
31 writer.addDocument(doc)
34 self.searcher = IndexSearcher(directory, True)
40 def newTerm(self, value):
42 return Term(self.FN, value)
44 def regexQueryNrHits(self, regex):
46 query = RegexQuery(self.newTerm(regex))
48 return self.searcher.search(query, 50).totalHits
50 def spanRegexQueryNrHits(self, regex1, regex2, slop, ordered):
52 srq1 = SpanRegexQuery(self.newTerm(regex1))
53 srq2 = SpanRegexQuery(self.newTerm(regex2))
54 query = SpanNearQuery([srq1, srq2], slop, ordered)
56 return self.searcher.search(query, 50).totalHits
60 self.assertEqual(1, self.regexQueryNrHits("^q.[aeiou]c.*$"))
64 self.assertEqual(0, self.regexQueryNrHits("^.[aeiou]c.*$"))
68 self.assertEqual(0, self.regexQueryNrHits("^q.[aeiou]c$"))
70 def testSpanRegex1(self):
72 self.assertEqual(1, self.spanRegexQueryNrHits("^q.[aeiou]c.*$",
75 def testSpanRegex2(self):
77 self.assertEqual(0, self.spanRegexQueryNrHits("^q.[aeiou]c.*$",
81 if __name__ == "__main__":
84 if '-loop' in sys.argv:
85 sys.argv.remove('-loop')