Example #1
0
        /// <summary>
        /// 生成所有索引
        /// </summary>
        /// <param name="itemCallback"></param>
        public void MakeLuceneIndex(Action <Document> callback)
        {
            #region 大学专业

            SpecialtyDao     dao  = new SpecialtyDao();
            List <Specialty> list = dao.GetSpecialtyList();

            PanGuAnalyzer analyzer = new PanGuAnalyzer(true);

            string textIndexDir = LuceneManager.GetLuceneTextIndexDirectoryPath(LuceneTextIndexType.Specialty, null);

            List <Document> documentList = new List <Document>();
            foreach (Specialty item in list)
            {
                Document indexDoc = new Document();

                #region 根据需要添加要被索引的数据列

                indexDoc.Add(new NumericField("SpecialtyId", Field.Store.YES, true).SetIntValue(item.SpecialtyId));
                indexDoc.Add(new Field("Name", item.Name.ToLower(), Field.Store.YES, Field.Index.ANALYZED));
                #endregion 根据需要添加要被索引的数据列

                documentList.Add(indexDoc);
            }
            LuceneManager.MakeIndex(documentList, textIndexDir, callback);

            #endregion
        }
        /// <summary>
        /// 生成所有索引
        /// </summary>
        /// <param name="itemCallback"></param>
        public void MakeLuceneIndex(Action <Document> callback)
        {
            #region 中国大学

            ChinaUniversityDao     chinaUniverisityDao = new ChinaUniversityDao();
            List <ChinaUniversity> universityList      = chinaUniverisityDao.GetChinaUniversityList();

            PanGuAnalyzer analyzer = new PanGuAnalyzer(true);

            string textIndexDir = LuceneManager.GetLuceneTextIndexDirectoryPath(LuceneTextIndexType.ChinaUniversity, null);

            List <Document> documentList = new List <Document>();
            foreach (ChinaUniversity university in universityList)
            {
                Document indexDoc = new Document();

                #region 根据需要添加要被索引的数据列

                indexDoc.Add(new NumericField("UniversityId", Field.Store.YES, true).SetIntValue(university.UniversityId));
                indexDoc.Add(new Field("CnName", university.Name.ToLower(), Field.Store.YES, Field.Index.ANALYZED));
                indexDoc.Add(new Field("Pinyin", university.Pinyin.ToLower(), Field.Store.YES, Field.Index.ANALYZED));
                #endregion 根据需要添加要被索引的数据列

                documentList.Add(indexDoc);
            }
            LuceneManager.MakeIndex(documentList, textIndexDir, callback);

            #endregion 中国大学
        }
        public List <SchoolSample> SearchList(string keywords, PagerInfo pagerInfo)
        {
            List <SchoolSample> list = new List <SchoolSample>();

            try
            {
                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("CnName", "*" + lowerCaseKeywordsPart + "*")), BooleanClause.Occur.SHOULD);
                        groupQuery.Add(new WildcardQuery(new Term("Pinyin", "*" + lowerCaseKeywordsPart + "*")), BooleanClause.Occur.SHOULD);

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



                Sort   sort         = null;
                string textIndexDir = LuceneManager.GetLuceneTextIndexDirectoryPath(LuceneTextIndexType.ChinaUniversity, null);

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

                    list.Add(school);
                });
            }
            catch (Exception ex)
            {
                pagerInfo.RecordCount = 0;
            }

            return(list);
        }