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 # ====================================================================
17 from itertools import izip
18 from unittest import TestCase
20 from datetime import timedelta
23 IndexWriter, SimpleAnalyzer, Document, Field, System, File, \
24 Term, TermQuery, IndexSearcher, SimpleFSDirectory
27 class FieldLengthTest(TestCase):
30 unindexed = ["Netherlands", "Italy"]
31 unstored = ["Amsterdam has lots of bridges",
32 "Venice has lots of canals"]
33 text = ["Amsterdam", "Venice"]
37 indexDir = os.path.join(System.getProperty("java.io.tmpdir", "tmp"),
39 self.dir = SimpleFSDirectory(File(indexDir))
41 def testFieldSize(self):
43 self.addDocuments(self.dir, 10)
44 self.assertEqual(1, self.getHitCount("contents", "bridges"))
46 self.addDocuments(self.dir, 1)
47 self.assertEqual(0, self.getHitCount("contents", "bridges"))
49 def getHitCount(self, fieldName, searchString):
51 searcher = IndexSearcher(self.dir, True)
52 t = Term(fieldName, searchString)
54 hitCount = len(searcher.search(query, 50).scoreDocs)
59 def addDocuments(self, dir, maxFieldLength):
61 writer = IndexWriter(dir, SimpleAnalyzer(), True,
62 IndexWriter.MaxFieldLength(maxFieldLength))
64 for keyword, unindexed, unstored, text in \
65 izip(self.keywords, self.unindexed, self.unstored, self.text):
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)