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
17 WhitespaceAnalyzer, Document, Field, IndexWriter, Term, MultiSearcher, \
18 TermRangeQuery, RAMDirectory, IndexSearcher
21 class MultiSearcherTest(TestCase):
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" ]
33 analyzer = WhitespaceAnalyzer()
35 aTOmDirectory = RAMDirectory()
36 nTOzDirectory = RAMDirectory()
38 aTOmWriter = IndexWriter(aTOmDirectory, analyzer, True,
39 IndexWriter.MaxFieldLength.UNLIMITED)
40 nTOzWriter = IndexWriter(nTOzDirectory, analyzer, True,
41 IndexWriter.MaxFieldLength.UNLIMITED)
43 for animal in animals:
45 doc.add(Field("animal", animal,
46 Field.Store.YES, Field.Index.NOT_ANALYZED))
48 if animal[0].lower() < "n":
49 aTOmWriter.addDocument(doc)
51 nTOzWriter.addDocument(doc)
56 self.searchers = [ IndexSearcher(aTOmDirectory),
57 IndexSearcher(nTOzDirectory) ]
61 searcher = MultiSearcher(self.searchers)
63 # range spans documents across both indexes
64 query = TermRangeQuery("animal", "h", "t", True, True)
66 scoreDocs = searcher.search(query, 50).scoreDocs
67 self.assertEqual(12, len(scoreDocs), "tarantula not included")