PyLucene 3.4.0-1 import
[pylucene.git] / samples / LuceneInAction / lia / indexing / DocumentUpdateTest.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 lucene import \
16      IndexWriter, IndexReader, IndexSearcher, \
17      WhitespaceAnalyzer, Document, Field, Term, TermQuery
18
19 from lia.indexing.BaseIndexingTestCase import BaseIndexingTestCase
20
21
22 class DocumentUpdateTest(BaseIndexingTestCase):
23
24     def testUpdate(self):
25
26         self.assertEqual(1, self.getHitCount("city", "Amsterdam"))
27
28         reader = IndexReader.open(self.dir, False)
29         reader.deleteDocuments(Term("city", "Amsterdam"))
30         reader.close()
31
32         writer = IndexWriter(self.dir, self.getAnalyzer(), False,
33                              IndexWriter.MaxFieldLength.UNLIMITED)
34         doc = Document()
35         doc.add(Field("id", "1", Field.Store.YES, Field.Index.NOT_ANALYZED))
36         doc.add(Field("country", "Russia",
37                       Field.Store.YES, Field.Index.NO))
38         doc.add(Field("contents", "St. Petersburg has lots of bridges",
39                       Field.Store.NO, Field.Index.ANALYZED))
40         doc.add(Field("city", "St. Petersburg",
41                       Field.Store.YES, Field.Index.ANALYZED))
42         writer.addDocument(doc)
43         writer.optimize()
44         writer.close()
45
46         self.assertEqual(0, self.getHitCount("city", "Amsterdam"))
47         self.assertEqual(1, self.getHitCount("city", "Petersburg"))
48
49
50     def getAnalyzer(self):
51
52         return WhitespaceAnalyzer()
53
54     def getHitCount(self, fieldName, searchString):
55
56         searcher = IndexSearcher(self.dir, True)
57         t = Term(fieldName, searchString)
58         query = TermQuery(t)
59         hitCount = len(searcher.search(query, 50).scoreDocs)
60         searcher.close()
61
62         return hitCount