看到网上一些资料lucene都是java版的,今天上午写了一个.net
首先下个lucene吧,网上一大堆,我不说了,我用的是1.9final那个版本,然后说添加引用
然后是
using Lucene.Net;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.QueryParsers;
using Lucene.Net.Search;
using Lucene.Net.Store;
using Lucene.Net.Util;
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Cn;
using Lucene.Net.Analysis.Standard;
把这些加进去,就可以写了
public void Index()
{
IndexWriter writer = new IndexWriter(directory,analyzer,true);
writer.maxFieldLength=1000;
for(int i=1;i<=100;i++)
{
Document document = new Document();
document.Add(new Field(FieldName,"name"+i,Field.Store.YES,Field.Index.UN_TOKENIZED));
document.Add(new Field(FieldValue,"Hello World",Field.Store.YES,Field.Index.TOKENIZED));
writer.AddDocument(document);
}
writer.Optimize();
writer.Close();
}
public void Search()
{
Query query = QueryParser.Parse("name*",FieldName,analyzer);
IndexSearcher searcher = new IndexSearcher(directory);
Hits hits = searcher.Search(query);
Console.WriteLine("符合条件记录:{0};索引库记录总数:{1}",hits.Length(),searcher.Reader.NumDocs());
for(int i=0;i {
int docid = hits.Id(i);
string name = hits.Doc(i).Get(FieldName);
string value = hits.Doc(i).Get(FieldValue);
float score = hits.Score(i);
Console.WriteLine("{0}: DocId:{1}; Name:{2}; Value:{3}; Score:{4}", i + 1, docid, name, value, score);
}
searcher.Close();
Console.WriteLine("查找结束!");
Console.ReadLine();
}
阅读(3579) | 评论(0) | 转发(0) |