pylucene 3.5.0-3
[pylucene.git] / samples / LuceneInAction / lia / advsearching / MultiSearcherTest.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 from lucene import \
17      WhitespaceAnalyzer, Document, Field, IndexWriter, Term, MultiSearcher, \
18      TermRangeQuery, RAMDirectory, IndexSearcher
19
20
21 class MultiSearcherTest(TestCase):
22
23     def setUp(self):
24         
25         animals = [ "aardvark", "beaver", "coati",
26                     "dog", "elephant", "frog", "gila monster",
27                     "horse", "iguana", "javelina", "kangaroo",
28                     "lemur", "moose", "nematode", "orca",
29                     "python", "quokka", "rat", "scorpion",
30                     "tarantula", "uromastyx", "vicuna",
31                     "walrus", "xiphias", "yak", "zebra" ]
32
33         analyzer = WhitespaceAnalyzer()
34
35         aTOmDirectory = RAMDirectory()
36         nTOzDirectory = RAMDirectory()
37
38         aTOmWriter = IndexWriter(aTOmDirectory, analyzer, True,
39                                  IndexWriter.MaxFieldLength.UNLIMITED)
40         nTOzWriter = IndexWriter(nTOzDirectory, analyzer, True,
41                                  IndexWriter.MaxFieldLength.UNLIMITED)
42
43         for animal in animals:
44             doc = Document()
45             doc.add(Field("animal", animal,
46                           Field.Store.YES, Field.Index.NOT_ANALYZED))
47
48             if animal[0].lower() < "n":
49                 aTOmWriter.addDocument(doc)
50             else:
51                 nTOzWriter.addDocument(doc)
52
53         aTOmWriter.close()
54         nTOzWriter.close()
55
56         self.searchers = [ IndexSearcher(aTOmDirectory),
57                            IndexSearcher(nTOzDirectory) ]
58
59     def testMulti(self):
60
61         searcher = MultiSearcher(self.searchers)
62
63         # range spans documents across both indexes
64         query = TermRangeQuery("animal", "h", "t", True, True)
65
66         scoreDocs = searcher.search(query, 50).scoreDocs
67         self.assertEqual(12, len(scoreDocs), "tarantula not included")