LGX.NET
漂泊...
using System;
using System.Collections.Generic;
using System.Text;

using Lucene.Net.Analysis;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.QueryParsers;
using Lucene.Net.Search;
using Lucene.Net.Store;

namespace LuceneTest
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
try
            {
                
//在内存中建立索引
                Directory directory = new RAMDirectory();
                
//生成分析器对象,用于分词
                Analyzer analyzer = new SimpleAnalyzer();
                
//索引书写器
                IndexWriter writer = new IndexWriter(directory, analyzer, true);
                
//将要建立索引的字符串
                string[] docs ={
                    
"a b c d e",
                    
"a b c d e a b c d e",
                    
"a b c d e f g h i j",
                    
"a f e",
                    
"d u o",
                    
"s l o p ix "
                };
                
//将要建立索引的字符串添加到索引中
                for (int i = 0; i < docs.Length; i++)
                {
                    Document d 
= new Document();
                    d.Add(Field.Text(
"contents",docs[i]));
                    writer.AddDocument(d);
                }
                writer.Close();

                
//生成搜索对象
                Searcher searcher = new IndexSearcher(directory);
                
//用于查询的字符串
                string[] queries = {"\"a b c\"",};
                
//生成结果集对象初始化为空
                Hits hits = null;

                
//生成QueryParser对象
                QueryParser parser = new QueryParser("contents", analyzer);
                
//依次用查询字符串生成查询对象
                for (int j = 0; j < queries.Length; j++)
                {
                    Query query 
= parser.Parse(queries[j]);
                    
//输出要查询的内容
                    Console.Out.WriteLine("Query:" + query.ToString("contents"));
                    
//返回结果集
                    hits = searcher.Search(query);
                    
//输出搜索到的总文档数
                    Console.Out.WriteLine("{0} total results:",hits.Length());
                    
//依次输出搜索到的文档内容
                    for (int i = 0; i < hits.Length() && i < 10; i++)
                    {
                        Document d 
= hits.Doc(i);
                        Console.Out.WriteLine(
"{0}  {1}  {2}",i,hits.Score(i),d.Get("contents"));
                    }


                }
                searcher.Close();
                Console.In.Read();
            }
            
catch (Exception e)
            {
                Console.Out.WriteLine(e.Message);
            }
        }
    }
}
posted on 2006-10-19 00:18  LGX.NET  阅读(141)  评论(0)    收藏  举报