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 # ====================================================================
16 IndexWriter, IndexReader, IndexSearcher, \
17 WhitespaceAnalyzer, Document, Field, Term, TermQuery
19 from lia.indexing.BaseIndexingTestCase import BaseIndexingTestCase
22 class DocumentUpdateTest(BaseIndexingTestCase):
26 self.assertEqual(1, self.getHitCount("city", "Amsterdam"))
28 reader = IndexReader.open(self.dir, False)
29 reader.deleteDocuments(Term("city", "Amsterdam"))
32 writer = IndexWriter(self.dir, self.getAnalyzer(), False,
33 IndexWriter.MaxFieldLength.UNLIMITED)
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)
46 self.assertEqual(0, self.getHitCount("city", "Amsterdam"))
47 self.assertEqual(1, self.getHitCount("city", "Petersburg"))
50 def getAnalyzer(self):
52 return WhitespaceAnalyzer()
54 def getHitCount(self, fieldName, searchString):
56 searcher = IndexSearcher(self.dir, True)
57 t = Term(fieldName, searchString)
59 hitCount = len(searcher.search(query, 50).scoreDocs)