请教:使用.net lucene时索引建立了,但搜索的时候,没有结果,hits.length显示为0

Coostone 2006-08-23 12:09:09
建立索引后可以提示索引中有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());
中的第二个参数的意思是不是默认的检索字段,它对搜索结果又没有影响,是不是随便设一个字段都可以
...全文
178 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
wythust 2006-08-23
  • 打赏
  • 举报
回复
第二个问题:
QueryParser .parse(String query, String field, Analyzer analyzer)
query为检索词, field为检索的字段名, analyzer为分析器

第一个问题:
看下dr["id"].ToString()与this.textBoxQuery.Text的值,分析下就知道为什么不能匹配出来,你这里应该是没有分词情况下的匹配
sundeveloper 2006-08-23
  • 打赏
  • 举报
回复
StandardAnalyzer----> 中文的用CJKAnalyzer..

2,760

社区成员

发帖
与我相关
我的任务
社区描述
搜索引擎的服务器通过网络搜索软件或网络登录等方式,将Internet上大量网站的页面信息收集到本地,经过加工处理建立信息数据库和索引数据库。
社区管理员
  • 搜索引擎技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧