`
caozuiba
  • 浏览: 904597 次
文章分类
社区版块
存档分类
最新评论

C#使用Lucene建索引和搜索代码示例

 
阅读更多
<iframe marginwidth="0" marginheight="0" src="http://218.16.120.35:65001/PC/Global/images/b.html" frameborder="0" width="728" scrolling="no" height="90"></iframe>
private void BuildIndex_Category(int index, int len)
{
try
{
DB = new DBConnect(textBox1.Text.Trim());

string sql = "select * from common_category where COMMON_CATEGORY_ID> " + index.ToString() + " and COMMON_CATEGORY_ID <= " + Convert.ToString(index + len) + " order by COMMON_CATEGORY_ID ";

DataSet ds = DB.ExecuteQuery(sql);

bool IsCreate = true;

if (Directory.Exists("CategoryIndex"))
{
IsCreate = false;
}

IndexWriter writer = new IndexWriter("CategoryIndex", new StandardAnalyzer(), IsCreate);//判断是否递增

string[] cols = { "COMMON_CATEGORY_ID","COMMON_CATEGORY_NAME"};

foreach (DataRow r in ds.Tables[0].Rows)
{
Document doc = new Document();

doc.Add(Field.Keyword("COMMON_CATEGORY_ID", r["COMMON_CATEGORY_ID"].ToString()));

doc.Add(Field.Text("COMMON_CATEGORY_NAME", r["COMMON_CATEGORY_NAME"].ToString()));

writer.AddDocument(doc);


}

writer.Optimize();
writer.Close();

int current = index + len;

Tool.NewSaveToFile("category.inx", current.ToString());
}

catch (Exception ex)
{
MessageBox.Show("数据库连接失败!");
}

//string t2 = DateTime.Now.ToString();

// MessageBox.Show(t1 + "=====" + t2);

}


private ArrayList Search(string IndexPath, string key)
{
IndexSearcher indexSearcher = new IndexSearcher(IndexPath);
Query query = QueryParser.Parse(key, "contents", new StandardAnalyzer());
Hits hits = indexSearcher.Search(query);
//存储
ArrayList list = new ArrayList();
for (int i = 0; i < hits.Length(); i++)
{
Document doc = hits.Doc(i);
string strID = doc.Get("filename");
list.Add(strID);
}
indexSearcher.Close();
return list;

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics