我最近在研究用Lucene做网站搜索功能,遇到如下问题,代码如下
创建索引
private static void CreateIndex()
{
string path = @"d:\search\";
IndexWriter writer = new IndexWriter(path, new StandardAnalyzer(),true);
Document doc = new Document();
string strSQL = "SELECT TOP 100 title FROM Article ";
DataTable dt = SqlHelper.ExecuteDataTable(strSQL);
foreach (DataRow dr in dt.Rows)
{
doc.Add(new Field("title", dr["title"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
writer.AddDocument(doc);
}
writer.Optimize();
writer.Close();
}
搜索
private static void Search(string words)
{
string path = @"d:\search\";
IndexSearcher searcher = new IndexSearcher(path);
Query query = new QueryParser("title", new StandardAnalyzer()).Parse(words);
Hits hits = searcher.Search(query);
Console.WriteLine(hits.Length());
for (int i = 0; i < hits.Length(); i++)
{
Document doc = hits.Doc(i);
string title = doc.Get("title").ToString();
Console.WriteLine(title);
}
searcher.Close();
}
现在的问题是,我无论输入的什么关键词,都返回上面SQL查询的第一列的title
请问是哪里写错了,请指教。
------解决方案--------------------------------------------------------
好久不弄了,帮你UP下