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
18 WhitespaceAnalyzer, Document, Field, IndexWriter, Term, \
19 IndexSearcher, PhraseQuery, RAMDirectory
22 class PhraseQueryTest(TestCase):
26 # set up sample document
27 directory = RAMDirectory()
28 writer = IndexWriter(directory, WhitespaceAnalyzer(), True,
29 IndexWriter.MaxFieldLength.UNLIMITED)
31 doc.add(Field("field", "the quick brown fox jumped over the lazy dog",
32 Field.Store.YES, Field.Index.ANALYZED))
33 writer.addDocument(doc)
36 self.searcher = IndexSearcher(directory)
38 def matched(self, phrase, slop):
44 query.add(Term("field", word))
46 topDocs = self.searcher.search(query, 50)
48 return topDocs.totalHits > 0
50 def testSlopComparison(self):
52 phrase = ["quick", "fox"]
54 self.assert_(not self.matched(phrase, 0), "exact phrase not found")
55 self.assert_(self.matched(phrase, 1), "close enough")
57 def testReverse(self):
59 phrase = ["fox", "quick"]
61 self.assert_(not self.matched(phrase, 2), "hop flop")
62 self.assert_(self.matched(phrase, 3), "hop hop slop")
64 def testMultiple(self):
66 self.assert_(not self.matched(["quick", "jumped", "lazy"], 3),
69 self.assert_(self.matched(["quick", "jumped", "lazy"], 4),
72 self.assert_(not self.matched(["lazy", "jumped", "quick"], 7),
73 "almost but not quite")
75 self.assert_(self.matched(["lazy", "jumped", "quick"], 8),