Пример #1
0
        /// <summary>
        /// Lucene并没有提供更新,这里的更新操作其实是如下两个操作的合集 先删除之后再添加
        /// </summary>
        /// <param name="mySearchUnit"></param>
        /// <returns></returns>
        #endregion
        public bool Update(MySearchUnit mySearchUnit, out bool IsDeleted)
        {
            bool          IsSuccess = false;
            IndexWriter   writer    = null;
            IndexSearcher searcher  = new IndexSearcher(directory_luce, true);

            try
            {
                writer = new IndexWriter(directory_luce, analyzer, false, IndexWriter.MaxFieldLength.LIMITED);//false表示追加(true表示删除之前的重新写入)
            }
            catch
            {
                writer = new IndexWriter(directory_luce, analyzer, true, IndexWriter.MaxFieldLength.LIMITED);//false表示追加(true表示删除之前的重新写入)
            }
            finally
            {
                IsDeleted = Delete(mySearchUnit.id, writer);
                bool iscreate = CreateIndex(writer, mySearchUnit);
                if (IsDeleted && iscreate)
                {
                    IsSuccess = true;
                }
                writer.Optimize();
                writer.Dispose();
            }
            return(IsSuccess);
        }
Пример #2
0
        public bool CreateIndex(IndexWriter writer, MySearchUnit data)
        {
            try
            {
                if (data == null)
                {
                    return(false);
                }
                Document doc  = new Document();
                Type     type = data.GetType();//assembly.GetType("Reflect_test.PurchaseOrderHeadManageModel", true, true); //命名空间名称 + 类名

                //创建类的实例
                //object obj = Activator.CreateInstance(type, true);
                //获取公共属性
                PropertyInfo[] Propertys = type.GetProperties();
                for (int i = 0; i < Propertys.Length; i++)
                {
                    //Propertys[i].SetValue(Propertys[i], i, null); //设置值
                    PropertyInfo pi     = Propertys[i];
                    string       name   = pi.Name;
                    object       objval = pi.GetValue(data, null);
                    string       value  = objval == null ? "" : objval.ToString();                  //值
                    if (name == "id" || name == "flag")                                             //id在写入索引时必是不分词,否则是模糊搜索和删除,会出现混乱
                    {
                        doc.Add(new Field(name, value, Field.Store.YES, Field.Index.NOT_ANALYZED)); //id不分词
                    }
                    else
                    {
                        doc.Add(new Field(name, value, Field.Store.YES, Field.Index.ANALYZED));
                    }
                }
                writer.AddDocument(doc);
            }
            catch (System.IO.FileNotFoundException fnfe)
            {
                throw fnfe;
            }
            return(true);
        }