根据java版的lucene的德莫,用C#写的样例
FileProcessMain.cs
- namespace com.cplatform.lucene.main
- {
- using InDexProcesser = com.cplatform.lucene.index.IndexProcesser;
-
- public class FileProcessMain
- {
-
- public static void Main(string[] args)
- {
-
- string inputFile = "哈利波特.txt";
- string outputDir = "f:\\lucene\\index\\";
-
- if (!System.IO.Directory.Exists(outputDir))
- {
- System.IO.Directory.CreateDirectory(outputDir);
- }
-
- InDexProcesser index = new InDexProcesser();
- index.createIndex(inputFile);
-
- }
-
- }
-
- }
复制代码
IndexProcesser.cs
- using System;
- using System.IO;
- using Lucene.Net.Analysis.Cn;
-
- namespace com.cplatform.lucene.index
- {
-
- using Analyzer = Lucene.Net.Analysis.Cn.ChineseAnalyzer;
- using Document = Lucene.Net.Documents.Document;
- using Field = Lucene.Net.Documents.Field;
- using IndexWriter = Lucene.Net.Index.IndexWriter;
- using FSDirectory = Lucene.Net.Store.SimpleFSDirectory;
- using MaxFieldLength = Lucene.Net.Index.IndexWriter.MaxFieldLength;
- using StringField = Lucene.Net.Search.SortField;
-
- public class IndexProcesser
- {
-
- private string INDEX_STORE_PATH = "f:\\lucene\\index\\";
-
- public virtual int createIndex(string inputDir)
- {
- int numIndexed = 0;
- try
- {
- Analyzer analyzerWrapper = new Analyzer();
- FSDirectory indexStore = new FSDirectory(new DirectoryInfo(INDEX_STORE_PATH));
- MaxFieldLength m = new MaxFieldLength(100);
- IndexWriter writer = new IndexWriter(indexStore, analyzerWrapper ,m);
-
- indexFile(writer, new FileInfo(inputDir));
- writer.Close();
-
- }
- catch (Exception e)
- {
- Console.WriteLine(e.ToString());
- Console.Write(e.StackTrace);
- }
-
- return numIndexed;
-
- }
-
-
-
- private static void indexFile(IndexWriter writer, FileInfo f)
- {
-
- Console.WriteLine("Indexing " + f.Name);
- Document doc = new Document();
- doc.Add(new Field("filename", f.Name, Field.Store.YES ,Field.Index.ANALYZED));
- doc.Add(new Field("path", f.CreationTime.ToString(), Field.Store.YES, Field.Index.ANALYZED));
- writer.AddDocument(doc);
-
- }
-
-
-
- }
-
- }
复制代码
|
|