public void CreateIndex() {
//创建索引库目录 string path = Server.MapPath("indexdata");//索引文件储存位置
FSDirectory directory = FSDirectory.Open(new DirectoryInfo(path), new NativeFSLockFactory()); //IndexReader:对索引库进行读取的类
bool isExist = IndexReader.IndexExists(directory); //是否存在索引库文件夹以及索引库特征文件 if (isExist) { //如果索引目录被锁定(比如索引过程中程序异常退出或另一进程在操作索引库),则解锁 //Q:存在问题 如果一个用户正在对索引库写操作 此时是上锁的 而另一个用户过来操作时 将锁解开了 于是产生冲突 --解决方法后续 if (IndexWriter.IsLocked(directory)) { IndexWriter.Unlock(directory); } } //创建一个索引,采用StandardAnalyzer对句子进行分词 IndexWriter indexWriter = new IndexWriter(directory, new PanGuAnalyzer(), true, IndexWriter.MaxFieldLength.UNLIMITED);
SqlConnection conn = new SqlConnection(@"XXXXXXXXXXX"); conn.Open();
SqlCommand cmd = new SqlCommand("select ID,Title,[Content] from news", conn); SqlDataReader reader = cmd.ExecuteReader(); //string sql = "select ID,Title,[Content] from news"; //SqlDataReader reader = helper.GetDataReader(sql); while (reader.Read()) { //域的集合:文档,类似于表的行 Document doc = new Document(); //要索引的字段 doc.Add(new Field("Title", reader["Title"].ToString(), Field.Store.YES, Field.Index.ANALYZED)); doc.Add(new Field("Content", reader["Content"].ToString(), Field.Store.YES, Field.Index.ANALYZED)); doc.Add(new Field("URL", "/AboutUs/News.aspx?ID=" + reader["ID"].ToString() + "", Field.Store.YES, Field.Index.ANALYZED)); indexWriter.AddDocument(doc); } reader.Close(); SqlCommand cmd1 = new SqlCommand("select ID,Title,[Content] from service", conn);
SqlDataReader reader1 = cmd1.ExecuteReader(); //string sql1 = "select ID,Title,[Content] from service"; //SqlDataReader reader1 = helper.GetDataReader(sql1); while (reader1.Read()) { //域的集合:文档,类似于表的行 Document doc = new Document(); //要索引的字段 doc.Add(new Field("Title", reader1["Title"].ToString(), Field.Store.YES, Field.Index.ANALYZED)); doc.Add(new Field("Content", reader1["Content"].ToString(), Field.Store.YES, Field.Index.ANALYZED)); doc.Add(new Field("URL", "/Service/ServiceContent.aspx?ID=" + reader1["ID"].ToString() + "", Field.Store.YES, Field.Index.ANALYZED)); indexWriter.AddDocument(doc); } reader1.Close(); SqlCommand cmd2 = new SqlCommand("select ID,Title,[Content] from shops", conn); SqlDataReader reader2 = cmd2.ExecuteReader(); //string sql2 = "select ID,Title,[Content] from shops"; //SqlDataReader reader2 = helper.GetDataReader(sql2); while (reader2.Read()) { //域的集合:文档,类似于表的行 Document doc = new Document(); //要索引的字段 doc.Add(new Field("Title", reader2["Title"].ToString(), Field.Store.YES, Field.Index.ANALYZED)); doc.Add(new Field("Content", reader2["Content"].ToString(), Field.Store.YES, Field.Index.ANALYZED)); doc.Add(new Field("URL", "/Shop.aspx?ID=" + reader2["ID"].ToString() + "", Field.Store.YES, Field.Index.ANALYZED)); indexWriter.AddDocument(doc); } reader2.Close();
SqlCommand cmd3 = new SqlCommand("select ID,Position,[Describe] from Recruit", conn); SqlDataReader reader3 = cmd3.ExecuteReader(); //string sql3 = "select ID,Position,[Describe] from Recruit"; //SqlDataReader reader3 = helper.GetDataReader(sql3); while (reader3.Read()) { //域的集合:文档,类似于表的行 Document doc = new Document(); //要索引的字段 doc.Add(new Field("Title", reader3["Position"].ToString(), Field.Store.YES, Field.Index.ANALYZED)); doc.Add(new Field("Content", reader3["Describe"].ToString(), Field.Store.YES, Field.Index.ANALYZED)); doc.Add(new Field("URL", "/Recruit.aspx", Field.Store.YES, Field.Index.ANALYZED)); indexWriter.AddDocument(doc); } reader3.Close(); //对索引文件进行优化 indexWriter.Optimize(); indexWriter.Close();
}
}