代码改变世界

l u c e n e 创 建 修 改 删 除 索 引

2011-11-03 15:56  yuejianjun  阅读(246)  评论(0编辑  收藏  举报
using System;
using System.Collections.Generic;
using System.Text;
using Business;
using Lucene.Net.Index;
using Lucene.Net.Documents;
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.PanGu;
using Lucene.Net.Search;
using Lucene.Net.Store;

namespace Index
{
    class Program
    {
        private  static string path = @"D:\Work\HotelIndex";
        static void Main(string[] args)
        {
            CreateIndex();

            Search();
            Update();
            Search();
            Delete();
            Search();


            Console.Read();
        }
        private static void Update()
        {
            IndexWriter writer = new IndexWriter(path, new PanGuAnalyzer());
            Term term = new Term("keyid""3");
            //Term term = new Term("datapart", "sd");
            string title = "datapartxiu酒店酒店";
            writer.UpdateDocument(term, CreateDataEntityIndex(title));
            writer.Commit();
            //writer.Optimize();
            writer.Close();
        }
        private static void Delete()
        { 
            IndexReader reader = IndexReader.Open(FSDirectory.Open(new System.IO.FileInfo(path)), false);
 
            //Term term = new Term("keyid", "196");
            Term term = new Term("keyid""3"); 
            reader.DeleteDocuments(term);
            reader.Commit(); 
            reader.Close();
        }
        private static void Search()
        {
            TopScoreDocCollector collector = TopScoreDocCollector.create(10false);
            IndexSearcher searcher = new IndexSearcher(path );
            BooleanQuery booleanQuery = new BooleanQuery(); 
            BooleanQuery keyQuery = new BooleanQuery();
            keyQuery.Add(new TermQuery(new Term("title""酒店")), BooleanClause.Occur.SHOULD);
            booleanQuery.Add(keyQuery, BooleanClause.Occur.MUST);
            Query query = booleanQuery; 
            searcher.Search(query, null, collector);
            ScoreDoc[] scoreDocArr = collector.TopDocs(010).scoreDocs;
            for (int i = 0; i < scoreDocArr.Length; i++)
            {
                ScoreDoc scoreDoc = scoreDocArr[i];
                int docId = scoreDoc.doc;//拿到搜到的文档ID

                Document doc = searcher.Doc(docId);//根据文档ID创建DOCUMENT 
                 string id = doc.Get("keyid").ToString();
                 string title = doc.Get("title").ToString();
                 Console.WriteLine("id: " + id + "   title: " + title);
            }
            searcher.Close();
            Console.WriteLine("------------------------------");
        }
        private static void CreateIndex()
        {
            IndexWriter writer = new IndexWriter(path, new PanGuAnalyzer());
            string title = "希尔顿酒店s";
            Document doc = CreateDataEntityIndex(title,"2");
            writer.AddDocument(doc);

            doc = CreateDataEntityIndex(title,"3");
            writer.AddDocument(doc);

            writer.Close();
        }
        private static Document CreateDataEntityIndex(string title,string keyid="1")
        {
            Field field;
            Document doc = new Document();
            field = new Field("title", title, Field.Store.YES, Field.Index.TOKENIZED);//存储, 索引 
            doc.Add(field);
            field = new Field("data","sss", Field.Store.NO, Field.Index.TOKENIZED);//不存储( 不能在搜索结果中用doc.get("data").toString() 读取 ), 索引
            
//field.SetBoost(dataEntity.Content.EntityBoost ); 
            doc.Add(field);
            field = new Field("datapart""sd", Field.Store.YES, Field.Index.UN_TOKENIZED);//存储,不索引
            
//field.SetBoost(dataEntity.Content.EntityBoost ); 
            doc.Add(field);
            field = new Field("mainurl",  "sd",  Field.Store.YES, Field.Index.UN_TOKENIZED);
            doc.Add(field);
            //field = new Field("districtid", "12112", Field.Store.YES, Field.Index.UN_TOKENIZED);
            
//doc.Add(field);
            field = new Field("keyid", keyid, Field.Store.YES, Field.Index.UN_TOKENIZED);
            doc.Add(field);
            field = new Field("ctripscore""1", Field.Store.YES, Field.Index.UN_TOKENIZED);
            doc.Add(field);
            field = new Field("districtname""1", Field.Store.YES, Field.Index.UN_TOKENIZED);
            doc.Add(field);
            field = new Field("datatype""1", Field.Store.YES, Field.Index.UN_TOKENIZED);
            doc.Add(field);
            field = new Field("districtpath""1", Field.Store.YES, Field.Index.UN_TOKENIZED);
            doc.Add(field);
            //double boost = double.Parse(dataEntity.CtripScore) + 1;
            
//boost = Math.Sqrt(boost);
            
//field = new Field("ctripscore", boost.ToString(), Field.Store.YES, Field.Index.TOKENIZED);
            
//doc.Add(field);
            
//doc.SetBoost(float.Parse(boost.ToString()));//提高其中一条数据的权重,如广告 
            return doc;
        }
    }
}