Exemplo n.º 1
0
        /// <summary>
        /// 根据网站ID和关键字查询结果
        /// </summary>
        /// <param name="wenSiteIds"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public static List <ContentEntity> Search(string wenSiteIds, string keyword)
        {
            List <ContentEntity> mdoels = new List <ContentEntity>();

            if (!string.IsNullOrEmpty(wenSiteIds))
            {
                WebSiteApp    webSiteApp = new WebSiteApp();
                WebSiteEntity webSite    = webSiteApp.GetFormNoDel(wenSiteIds);
                if (webSite != null)
                {
                    LUCENCEINDEXPATH = string.Format(LUCENCEINDEXPATH, webSite.ShortName);
                    INDEX_DIR        = new DirectoryInfo(LUCENCEINDEXPATH);
                    BooleanQuery bQuery = new BooleanQuery();

                    using (IndexSearcher searcher = new IndexSearcher(FSDirectory.Open(INDEX_DIR), true))
                    {
                        QueryParser parseFullName = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "FullName", analyzer);
                        Query       query         = parseFullName.Parse(keyword);
                        bQuery.Add(query, Occur.SHOULD);

                        QueryParser parseAuthor = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "Author", analyzer);
                        Query       queryAuthor = parseAuthor.Parse(keyword);
                        bQuery.Add(queryAuthor, Occur.SHOULD);

                        QueryParser parseContent = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "Content", analyzer);
                        Query       queryContent = parseContent.Parse(keyword);
                        bQuery.Add(queryContent, Occur.SHOULD);

                        TopDocs       tds = searcher.Search(bQuery, Int32.MaxValue);
                        List <string> ids = new List <string>();
                        foreach (ScoreDoc sd in tds.ScoreDocs)
                        {
                            Document doc = searcher.Doc(sd.Doc);
                            ids.Add(doc.Get("Id"));
                        }
                        if (ids != null && ids.Count > 0)
                        {
                            ContentApp contentApp = new ContentApp();
                            mdoels = contentApp.GetLists(m => ids.Contains(m.Id) && m.DeleteMark != true && m.EnabledMark == true);
                        }
                        searcher.Dispose();
                    }
                }
            }
            return(mdoels);
        }