/// <summary> /// 生成所有院校索引 /// </summary> /// <param name="itemCallback"></param> public void MakeAllSchoolIndex(Action <Document> callback) { #region 所有海外大学 string directoryPath = ConfigurationManager.AppSettings["UNIVERTITY_INDEX_FILE_DIRECTORY"].ToString(); OverseaUniversityDao overseaUniversityDao = new OverseaUniversityDao(); List <OverseaUniversity> universityList = overseaUniversityDao.Backstage_GetAllUniversityList(); List <Document> documentList = new List <Document>(); foreach (OverseaUniversity university in universityList) { Document indexDoc = new Document(); #region 根据需要添加要被索引的数据列 indexDoc.Add(new Field("Key", string.Concat("OverseaUniversity_", university.UniversityId), Field.Store.YES, Field.Index.ANALYZED)); indexDoc.Add(new NumericField("SchoolId", Field.Store.YES, true).SetIntValue(university.UniversityId)); indexDoc.Add(new Field("Name", university.Name, Field.Store.YES, Field.Index.ANALYZED)); indexDoc.Add(new Field("LC_Name", university.Name.ToLower(), Field.Store.NO, Field.Index.ANALYZED)); indexDoc.Add(new Field("CnName", university.CnName, Field.Store.YES, Field.Index.ANALYZED)); indexDoc.Add(new Field("LC_CnName", university.CnName.ToLower(), Field.Store.NO, Field.Index.ANALYZED)); indexDoc.Add(new Field("Pinyin", university.Pinyin, Field.Store.YES, Field.Index.ANALYZED)); indexDoc.Add(new Field("LC_Pinyin", university.Pinyin.ToLower(), Field.Store.NO, Field.Index.ANALYZED)); #endregion documentList.Add(indexDoc); LuceneManager.MakeIndex(documentList, directoryPath, callback); #endregion } }
/// <summary> /// 生成所有院校索引 /// </summary> /// <param name="itemCallback"></param> public void MakeAllSchoolIndex(Action<Document> callback) { #region 所有海外大学 string directoryPath = ConfigurationManager.AppSettings["UNIVERTITY_INDEX_FILE_DIRECTORY"].ToString(); OverseaUniversityDao overseaUniversityDao = new OverseaUniversityDao(); List<OverseaUniversity> universityList = overseaUniversityDao.Backstage_GetAllUniversityList(); List<Document> documentList = new List<Document>(); foreach (OverseaUniversity university in universityList) { Document indexDoc = new Document(); #region 根据需要添加要被索引的数据列 indexDoc.Add(new Field("Key", string.Concat("OverseaUniversity_", university.UniversityId), Field.Store.YES, Field.Index.ANALYZED)); indexDoc.Add(new NumericField("SchoolId", Field.Store.YES, true).SetIntValue(university.UniversityId)); indexDoc.Add(new Field("Name", university.Name, Field.Store.YES, Field.Index.ANALYZED)); indexDoc.Add(new Field("LC_Name", university.Name.ToLower(), Field.Store.NO, Field.Index.ANALYZED)); indexDoc.Add(new Field("CnName", university.CnName, Field.Store.YES, Field.Index.ANALYZED)); indexDoc.Add(new Field("LC_CnName", university.CnName.ToLower(), Field.Store.NO, Field.Index.ANALYZED)); indexDoc.Add(new Field("Pinyin", university.Pinyin, Field.Store.YES, Field.Index.ANALYZED)); indexDoc.Add(new Field("LC_Pinyin", university.Pinyin.ToLower(), Field.Store.NO, Field.Index.ANALYZED)); #endregion documentList.Add(indexDoc); LuceneManager.MakeIndex(documentList, directoryPath, callback); #endregion } }
/// <summary> /// 重新生成所有院校索引 /// </summary> /// <param name="itemCallback"></param> public void MakeAllSchoolIndex(MakeSchoolItemIndexCallback itemCallback) { #region 海外大学 try { OverseaUniversityDao overseaUniversityDao = new OverseaUniversityDao(); List <OverseaUniversity> universityList = overseaUniversityDao.Backstage_GetAllUniversityList(); PanGuAnalyzer analyzer = new PanGuAnalyzer(true); string textIndexDir = Utilities.GetLuceneTextIndexDirectoryPath(LuceneTextIndexType.AllSchool, null); if (!System.IO.Directory.Exists(textIndexDir)) { System.IO.Directory.CreateDirectory(textIndexDir); } Lucene.Net.Store.Directory indexDirectory = Lucene.Net.Store.FSDirectory.Open(new System.IO.DirectoryInfo(textIndexDir), new NativeFSLockFactory()); if (IndexReader.IndexExists(indexDirectory)) { if (IndexWriter.IsLocked(indexDirectory)) { IndexWriter.Unlock(indexDirectory); } } IndexWriter indexWriter = new IndexWriter(indexDirectory, analyzer, false, Lucene.Net.Index.IndexWriter.MaxFieldLength.LIMITED); foreach (OverseaUniversity university in universityList) { try { Document indexDoc = new Document(); #region 数据列 indexDoc.Add(new Field("Key", string.Concat("OverseaUniversity_", university.UniversityId), Field.Store.YES, Field.Index.ANALYZED)); indexDoc.Add(new NumericField("SchoolId", Field.Store.YES, true).SetIntValue(university.UniversityId)); indexDoc.Add(new NumericField("Type", Field.Store.YES, true).SetIntValue((int)SchoolType.OverseaUniversity)); indexDoc.Add(new Field("Name", university.Name, Field.Store.YES, Field.Index.ANALYZED)); indexDoc.Add(new Field("LC_Name", university.Name.ToLower(), Field.Store.NO, Field.Index.ANALYZED)); indexDoc.Add(new Field("CnName", university.CnName, Field.Store.YES, Field.Index.ANALYZED)); indexDoc.Add(new Field("LC_CnName", university.CnName.ToLower(), Field.Store.NO, Field.Index.ANALYZED)); indexDoc.Add(new Field("Pinyin", university.Pinyin, Field.Store.YES, Field.Index.ANALYZED)); indexDoc.Add(new Field("LC_Pinyin", university.Pinyin.ToLower(), Field.Store.NO, Field.Index.ANALYZED)); indexDoc.Add(new NumericField("RegionId", Field.Store.YES, true).SetIntValue(university.CountryId)); indexDoc.Add(new NumericField("StateId", Field.Store.YES, true).SetIntValue(university.StateId)); indexDoc.Add(new NumericField("CityId", Field.Store.YES, true).SetIntValue(university.CityId)); #endregion indexWriter.AddDocument(indexDoc, analyzer); if (itemCallback != null) { itemCallback(indexDoc); } } catch (Exception ex) { LogHelper.WriteLog(ex); } } indexWriter.Optimize(); indexWriter.Close(); analyzer.Close(); } catch (Exception ex) { LogHelper.WriteLog(ex); } #endregion }