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;
}