PyLucene 3.4.0-1 import
[pylucene.git] / samples / LuceneInAction / lia / indexing / FieldLengthTest.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 import os
16
17 from itertools import izip
18 from unittest import TestCase
19 from time import time
20 from datetime import timedelta
21
22 from lucene import \
23      IndexWriter, SimpleAnalyzer, Document, Field, System, File, \
24      Term, TermQuery, IndexSearcher, SimpleFSDirectory
25
26
27 class FieldLengthTest(TestCase):
28
29     keywords = ["1", "2"]
30     unindexed = ["Netherlands", "Italy"]
31     unstored = ["Amsterdam has lots of bridges",
32                 "Venice has lots of canals"]
33     text = ["Amsterdam", "Venice"]
34
35     def setUp(self):
36
37         indexDir = os.path.join(System.getProperty("java.io.tmpdir", "tmp"),
38                                 "index-dir")
39         self.dir = SimpleFSDirectory(File(indexDir))
40
41     def testFieldSize(self):
42
43         self.addDocuments(self.dir, 10)
44         self.assertEqual(1, self.getHitCount("contents", "bridges"))
45
46         self.addDocuments(self.dir, 1)
47         self.assertEqual(0, self.getHitCount("contents", "bridges"))
48
49     def getHitCount(self, fieldName, searchString):
50
51         searcher = IndexSearcher(self.dir, True)
52         t = Term(fieldName, searchString)
53         query = TermQuery(t)
54         hitCount = len(searcher.search(query, 50).scoreDocs)
55         searcher.close()
56
57         return hitCount
58
59     def addDocuments(self, dir, maxFieldLength):
60
61         writer = IndexWriter(dir, SimpleAnalyzer(), True,
62                              IndexWriter.MaxFieldLength(maxFieldLength))
63         
64         for keyword, unindexed, unstored, text in \
65                 izip(self.keywords, self.unindexed, self.unstored, self.text):
66             doc = Document()
67             doc.add(Field("id", keyword,
68                           Field.Store.YES, Field.Index.NOT_ANALYZED))
69             doc.add(Field("country", unindexed,
70                           Field.Store.YES, Field.Index.NO))
71             doc.add(Field("contents", unstored,
72                           Field.Store.NO, Field.Index.ANALYZED))
73             doc.add(Field("city", text,
74                           Field.Store.YES, Field.Index.ANALYZED))
75             writer.addDocument(doc)
76
77         writer.optimize()
78         writer.close()