Lucene .NET IndexWriter DeleteDocuments Not Working
Here is the code:
Try
Dim util As New IndexerUtil()
Dim dir As Lucene.Net.Store.Directory = FSDirectory.Open(New DirectoryInfo(util.getIndexDir()))
Dim indexWriter As New IndexWriter(dir, New SimpleAnalyzer(), indexWriter.MaxFieldLength.UNLIMITED)
Dim numDocs As Integer = indexWriter.NumDocs()
indexWriter.DeleteDocuments(New Term("id", insightId))
indexWriter.Optimize()
indexWriter.Commit()
indexWriter.Close()
numDocs = indexWriter.NumDocs()
Catch ex As Exception
LOG.Error("Could not remove insight " + insightId + " from index", ex)
End Try
numDocs = 85 both times
I also have a little gui app I wrote which reads the index and prints the docs out in a nice format. The doc with the id field that equals insightId definitely exists and STILL exists after the "deletion".
Here 开发者_如何学运维is how the id field is being created
doc.Add(New Field("id", insightID, Field.Store.YES, Field.Index.ANALYZED)) //insightID is an integer
As you have probably discovered with your more recent post, your ID column is not being indexed correctly because SimpleAnalyzer uses LetterTokenizer, which only returns letters.
Consider using the KeywordAnalyzer instead for the id
field.
Since SimpleAnalyzer converts input text to lowercase, you will have lowercased terms in the index. Are you sure that "insightId" is also lowercased?
You should create a new IndexWriter instead of counting documents on a closed one.
精彩评论