old python needs __main__ to call a module
[pylucene.git] / samples / LuceneInAction / lia / extsearch / queryparser / AdvancedQueryParserTest.py
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
5 #
6 #       http://www.apache.org/licenses/LICENSE-2.0
7 #
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 # ====================================================================
14
15 from unittest import TestCase
16
17 from lucene import \
18      WhitespaceAnalyzer, IndexSearcher, RAMDirectory, \
19      Document, Field, IndexWriter, TermQuery, SpanNearQuery
20
21 from lia.extsearch.queryparser.NumberUtils import NumberUtils
22 from lia.extsearch.queryparser.CustomQueryParser import \
23     MultiFieldCustomQueryParser, CustomQueryParser
24
25
26 class AdvancedQueryParserTest(TestCase):
27
28     def setUp(self):
29
30         self.analyzer = WhitespaceAnalyzer()
31         self.directory = RAMDirectory()
32
33         writer = IndexWriter(self.directory, self.analyzer, True, 
34                              IndexWriter.MaxFieldLength.LIMITED)
35
36         for i in xrange(1, 501):
37             doc = Document()
38             doc.add(Field("id", NumberUtils.pad(i),
39                           Field.Store.YES, Field.Index.NOT_ANALYZED))
40             writer.addDocument(doc)
41
42         writer.close()
43
44     def testCustomQueryParser(self):
45
46         parser = CustomQueryParser("field", self.analyzer)
47
48         try:
49             parser.parse("a?t")
50             self.fail("Wildcard queries should not be allowed")
51         except:
52             # expected
53             self.assert_(True)
54
55         try:
56             parser.parse("xunit~")
57             self.fail("Fuzzy queries should not be allowed")
58         except:
59             # expected
60             self.assert_(True)
61
62     def testCustomMultiFieldQueryParser(self):
63
64         parser = MultiFieldCustomQueryParser(["field"], self.analyzer)
65
66         try:
67             parser.parse("a?t")
68             self.fail("Wildcard queries should not be allowed")
69         except:
70             # expected
71             self.assert_(True)
72
73         try:
74             parser.parse("xunit~")
75             self.fail("Fuzzy queries should not be allowed")
76         except:
77             # expected
78             self.assert_(True)
79
80     def testIdRangeQuery(self):
81
82         parser = CustomQueryParser("field", self.analyzer)
83
84         query = parser.parse("id:[37 TO 346]")
85         self.assertEqual("id:[0000000037 TO 0000000346]",
86                          query.toString("field"), "padded")
87
88         searcher = IndexSearcher(self.directory, True)
89         scoreDocs = searcher.search(query, 1000).scoreDocs
90         self.assertEqual(310, len(scoreDocs))
91
92         print parser.parse("special:[term TO *]")
93         print parser.parse("special:[* TO term]")
94
95     def testPhraseQuery(self):
96
97         parser = CustomQueryParser("field", self.analyzer)
98
99         query = parser.parse("singleTerm")
100         self.assert_(TermQuery.instance_(query), "TermQuery")
101
102         query = parser.parse("\"a phrase\"")
103         self.assert_(SpanNearQuery.instance_(query), "SpanNearQuery")