Update Document

rusli
Site Admin
文章: 212
註冊時間: 週三 7月 07, 2010 9:49 pm

Update Document

文章rusli » 週四 9月 13, 2012 11:15 am

代碼: 選擇全部

         UpdateByDocId( new DocumentModel()  , 1);


代碼: 選擇全部

    public class DocumentModel
   {
        public DocumentModel()
        {
            Fields = new List<Field>();
            this.Id = System.Guid.NewGuid().ToString();
        }

        public DocumentModel(int docId)
        {
            Fields = new List<Field>();
            Document doc = Index.GetDocument(docId);
            this.Title = doc.Get("Title");
            this.Id = doc.Get("Id");
            this.Content = doc.Get("Content");
            this.Privilege = doc.Get("Privilege");
            this.DocId = docId.ToString();
        }

        public IList<string> ShareType { get; set; }

        public IList<string> SpecifiedUsers { get; set; }

        public IList<string> SpecifiedTeams { get; set; }

        public IList<Field> Fields { get; set; }

        public string Id { get; set; }

        public string Privilege { get; set; }

        public string Title { get; set; }

        public string Content { get; set; }

        public string DocId { get; set; }

        public Term Term { get; set; }


        public void Update()
        {
            int docId;
            if (int.TryParse(this.DocId, out docId))
            {
                CreateFields();
                Index.UpdateByDocId(this, docId);
            }
        }

        private void CreateFields()
        {
            Term = new Term("Id", this.Id);
            this.Fields.Add(new Field("Id", this.Id, Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));

            /*
             需要分詞並建立索引
             Index.ANALYZED – use the analyzer to break the Field’s value into a stream of separate tokens
                and make each token searchable. This is useful for normal text fields (body, title, abstract, etc.).

             不分詞,直接建立索引
             Index.NOT_ANALYZED – do index the field, but do not analyze the String.
             
             不建立索引
             Index.NO – don’t make this field’s value available for searching at all.
             
             */

            // Add title field
            var titleField = new Field("Title", this.Title, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS);
            titleField.SetBoost(3.0f);
            this.Fields.Add(titleField);

            if (this.ShareType != null)
            {
                var privilege = new Field("Privilege", string.Empty, Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS);

                if (this.ShareType.Contains("OnlyMe"))
                {
                    privilege = new Field("Privilege", "OnlyMe", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS);
                    Privilege = "OnlyMe";
                    this.Fields.Add(privilege);
                }

                // Everyone
                if (this.ShareType.Contains("Everyone"))
                {
                    privilege = new Field("Privilege", "Everyone", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS);
                    Privilege = "Everyone";
                    this.Fields.Add(privilege);
                }

                //// 群組
                if (this.ShareType.Contains("SpecifiedTeams"))
                {
                    foreach (var team in this.SpecifiedTeams)
                    {
                        privilege = new Field("Privilege", team, Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS);
                        Privilege = team;
                        this.Fields.Add(privilege);
                    }
                }

                //// 使用者
                if (this.ShareType.Contains("SpecifiedUsers"))
                {
                    foreach (var user in this.SpecifiedUsers)
                    {
                        privilege = new Field("Privilege", user, Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS);
                        Privilege = user;
                        this.Fields.Add(privilege);
                    }
                }
            }

            this.Fields.Add(new Field("Content", this.Content, Field.Store.COMPRESS, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
        }

        public void Delete()
        {
            CreateFields();
            Index.DeleteByTerm(this.Term);
        }

        public void Create()
        {
            // skip blank documents, they are worthless to us (even though they have a title we could index)
            if (string.IsNullOrWhiteSpace(Content) || string.IsNullOrWhiteSpace(Title))
            {
                return;
            }

            CreateFields();
            Index.Create(this);
        }
    }


代碼: 選擇全部


    public static void UpdateByDocId(DocumentModel c, int docId)
        {
            GetSearcher();
            GetWritter();
            Writer.SetUseCompoundFile(false);

            try
            {
                Document doc = Searcher.Doc(docId);

                IList<Field> replacementFields = c.Fields;
                foreach (Field field in replacementFields)
                {
                    string name = field.Name();
                    string currentValue = doc.Get(name);
                    if (currentValue != null)
                    {
                        // replacement field value
                        // remove all occurrences of the old field
                        doc.RemoveFields(name);

                        // insert the replacement
                        doc.Add(field);
                    }
                    else
                    {
                        // new field
                        doc.Add(field);
                    }
                }

                Writer.DeleteDocuments(c.Term);
                // write the old document to the index with the modifications
                Writer.UpdateDocument(c.Term, doc);
            }
            finally
            {
                if (Searcher != null)
                {
                    Searcher.Close();
                }

                if (Writer != null)
                {
                    Writer.SetUseCompoundFile(true);
                    Writer.Commit();
                    Writer.Optimize();
                    Writer.Close();
                }
            }
        }


回到「Lucene .Net」

誰在線上

正在瀏覽這個版面的使用者:沒有註冊會員 和 1 位訪客