using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

//全文索引所需引入的命名空间
using Lucene.Net.Index;
using Lucene.Net.Documents;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Search;
using Lucene.Net.QueryParsers;
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.PanGu;

namespace ATL.Search
{
    public class IndexWriter
    {
        //索引存放路径
        private string _path;

        public string Path
        {
            get { return _path; }
            set { _path = value; }
        }
      

        public void CreateIndex(string id, string title, string content, string kw, string url, string time)
        {
            //分词
            PanGuAnalyzer Analyzer = new PanGuAnalyzer();
            Lucene.Net.Index.IndexWriter writer = new Lucene.Net.Index.IndexWriter(Path, Analyzer, true);

            IndexString(writer,id, title, content, kw, url, time);
            //优化索引
            writer.Optimize();
            //关闭索引
            writer.Close();
            
        }
        private void IndexString(Lucene.Net.Index.IndexWriter writer,string id,string title,string content,string kw,string url,string time)
        {
            try
            {
                Document doc = new Document();
 
                doc.Add(new Field("id", id, Field.Store.YES, Field.Index.NO));
          
                doc.Add(new Field("url", url, Field.Store.YES, Field.Index.NO));
            
                doc.Add(new Field("time", time, Field.Store.YES, Field.Index.NO));
             
                doc.Add(new Field("title", title, Field.Store.YES, Field.Index.TOKENIZED));
            
                doc.Add(new Field("content", content, Field.Store.YES, Field.Index.TOKENIZED));

                doc.Add(new Field("kw", kw, Field.Store.YES, Field.Index.TOKENIZED));

                //写入索引
                writer.AddDocument(doc);
            }
            catch(Exception ex)
            { }

          
        }
    }
}
