將lucene.net用 window service 定時的 生成的索引文件 以便在使用中不浪費建立索引的時間
在分詞方面我選擇的是盤古的分詞 聽說是最好的.
當然在這 我使用的是控制檯程序,如果想換成window service 的話 可以將main方法中的代碼放到window service 中的onstart方法中就行了
如果不知道創建服務 和安裝服務的朋友可以查看我上一篇文章.window servicve 的相關內容
具體的使用情況是:
//建立索引字段using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OracleClient;
using System.Data;
using WindowsService2;
using Lucene.Net;
using Lucene.Net.Analysis.PanGu;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.Search;
using PanGu;
using PanGu.HighLight;
using PanGu.Dict;
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.QueryParsers;
using Lucene.Net.Store;
using Lucene.Net.Util;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
PanGuAnalyzer analyzer = null;
IndexWriter Writer = null;
try
{
//analyzer = new PanGuAnalyzer();
//Writer = new IndexWriter(@"D:\MyIndex", new PanGuAnalyzer(), true);//實例化一個寫入器(盤古分詞器)
//Writer = new IndexWriter(Indexpath, new WhitespaceAnalyzer(), true);//實例化一個寫入器(lucene.net自帶分詞)
//Document dm = new Document();//實例化文檔對象
string sql = "SELECT * FROM V_PROCUREMENT";
DataSet ds = DBserver.Query(sql);
foreach (DataRowView drv in ds.Tables[0].DefaultView)
{
Document Doc = new Document();
Doc.Add(new Field("title", drv["title"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
sDoc.Add(new Field("content", drv["content"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
Doc.Add(new Field("name", drv["name"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
Doc.Add(new Field("corporation_cname", drv["corporation_cname"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
Writer.AddDocument(Doc);
}
Writer.Optimize();
Writer.Close(); //創建索引結束
}
catch
{
throw;
}
}
}
}
看起來以前都是如此的簡單和明瞭 結果很好,建立服務.....直到:啓動服務的時候出現了問題.服務器啓動了又停止了....什麼什麼的,這說明什麼呢?
說明寫服務代碼本身就有問題所以導致服務無法啓動.
那是什麼問題呢?
通過把代碼複製到控制檯程序中運行 才發現:錯誤是:
未能找到路徑“C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\windows Sevcer\ConsoleApplication1\bin\Dictionaries\Dict.Dct”的一部分。 說明什麼呢? 我們缺少一個dict.dct的文件 這個文件時盤古內置的文件.
還有一個文件:pangu.xml 也是必須的文件
這樣成成功的啓動了項目;
將文件放置在該放置的位置 這樣就啓動成功了.
就是這樣了.