我用的lucene全文检索在给索引库指定目录,有的时候会提示对文件夹无法访问,所以报错
我是创建索引是这样建的,那错了啊
谢谢
//创建索引
private IndexWriter CreateIndex()
{
IndexWriter writer = null;
try
{
writer = new IndexWriter(Server.MapPath("~/Lucene/goods"), new StandardAnalyzer(), true);//////////
if (Convert.ToInt32(this.ddlInfo.SelectedValue) == 0)///////////
{
IList<Goods> goods = GoodsManager.GetAllGoods();////////////
foreach (Goods g in goods)
{
Document doc = new Document();
doc.Add(new Field("id", g.Id.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
doc.Add(new Field("CompanyID", g.CompanyID.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
doc.Add(new Field("goodsname", g.GoodsName, Field.Store.YES, Field.Index.TOKENIZED));
writer.AddDocument(doc);
}
}
writer.Optimize();
}
catch (Exception e)
{
Server.Execute("~/default.aspx");
}
finally
{
writer.Close();
}
return writer;
}
public Hits Search()
{
Hits hits = null;
IndexSearcher mysea = new IndexSearcher(Server.MapPath("~/Lucene/goods"));//////////////
//对单一字段搜索
QueryParser parser = new QueryParser("goodsname", new StandardAnalyzer());/////////////////
Query query = parser.Parse(this.txtKey.Text.Trim());
//Query query = MultiFieldQueryParser.Parse(new string[] { this.TextBox1.Text.Trim() }, new string[] { "title" }, new YurowAnalyzer.YurowAnalyzer("f:\\index"));
hits = mysea.Search(query);
return hits;
}
protected void imgSearch_Click(object sender, ImageClickEventArgs e)
{
if (this.ddlInfo.SelectedIndex == 0)
{
if (this.txtKey.Text.Trim() == "")
{
return;
}
//IndexWriter writer = CreateIndex();
IList<Goods> goods = new List<Goods>();
Hits hits = Search();
//Response.Write("关于搜索到" + hits.Length() + "条记录!");
if (hits != null)
{
for (int i = 0; i < hits.Length(); i++)
{
Document doc = hits.Doc(i);
Goods g = new Goods();
g.Id = Convert.ToInt32(doc.Get("id"));
g.CompanyID = Convert.ToInt32(doc.Get("CompanyID"));
g.GoodsName = Convert.ToString(doc.Get("goodsname"));
goods.Add(g);
}
}
//Session["goods"] = "";
Session["goods"] = goods;
Response.Redirect("~/searchGoods.aspx");
}
}
检查你的文件夹是不是有 aspnet 这个账号的读写权限
有时会出错,可能是防火墙的原因。我的服务器上索引文件以前老是被norton罩住。