Reputation: 61
I was been doing indexing using lucene.net in WPF C# and suddenly got that new every new doc was added instead of previous and thus the any moment number of docs in index was 1
I don't know how can this happen because I have tested the same adding methods in C# Console but in forms it just something strange happen
Can't you suggest how to solve the problem?
This is the code of where the method to create index is called
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
try
{
LuceneSearch.LuceneEngine le = new LuceneSearch.LuceneEngine();
var engine = new FileEngine();
if (txtboxName.Text != "" && cmbboxDis.SelectedIndex != -1 && btnFile.Content.ToString() != "Choose a file")
{//adds some stuff to DB and returns an object to index
var IndexMe= engine.AddFile(txtboxName.Text, cmbboxDis.SelectedIndex, txtboxAuth.Text, txtboxTags.Text, txtboxComment.Text);
le.BuildIndex(IndexMe);//index this file
this.Close();
}
else
MessageBox.Show("Not all fields are filled", "Oops!", MessageBoxButton.OK, MessageBoxImage.Error);
}
catch (Exception ex)
{
MessageBox.Show("You haven't chosen a file! " +ex.Message, "Ошибка!", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
That's the way how it looks in class of lucene methods
namespace StudentWorksSearch.LuceneSearch
{
public class LuceneEngine
{//get directory
private const string _luceneDir = "../../../Data/lucene_index1";
private FSDirectory _directoryTemp;
private FSDirectory _directory
{
get
{
if (_directoryTemp == null) _directoryTemp = FSDirectory.Open(new
DirectoryInfo(_luceneDir));
if (IndexWriter.IsLocked(_directoryTemp)) IndexWriter.Unlock(_directoryTemp);
var lockFilePath = Path.Combine(_luceneDir, "write.lock");
if (File.Exists(lockFilePath)) File.Delete(lockFilePath);
return _directoryTemp;
}
}
//this method creates document from an ObjectToIndex
public void BuildIndex(FileToIndex file)
{
using (var analyzer = new Lucene.Net.Analysis.Ru.RussianAnalyzer(Version.LUCENE_30))
{
using (IndexWriter idxw = new IndexWriter(_directory, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED))
{
//check if document exists, if true deletes existing
var searchQuery = new TermQuery(new Term("Id", file.Id.ToString()));
idxw.DeleteDocuments(searchQuery);
//creation
Document doc = new Document();
doc.Add(new Field("Id", file.Id.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));//аналайзер разбивает строки на слова
doc.Add(new Field("Title", file.Title, Field.Store.YES, Field.Index.ANALYZED));
doc.Add(new Field("Description", file.Description, Field.Store.YES, Field.Index.ANALYZED));
doc.Add(new Field("Authors", file.Authors, Field.Store.YES, Field.Index.ANALYZED));
doc.Add(new Field("Text", file.Text, Field.Store.YES, Field.Index.ANALYZED));
doc.Add(new Field("Hashtags", file.Hashtags, Field.Store.YES, Field.Index.ANALYZED));
//write the document to the index
idxw.AddDocument(doc);
//optimize and close the writer
idxw.Commit();
idxw.Optimize();
}
}
}
Upvotes: 0
Views: 196
Reputation: 33341
The third argument to the IndexWriter constructor specifies whether it should create a new index. If it is set to true
, it will overwrite any existing index and create a new one. You should set it to false
to open an existing index:
IndexWriter idxw = new IndexWriter(_directory, analyzer, false, IndexWriter.MaxFieldLength.UNLIMITED
Upvotes: 2