using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Lucene.Net;
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Index;
using Lucene.Net.Store;
using Lucene.Net.Documents;
using Lucene.Net.Search;
using Lucene.Net.QueryParsers;
StandardAnalyzer standardAnalyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29);
IndexWriter indexWriter = new IndexWriter(fsDirectory, standardAnalyzer, true, IndexWriter.MaxFieldLength.LIMITED);
FileInfo fileInfo = new FileInfo(@"d:\search\射雕英雄传.txt");
using (StreamReader streamReader = new StreamReader(fileInfo.FullName, Encoding.Default))
{
while (!streamReader.EndOfStream)
{
string content = streamReader.ReadLine();
if (string.IsNullOrEmpty(content))
{
continue;
}
Document document = new Document();
document.Add(new Field("content", content, Field.Store.YES, Field.Index.ANALYZED));
indexWriter.AddDocument(document);
}
}
indexWriter.Optimize();
indexWriter.Close();
IndexSearcher indexSearcher = new IndexSearcher(fsDirectory, true);
Query query = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "content", standardAnalyzer).Parse("欧阳锋");
TopDocs topDocs = indexSearcher.Search(query, 10);
for (int i = 0;i < 10;++i)
{
if (topDocs.ScoreDocs[i] == null)
{
break;
}
Document temp = indexSearcher.Doc(topDocs.ScoreDocs[i].doc);
Console.WriteLine(temp.GetField("content").StringValue());
}
indexSearcher.Close();
下为盘古分词版===========================================
Segment.Init();
PanGuAnalyzer analyzer = new PanGuAnalyzer();
//StandardAnalyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29);
FSDirectory fsDirectory = FSDirectory.Open(new DirectoryInfo(@"d:\search\"), new NoLockFactory());
IndexWriter indexWriter = new IndexWriter(fsDirectory, analyzer, true, IndexWriter.MaxFieldLength.LIMITED);
FileInfo fileInfo = new FileInfo(@"d:\search\射雕英雄传.txt");
using (StreamReader streamReader = new StreamReader(fileInfo.FullName, Encoding.Default))
{
while (!streamReader.EndOfStream)
{
string content = streamReader.ReadLine();
if (string.IsNullOrEmpty(content))
{
continue;
}
Document document = new Document();
document.Add(new Field("content", content, Field.Store.YES, Field.Index.ANALYZED));
indexWriter.AddDocument(document);
}
}
indexWriter.Optimize();
indexWriter.Close();
IndexSearcher indexSearcher = new IndexSearcher(fsDirectory, true);
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
Term t = new Term("content", "欧阳锋");
Query query = new TermQuery(t);
TopDocs topDocs = indexSearcher.Search(query, 100);
stopwatch.Stop();
Console.WriteLine(stopwatch.Elapsed.ToString());
indexSearcher.Close();