Lucene如何进行查询?
最近在研究Lucene分词查询,仿照别人的例子写了一段代码,但是怎么也差不出数据,不知道怎么回事,特地来请教大家
Sort sortTaxis = new Sort(new SortField[] { new SortField("CGUID",SortField.STRING, true) });
IndexSearcher search = new IndexSearcher("F:\\Index");
int _n = search.MaxDoc();//这里可以看到加载是没有问题的,总记录数2000条
BooleanQuery m_BooleanQuery = new BooleanQuery();
//下面是自己加的两个查询条件
if (this.txtAge.Text.Length>0)
{
QueryParser queryParser = new QueryParser("Age", new Lucene.Net.Analysis.KTDictSeg.KTDictSegAnalyzer(true));
Query queryAge = queryParser.Parse(this.txtAge.Text);
m_BooleanQuery.Add(queryAge, BooleanClause.Occur.MUST);//and操作
}
if (this.txtKeyWord.Text.Length>0)
{
string keyWord = GetKeyWordsSplitBySpaceTo(this.txtKeyWord.Text, new Lucene.Net.Analysis.KTDictSeg.KTDictSegTokenizer());
keyWord = StrReplaceSymbol(keyWord);
QueryParser queryParser = new QueryParser("Name", new Lucene.Net.Analysis.KTDictSeg.KTDictSegAnalyzer(true));
Query query = queryParser.Parse(keyWord);
m_BooleanQuery.Add(query, BooleanClause.Occur.MUST);//and操作
}
Hits hits = search.Search(m_BooleanQuery, sortTaxis);//查询并得到结果
int recCount = hits.Length();//查询结果为0
//........
private Hits seacher(string flg, string lib)
{
Hits hits = null;
try
{
BooleanQuery bq = new BooleanQuery();
IndexSearcher search = RCH.Lucene.LuceneIndex.createsearcher("D:\\WebIndex\\Index_Risk\");
if (flg != "")
{
if (rbBt.Checked)
{
string keywords = RCH.Lucene.LuceneAnalyzer.pangukeys(flg, RCH.Lucene.LuceneAnalyzer.pangu_tokenizer());
QueryParser title_QueryParser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "doc_flg", new PanGuAnalyzer(true));
Query title_Query = title_QueryParser.Parse(keywords);
bq.Add(title_Query, BooleanClause.Occur.MUST);
}
else
{
string keywords = RCH.Lucene.LuceneAnalyzer.pangukeys(flg, RCH.Lucene.LuceneAnalyzer.pangu_tokenizer());
QueryParser title_QueryParser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "doc_info", new PanGuAnalyzer(true));
Query title_Query = title_QueryParser.Parse(keywords);
bq.Add(title_Query, BooleanClause.Occur.MUST);
}
}
QueryParser type_QueryParser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "doc_library", new PanGuAnalyzer(true));
Query type_Query = type_QueryParser.Parse(lib);
bq.Add(type_Query, BooleanClause.Occur.MUST);
hits = search.Search(bq);
}
catch
{
Response.Write("搜索失败!");
}
return hits;
}
DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, "SELECT top 2000 * From Customer");
//生成的索引路径
string indexDirectory = ConfigurationManager.AppSettings["IndexDir"];
//定义分析器
Analyzer KTDictSegAnalyzer = new Lucene.Net.Analysis.KTDictSeg.KTDictSegAnalyzer();
//创建IndexWriter
IndexWriter writer = new IndexWriter(indexDirectory, KTDictSegAnalyzer, true);
if (dt != null && dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
Document document = new Document();
document.Add(new Field("cguid", dt.Rows[i]["cguid"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
document.Add(new Field("name", dt.Rows[i]["name"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
document.Add(new Field("bankcard", dt.Rows[i]["bankcard"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
document.Add(new Field("age", dt.Rows[i]["age"].ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
//设置权重,权重值越高,排序越靠前
document.SetBoost(1.0f);
try
{
writer.AddDocument(document);//写入数据
}
catch (Exception ex)
{
}
}
}
writer.Optimize();
writer.Close();
dt.Dispose();