Beispiel #1
0
        public static string Build(List <string> cultures)
        {
            string message = null;

            if (!IsBuilding)
            {
                IsBuilding = true;
                string luceneDirectory = GetLuceneDirectory();
                LuceneNetConfig.LuceneDirectory     = luceneDirectory;
                LuceneNetConfig.LuceneDictDirectory = LuceneDictDirectory;
                string        luceneBuildDir = null;
                Directory     directory      = null;
                IndexWriter   indexWriter    = null;
                ColumnField[] columnFields   = GetColumnFields();
                List <View_seo_keys_culture> viewSEOKeysCultureList = SelectAllViewSEOKeysCulture();
                foreach (string culture in cultures)
                {
                    luceneBuildDir = IOHelper.CombinePath(luceneDirectory, culture.ToUpper());
                    IOHelper.ClearDirectory(luceneBuildDir);
                    directory = LuceneBus.GetDirectory(luceneBuildDir);
                    if (LuceneBus.IsLocked(directory))
                    {
                        LuceneBus.UnLock(directory);
                    }
                    try
                    {
                        indexWriter = LuceneBus.GetWriter(culture, directory);
                        //http://blog.jobbole.com/80464/
                        int pageCount = Select(culture);
                        if (pageCount > 0)
                        {
                            int spuID = 0;
                            List <LuceneSpuModel> luceneSpuModelList = null;
                            for (int pageIndex = 1; pageIndex <= pageCount; pageIndex++)
                            {
                                luceneSpuModelList = Select(viewSEOKeysCultureList, culture, ref spuID);
                                if (luceneSpuModelList != null && luceneSpuModelList.Count > 0)
                                {
                                    LuceneBus.Insert <LuceneSpuModel>(indexWriter, luceneSpuModelList, columnFields);
                                    luceneSpuModelList.Clear();
                                    luceneSpuModelList = null;
                                }
                                else
                                {
                                    break;
                                }
                            }
                            if (luceneSpuModelList != null)
                            {
                                if (luceneSpuModelList.Count > 0)
                                {
                                    luceneSpuModelList.Clear();
                                }
                                luceneSpuModelList = null;
                            }
                            LuceneBus.MaybeMerge(indexWriter);
                            LuceneBus.Commit(indexWriter);
                        }
                    }
                    catch (Exception ex)
                    {
                        message = "Build(Language[] cultures)出现错误,错误信息:" + ex.Message;
                    }
                    finally
                    {
                        if (indexWriter != null)
                        {
                            LuceneBus.Close(indexWriter);
                            indexWriter = null;
                        }
                    }
                    if (!string.IsNullOrWhiteSpace(message))
                    {
                        break;
                    }
                }
                Change();//索引生成完切换前台搜索目录
                if (columnFields != null)
                {
                    Array.Clear(columnFields, 0, columnFields.Length);
                    columnFields = null;
                }
                IsBuilding = false;
            }
            return(message);
        }