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 lia.common.LiaTestCase import LiaTestCase
18 WhitespaceAnalyzer, StandardAnalyzer, Term, QueryParser, Locale, \
19 BooleanQuery, FuzzyQuery, IndexSearcher, TermRangeQuery, TermQuery, \
20 BooleanClause, Version
23 class QueryParserTest(LiaTestCase):
27 super(QueryParserTest, self).setUp()
28 self.analyzer = WhitespaceAnalyzer()
29 self.searcher = IndexSearcher(self.directory, True)
31 def testToString(self):
33 query = BooleanQuery()
34 query.add(FuzzyQuery(Term("field", "kountry")),
35 BooleanClause.Occur.MUST)
36 query.add(TermQuery(Term("title", "western")),
37 BooleanClause.Occur.SHOULD)
39 self.assertEqual("+kountry~0.5 title:western",
40 query.toString("field"), "both kinds")
42 def testPrefixQuery(self):
44 parser = QueryParser(Version.LUCENE_CURRENT, "category",
45 StandardAnalyzer(Version.LUCENE_CURRENT))
46 parser.setLowercaseExpandedTerms(False)
48 print parser.parse("/Computers/technology*").toString("category")
50 def testGrouping(self):
52 query = QueryParser(Version.LUCENE_CURRENT, "subject",
53 self.analyzer).parse("(agile OR extreme) AND methodology")
54 scoreDocs = self.searcher.search(query, 50).scoreDocs
56 self.assertHitsIncludeTitle(self.searcher, scoreDocs,
57 "Extreme Programming Explained")
58 self.assertHitsIncludeTitle(self.searcher, scoreDocs,
59 "The Pragmatic Programmer")
61 def testTermRangeQuery(self):
63 query = QueryParser(Version.LUCENE_CURRENT, "subject",
64 self.analyzer).parse("title2:[K TO N]")
65 self.assert_(TermRangeQuery.instance_(query))
67 scoreDocs = self.searcher.search(query, 10).scoreDocs
68 self.assertHitsIncludeTitle(self.searcher, scoreDocs, "Mindstorms")
70 query = QueryParser(Version.LUCENE_CURRENT, "subject",
71 self.analyzer).parse("title2:{K TO Mindstorms}")
72 scoreDocs = self.searcher.search(query, 10).scoreDocs
73 self.assertHitsIncludeTitle(self.searcher, scoreDocs, "Mindstorms",
76 def testDateRangeQuery(self):
78 # locale diff between jre and gcj 1/1/04 -> 01/01/04
79 # expression = "modified:[1/1/04 TO 12/31/04]"
81 expression = "modified:[01/01/04 TO 12/31/04]"
82 parser = QueryParser(Version.LUCENE_CURRENT, "subject", self.analyzer)
83 parser.setLocale(Locale.US)
84 query = parser.parse(expression)
85 print expression, "parsed to", query
87 topDocs = self.searcher.search(query, 50)
88 self.assert_(topDocs.totalHits > 0)
92 q = QueryParser(Version.LUCENE_CURRENT, "field",
93 self.analyzer).parse('"exact phrase"')
94 self.assertEqual("\"exact phrase\"", q.toString("field"),
97 qp = QueryParser(Version.LUCENE_CURRENT, "field", self.analyzer)
99 q = qp.parse('"sloppy phrase"')
100 self.assertEqual("\"sloppy phrase\"~5", q.toString("field"),
101 "sloppy, implicitly")
103 def testPhraseQuery(self):
105 analyzer = StandardAnalyzer(Version.LUCENE_24)
106 q = QueryParser(Version.LUCENE_24, "field",
107 analyzer).parse('"This is Some Phrase*"')
108 self.assertEqual("\"some phrase\"", q.toString("field"), "analyzed")
110 q = QueryParser(Version.LUCENE_CURRENT, "field",
111 self.analyzer).parse('"term"')
112 self.assert_(TermQuery.instance_(q), "reduced to TermQuery")
114 def testLowercasing(self):
116 q = QueryParser(Version.LUCENE_CURRENT, "field",
117 self.analyzer).parse("PrefixQuery*")
118 self.assertEqual("prefixquery*", q.toString("field"), "lowercased")
120 qp = QueryParser(Version.LUCENE_CURRENT, "field", self.analyzer)
121 qp.setLowercaseExpandedTerms(False)
122 q = qp.parse("PrefixQuery*")
123 self.assertEqual("PrefixQuery*", q.toString("field"), "not lowercased")
125 def testWildcard(self):
128 QueryParser(Version.LUCENE_CURRENT, "field",
129 self.analyzer).parse("*xyz")
130 self.fail("Leading wildcard character should not be allowed")
136 q = QueryParser(Version.LUCENE_CURRENT, "field",
137 self.analyzer).parse("term^2")
138 self.assertEqual("term^2.0", q.toString("field"))
140 def testParseException(self):
143 QueryParser(Version.LUCENE_CURRENT, "contents",
144 self.analyzer).parse("^&#")
146 # expression is invalid, as expected
149 self.fail("ParseException expected, but not thrown")