Beispiel #1
0
        /// <summary>
        /// 将关键字进行高亮显示
        /// </summary>
        /// <param name="keywords">属性和关键字的字典</param>
        /// <param name="t">高亮显示的对象</param>
        /// <returns></returns>
        public AbstractFileBase InitHightLight(Dictionary <string, string> keywords, AbstractFileBase t)
        {
            if (t == null)
            {
                throw new Exception("需要高亮显示的对象不存在");
            }
            SimpleHTMLFormatter simpleHTMLFormatter = new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"red\">", "</font>");
            Highlighter         highlighter         = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new Segment());

            highlighter.FragmentSize = MAXFRAGMENTSIZE;
            foreach (var item in hightLightFields)
            {
                switch (item)
                {
                //case HightLightField.FileName:
                //    string fileName = string.Empty;
                //    keywords.TryGetValue("fileName", out fileName);
                //    if (!string.IsNullOrWhiteSpace(fileName))
                //    {
                //        t.FileName = highlighter.GetBestFragment(fileName, t.FileName);
                //    }
                //    break;
                //case HightLightField.FilePath:
                //    string filePath = string.Empty;
                //    keywords.TryGetValue("filePath", out filePath);
                //    if (!string.IsNullOrWhiteSpace(filePath))
                //    {
                //        t.FilePath = highlighter.GetBestFragment(filePath, t.FilePath);
                //    }
                //    break;
                //case HightLightField.CreateTime:
                //    string createTime = string.Empty;
                //    keywords.TryGetValue("createTime", out createTime);
                //    if (!string.IsNullOrWhiteSpace(createTime))
                //    {
                //        t.CreateTime = highlighter.GetBestFragment(createTime, t.CreateTime);
                //    }
                //    break;
                case HightLightField.Content:
                    string content = string.Empty;
                    keywords.TryGetValue("content", out content);
                    if (!string.IsNullOrWhiteSpace(content))
                    {
                        //有问题
                        t.Content = highlighter.GetBestFragment(content, t.Content);
                    }
                    break;
                    //case HightLightField.UpLoadPersonId:
                    //    string upLoadPersonId = string.Empty;
                    //    keywords.TryGetValue("upLoadPersonId", out upLoadPersonId);
                    //    if (!string.IsNullOrWhiteSpace(upLoadPersonId))
                    //    {
                    //        //有问题
                    //        t.UpLoadPersonId = highlighter.GetBestFragment(upLoadPersonId, t.UpLoadPersonId);
                    //    }
                    //    break;
                }
            }
            return(t);
        }
Beispiel #2
0
        /// <summary>
        /// 查询索引
        /// </summary>
        /// <param name="key">关键字</param>
        /// <param>人员权限</param>
        /// <returns></returns
        public ResultSet SearchIndex(string key, int pageNum, string userHandle, string lastFileId, string nextFileId)
        {
            LibHandle handle = LibHandleCache.Default.GetCurrentHandle(userHandle) as LibHandle;
            //页大小
            int           pageSize          = 8;
            IHightLighter hightLighter      = new HightLighterFactory().Create();
            IIndexManager indexManager      = new IndexManagerFactory().Create();
            Dictionary <string, string> dic = new Dictionary <string, string>();

            dic.Add("content", key);
            SearchResult result = null;

            if (string.IsNullOrEmpty(lastFileId) && string.IsNullOrEmpty(nextFileId))//第一次查询
            {
                result = (SearchResult)indexManager.SearchIndex(dic, pageNum, pageSize, handle);
            }
            else if (string.IsNullOrEmpty(lastFileId))//上一页
            {
                result = (SearchResult)indexManager.SearchPrevIndex(dic, pageNum, pageSize, handle, nextFileId);
            }
            else if (string.IsNullOrEmpty(nextFileId))//下一页
            {
                result = (SearchResult)indexManager.SearchNextIndex(dic, pageNum, pageSize, handle, lastFileId);
            }
            ResultSet resultSet = new ResultSet();

            resultSet.SearchTime   = result.SearchTime;
            resultSet.PageNum      = pageNum;
            resultSet.ResultsCount = result.TotalHits;
            resultSet.PageCount    = (resultSet.ResultsCount - 1) / pageSize + 1;
            DirLinkAddress dirlink;

            foreach (var item in result.Docs)
            {
                AbstractFileBase fileBase = hightLighter.InitHightLight(dic, item);
                FileInfoItem     info     = new FileInfoItem()
                {
                    FileId   = fileBase.FileId,
                    Contents = fileBase.Content
                };
                dirlink       = new DirLinkAddress(fileBase.FileId);
                info.FileName = dirlink.DocName;
                info.DirId    = dirlink.DirID;
                info.DirType  = dirlink.DirType;
                info.Path     = dirlink.DirNameLink;
                resultSet.FileInfoItems.Add(info);
            }
            if (resultSet.FileInfoItems.Count == 0)
            {
                return(null);
            }
            return(resultSet);
        }
Beispiel #3
0
        /// <summary>
        /// 删除索引
        /// </summary>
        /// <param name="fileInfo"></param>
        /// <returns></returns>
        public bool DeleteIndex(AbstractFileBase fileInfo)
        {
            try
            {
                IndexWriter writer = new IndexWriter(FSDirectory.Open(new System.IO.DirectoryInfo(IndexDic)), PanGuAnalyzer, IndexWriter.MaxFieldLength.LIMITED);
                writer.DeleteDocuments(new Term("fileId", fileInfo.FileId));
                //这两句一定要执行
                writer.Optimize();
                writer.Close();

                LibDataAccess dataAccess = new LibDataAccess();
                dataAccess.ExecuteNonQuery(string.Format("update DMDOCUMENT set ISFULLINDEX = 0 where DOCID = {0}", LibStringBuilder.GetQuotString(fileInfo.FileId)));
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Beispiel #4
0
        /// <summary>
        /// 创建索引
        /// </summary>
        /// <param name="fileInfo"></param>
        /// <returns></returns>
        public bool CreateIndex(AbstractFileBase fileInfo)
        {
            try
            {
                IndexWriter writer = new IndexWriter(FSDirectory.Open(new System.IO.DirectoryInfo(IndexDic)), PanGuAnalyzer, IndexWriter.MaxFieldLength.LIMITED);

                //FileStream fs = File.OpenRead(Path.Combine(fileInfo.FilePath, fileInfo.FileName));

                //1.创建Document对象
                Document doc = new Document();

                //2.给doc对象添加Field
                doc.Add(new Field("fileId", fileInfo.FileId, Field.Store.YES, Field.Index.NOT_ANALYZED));
                //doc.Add(new Field("fileName", fileInfo.FileName, Field.Store.YES, Field.Index.ANALYZED));
                //doc.Add(new Field("filePath", fileInfo.FilePath, Field.Store.YES, Field.Index.ANALYZED));
                //doc.Add(new Field("createTime", fileInfo.CreateTime, Field.Store.YES, Field.Index.ANALYZED));
                //doc.Add(new Field("upLoadPersonId", fileInfo.UpLoadPersonId, Field.Store.YES, Field.Index.ANALYZED));
                //doc.Add(new Field("content", new StreamReader(fs, Encoding.UTF8)));
                doc.Add(new Field("content", fileInfo.Content, Field.Store.YES, Field.Index.ANALYZED));

                //将doc对象写入索引文件
                writer.AddDocument(doc);

                writer.Optimize();
                writer.Commit();
                writer.Close();

                //修改数据库的
                LibDataAccess dataAccess = new LibDataAccess();
                dataAccess.ExecuteNonQuery(string.Format("update DMDOCUMENT set ISFULLINDEX = 1 where DOCID = {0}", LibStringBuilder.GetQuotString(fileInfo.FileId)));
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }