62,041
社区成员
发帖
与我相关
我的任务
分享
//建立索引时
document.Add(new Field("title", title, Field.Store.YES, Field.Index.ANALYZED));
document.Add(new Field("content", content, Field.Store.YES, Field.Index.ANALYZED));
// 查询
IndexSearcher searcher = new IndexSearcher(reader);
Lucene.Net.Search.Sort sort = new Lucene.Net.Search.Sort();
Lucene.Net.Search.SortField f1= new SortField("longtime", SortField.LONG, true);
string keywords = GetKeyWords(key, new PanGuTokenizer());
sort.SetSort(new SortField[] { f1});
string[] fields = { "title" };
QueryParser queryParser = new MultiFieldQueryParser(version, fields, analyzer);
BooleanQuery bq = new BooleanQuery();
bq.Add(query, Occur.MUST);
TopDocs t_docs = searcher.Search(bq, null, 50, sort);
ScoreDoc[] docs = t_docs.ScoreDocs;
//查询时的分词函数
public static string GetKeyWords(string keywords, PanGuTokenizer ktTokenizer)
{
StringBuilder result = new StringBuilder();
ICollection<WordInfo> words = ktTokenizer.SegmentToWordInfos(keywords);
foreach(WordInfo word in words)
{
if(word == null)
{
continue
;
}
result.AppendFormat("{0}^{1}.0 ", word.Word, (int)Math.Pow(3, word.Rank));
}
return
result.ToString().Trim();
}