Beispiel #1
0
        private static void Main(string[] args)
        {
            try
            {
               
                Console.WriteLine("请选择要进行的操作");
                Console.WriteLine("输入Y,生成所有文章索引");
                Console.WriteLine("输入F,生成所有文章索引");
                Console.WriteLine("> X:关闭程序");
                string input = Console.ReadLine();
                ArticleLuceneBo luceneBo = new ArticleLuceneBo();
                if (string.Compare(input, "y", true) == 0)
                {
                    Console.WriteLine("开始执行:生成所有文章索引......");

                    string panguConfigPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "PanGu\\PanGu.xml");
                    PanGu.Segment.Init(panguConfigPath);

                    luceneBo.MakeArticleLuceneIndex(delegate(Document indexDoc)
                    {
                        Console.WriteLine("生成了文章《{0}》的索引", indexDoc.Get("Title"));
                    });
                    Console.WriteLine("索引全部生成");
                }
                else if (string.Compare(input, "F", true) == 0)
                {
                    Console.WriteLine("输入要查找的关键字,关键字用逗号分开");
                    string keyword = Console.ReadLine();

                    PagerInfo pagerInfo = new PagerInfo() { PageIndex = 1, PageSize = 10 };
                    List<LuceneArticle> list = luceneBo.SearchArticleList(keyword, pagerInfo);
                    Console.WriteLine("索引结果如下:");
                    if (list != null)
                    {
                        list.ForEach(item =>
                        {

                            Console.WriteLine(item.Content);
                        });
                    }
                }
                if (string.Compare(input, "X", true) == 0)
                {
                    Environment.Exit(0);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("程序出错:{0}", ex.Message);
            }
            Console.ReadLine();

        }
        /// <summary>
        /// 按关键词搜索学校
        /// </summary>
        /// <param name="keywords"></param>
        /// <param name="pagerInfo"></param>
        /// <returns></returns>
        public List<SchoolSample> SearchSchoolList(string keywords, PagerInfo pagerInfo)
        {
            List<SchoolSample> list = new List<SchoolSample>();

            try
            {
                BooleanQuery query = new BooleanQuery();
                if (!string.IsNullOrEmpty(keywords))
                {
                    string[] keywordsParts = StringHelper.Split<string>(keywords, StringSplitOptions.RemoveEmptyEntries, " ", ",");

                    foreach (string keywordsPart in keywordsParts)
                    {
                        string lowerCaseKeywordsPart = keywordsPart.ToLower();

                        BooleanQuery groupQuery = new BooleanQuery();
                        groupQuery.Add(new WildcardQuery(new Term("Name", "*" + keywordsPart + "*")), Lucene.Net.Search.BooleanClause.Occur.SHOULD);
                        groupQuery.Add(new WildcardQuery(new Term("CnName", "*" + keywordsPart + "*")), Lucene.Net.Search.BooleanClause.Occur.SHOULD);
                        groupQuery.Add(new WildcardQuery(new Term("Pinyin", "*" + keywordsPart + "*")), Lucene.Net.Search.BooleanClause.Occur.SHOULD);

                        groupQuery.Add(new WildcardQuery(new Term("LC_Name", "*" + lowerCaseKeywordsPart + "*")), Lucene.Net.Search.BooleanClause.Occur.SHOULD);
                        groupQuery.Add(new WildcardQuery(new Term("LC_CnName", "*" + lowerCaseKeywordsPart + "*")), Lucene.Net.Search.BooleanClause.Occur.SHOULD);
                        groupQuery.Add(new WildcardQuery(new Term("LC_Pinyin", "*" + lowerCaseKeywordsPart + "*")), Lucene.Net.Search.BooleanClause.Occur.SHOULD);

                        query.Add(groupQuery, BooleanClause.Occur.MUST);
                    }

                    Sort sort = null;

                    LuceneManager.SearchLuceneData(LuceneTextIndexType.AllSchool, query, sort, pagerInfo, delegate(Document doc)
                    {
                        SchoolSample school = new SchoolSample(doc);

                        list.Add(school);
                    });

                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(ex);
                pagerInfo.RecordCount = 0;
            }

            return list;
        }
        /// <summary>
        /// 按关键词搜索文章
        /// </summary>
        /// <param name="keywords"></param>
        /// <param name="pagerInfo"></param>
        /// <returns></returns>
        public List<LuceneArticle> SearchArticleList(string keywords, PagerInfo pagerInfo)
        {

            List<LuceneArticle> list = new List<LuceneArticle>();
            BooleanQuery query = new BooleanQuery();
            if (!string.IsNullOrEmpty(keywords))
            {
                string[] keywordsParts = keywords.Split(',');//逗号分隔

                foreach (string keywordsPart in keywordsParts)
                {
                    string lowerCaseKeywordsPart = keywordsPart.ToLower();

                    BooleanQuery groupQuery = new BooleanQuery();
                    groupQuery.Add(new WildcardQuery(new Term("Id", "*" + keywordsPart + "*")), Lucene.Net.Search.BooleanClause.Occur.SHOULD);
                    groupQuery.Add(new WildcardQuery(new Term("Title", "*" + keywordsPart + "*")), Lucene.Net.Search.BooleanClause.Occur.SHOULD);
                    groupQuery.Add(new WildcardQuery(new Term("Content", "*" + keywordsPart + "*")), Lucene.Net.Search.BooleanClause.Occur.SHOULD);
                    query.Add(groupQuery, BooleanClause.Occur.MUST);
                }
                Sort sort = null;
                try
                {

                    List<Document> documentList = LuceneManager.SearchLuceneData(directoryPath, query, sort, pagerInfo);
                    if (documentList != null && documentList.Count > 0)
                    {

                        documentList.ForEach(item => { list.Add(new LuceneArticle(item)); });
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Info(this, ex.ToString());

                    pagerInfo.RecordCount = 0;
                }
            }


            return list;
        }
        /// <summary>
        /// 搜索LUCENE数据
        /// </summary>
        /// <param name="indexType"></param>
        /// <param name="query"></param>
        /// <param name="sort"></param>
        /// <param name="pagerInfo"></param>
        /// <param name="callback"></param>
        /// <returns></returns>
        public static List<Document> SearchLuceneData(string directoryPath, Query query, Sort sort, PagerInfo pagerInfo)
        {
            List<Document> list = new List<Document>();

            FSDirectory directory = FSDirectory.Open(new System.IO.DirectoryInfo(directoryPath), new NoLockFactory());
            IndexReader indexReader = IndexReader.Open(directory, true);
            IndexSearcher indexSearcher = new IndexSearcher(indexReader);
            ScoreDoc[] docs;
            int totalCount;
            int startOffset;
            int endOffset;
            if (sort != null)
            {
                TopFieldDocs resultFieldDocs = indexSearcher.Search(query, null, indexSearcher.MaxDoc(), sort);
                totalCount = resultFieldDocs.totalHits;
                pagerInfo.RecordCount = totalCount;
                startOffset = (pagerInfo.PageIndex - 1) * pagerInfo.PageSize;
                endOffset = pagerInfo.PageIndex * pagerInfo.PageSize;
                if (endOffset >= totalCount)
                {
                    endOffset = totalCount;
                }
                docs = resultFieldDocs.scoreDocs;
            }
            else
            {
                TopDocs resultFieldDocs = indexSearcher.Search(query, null, indexSearcher.MaxDoc());
                totalCount = resultFieldDocs.totalHits;
                pagerInfo.RecordCount = totalCount;
                startOffset = (pagerInfo.PageIndex - 1) * pagerInfo.PageSize;
                endOffset = pagerInfo.PageIndex * pagerInfo.PageSize;
                if (endOffset >= totalCount)
                {
                    endOffset = totalCount;
                }
                docs = resultFieldDocs.scoreDocs;
            }

            if (totalCount > 0)
            {
                for (int i = startOffset; i < endOffset; i++)
                {
                    ScoreDoc hit = docs[i];
                    Document doc = indexSearcher.Doc(hit.doc);

                    list.Add(doc);
                }
            }

            indexSearcher.Close();
            directory.Close();

            return list;
        }