private void button2_Click(object sender, EventArgs e) { if (comboBox1.Text.Trim().Equals("")) { MessageHelper.ShowErrorMessage("索引文件不能为空!"); return; } IndexOpen open = new IndexOpen(this.comboBox1.Text, false); Console.WriteLine(110); if (open.IndexExists()) { bool isOpend = open.Open(); if (isOpend) { CurrentIndex.SetCurrentOpendIndex(open); this.Close(); this.DialogResult = DialogResult.OK; } else { MessageHelper.ShowErrorMessage("打开索引文件失败,有可能是索引文件损坏引起。"); } } else { MessageHelper.ShowErrorMessage("索引文件不存在!"); } }
private void btnUpdateDoc_Click(object sender, EventArgs e) { try { string path = System.Configuration.ConfigurationSettings.AppSettings["Data"].ToString(); Analyzer analyzer = new StockFooAnalyzer(path); FSDirectory dy = FSDirectory.Open(new DirectoryInfo(System.Configuration.ConfigurationSettings.AppSettings["IndexDirectory"].ToString())); IndexWriter indexWriter = new IndexWriter(dy, analyzer, false); IOpen oldopen = CurrentIndex.GetCurrentOpendIndex(); Document olddoc = oldopen.Reader.Document(Convert.ToInt32(textBox1.Text)); string oldurl = olddoc.Get("url").ToString(); bool oldisnew = false; if (oldurl.Trim().Equals(txturl.Text.Trim())) oldisnew = true; Document document = new Document(); Field ftitle = new Field("title", txttitle.Text, Field.Store.YES, Field.Index.ANALYZED);//存储,索引 document.Add(ftitle); Field furl = new Field("url", txturl.Text, Field.Store.YES, Field.Index.NOT_ANALYZED);//存储,不索引 document.Add(furl); Field fsite = new Field("site", txtsite.Text, Field.Store.YES, Field.Index.NOT_ANALYZED);//存储,不索引 document.Add(fsite); Field fbody = new Field("body", txtbody.Text, Field.Store.YES, Field.Index.ANALYZED);//存储,索引 document.Add(fbody); Field fpublishtime = new Field("publish_time", txtpublishtime.Text, Field.Store.YES, Field.Index.NOT_ANALYZED);//存储,不索引 document.Add(fpublishtime); Term term = new Term("url", txturl.Text); indexWriter.UpdateDocument(term, document, analyzer); indexWriter.Optimize(); indexWriter.Close(); IndexOpen open = new IndexOpen(System.Configuration.ConfigurationSettings.AppSettings["IndexDirectory"].ToString(), false); bool isOpend = open.Open(); if (isOpend) { CurrentIndex.SetCurrentOpendIndex(open); } if (!oldisnew) { FindDocuemnt(0); label9.Text = "当前为文档1"; textBox1.Text = "0"; docNum = docNum + 1; label1.Text = string.Format("共有文档{0}条", docNum); } else { docNum = open.Reader.MaxDoc(); FindDocuemnt(docNum - 1); label9.Text = "当前为文档" + (docNum).ToString(); textBox1.Text = (docNum - 1).ToString(); } MessageBox.Show("更新成功!"); } catch (Exception ex) { MessageBox.Show("更新失败!\\n"+ex.Message); } }