Lucenne+盘古分词,FuzzyQuery,WildcardQuery 查询不了字母

love.李 2014-10-11 05:39:39
Lucenne+盘古分词,FuzzyQuery,WildcardQuery 查询不了字母,查询数字是有结果 有,当查询关键字有字母是没有结果 。新手,请指教
索引生成:

public bool CreateIndex(IndexWriter writer, MySearchUnit data)
{
try
{

if (data == null) return false;
Document doc = new Document();
Type type = data.GetType();//assembly.GetType("Reflect_test.PurchaseOrderHeadManageModel", true, true); //命名空间名称 + 类名

//创建类的实例
//object obj = Activator.CreateInstance(type, true);
//获取公共属性
PropertyInfo[] Propertys = type.GetProperties();
for (int i = 0; i < Propertys.Length; i++)
{
//Propertys[i].SetValue(Propertys[i], i, null); //设置值
PropertyInfo pi = Propertys[i];
string name = pi.Name;
object objval = pi.GetValue(data, null);
string value = objval == null ? "" : objval.ToString(); //值
if (name == "id" || name == "flag")//id在写入索引时必是不分词,否则是模糊搜索和删除,会出现混乱
{
doc.Add(new Field(name, value, Field.Store.YES, Field.Index.NOT_ANALYZED));//id不分词
}
else
{
doc.Add(new Field(name, value, Field.Store.YES, Field.Index.ANALYZED));
}
}
writer.AddDocument(doc);
writer.Close();
}
catch (System.IO.FileNotFoundException fnfe)
{
throw fnfe;
}
return true;
}

查询 (部分):
public void Search0(string keyword)
{

IndexSearcher searcher = new IndexSearcher(directory_luce, true);

//构建一个Term,然后对其进行模糊查找
Term t = new Term("title", keyword);
//FuzzyQuery query = new FuzzyQuery(t);
//FuzzyQuery还有两个构造函数,来限制模糊匹配的程度
// 在FuzzyQuery中,默认的匹配度是0.5,当这个值越小时,通过模糊查找出的文档的匹配程度就
// 越低,查出的文档量就越多,反之亦然
//FuzzyQuery query1 = new FuzzyQuery(t, 0.5f, 3);
//FuzzyQuery query2 = new FuzzyQuery(t, 0.0f);
TopScoreDocCollector collector = TopScoreDocCollector.Create(110, false);




WildcardQuery query = new WildcardQuery(t);

// WildcardQuery query = new WildcardQuery(t);
searcher.Search(query, (Filter)null, collector);

ScoreDoc[] hits = collector.TopDocs(1, 10).ScoreDocs;
List<MySearchUnit> list = new List<MySearchUnit>();
int counter = 1;
int TotalCount = collector.TotalHits;
foreach (ScoreDoc sd in hits)//遍历搜索到的结果
{。。。。
...全文
140 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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