请教:使用.net lucene时索引建立了,但搜索的时候,没有结果,hits.length显示为0
建立索引后可以提示索引中有document记录,但hits.length()为0
代码如下
//建立检索
private void indexYiru(IndexWriter indexD)
{
string select="select * from chanpin" ; SqlCommand comm=new SqlCommand(select,this.conn);
SqlDataReader dr=comm.ExecuteReader();
while(dr.Read())
{
Document doc = new Document();
doc.Add(Field.Keyword("id", dr["id"].ToString()));//
doc.Add(Field.Keyword("mingcheng", dr["mingcheng"].ToString()));//名称
doc.Add(Field.Text("xinghao", dr["xinghao"].ToString()));//型号
doc.Add(Field.Text("jieshao", dr["jieshao"].ToString()));//介绍
indexD.AddDocument(doc);
}
dr.Close();
}
//检索按钮的代码
private void buttonSearch_Click(object sender, System.EventArgs e)
{
try
{
searcher = new IndexSearcher(this.pathIndex);
this.listViewResults.Items.Clear();
if (this.textBoxQuery.Text.Trim(new char[] {' '}) == String.Empty)
return;
Query query = QueryParser.Parse(this.textBoxQuery.Text, "mingcheng", new StandardAnalyzer());
Hits hits = searcher.Search(query);
MessageBox.Show(hits.Length().ToString());
MessageBox.Show(this.textBoxQuery.Text);
for (int i = 0; i < hits.Length(); i++)
{
Document doc = hits.Doc(i);
string id=doc.Get("id");
string name=doc.Get("mingcheng");
string xinghao=doc.Get("xinghao");
string jieshao=doc.Get("jieshao");
ListViewItem item = new ListViewItem(new string[] {null, id, mingcheng, xinghao,jieshao,hits.Score(i).ToString()});
item.Tag=id;
this.listViewResults.Items.Add(item);
Application.DoEvents();
}
searcher.Close();
}
catch(IOException ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
另外有一个地方不太清楚
Query query = QueryParser.Parse(this.textBoxQuery.Text, "mingcheng", new StandardAnalyzer());
中的第二个参数的意思是不是默认的检索字段,它对搜索结果又没有影响,是不是随便设一个字段都可以