Beispiel #1
0
        public JsonResult AddKeywords(string keywords)
        {
            if (string.IsNullOrEmpty(keywords))
            {
                return(Json(ErrorModel.InputError));
            }

            var model = new TraficKeywords
            {
                Keywords = keywords
            };

            var keywordsBll = new TraficKeywordsBll();

            keywordsBll.Insert(model);

            if (model.Id > 0)
            {
                DataUpdateLog.SingleUpdate(typeof(TraficKeywords).Name, model.Id, DataUpdateType.Insert);

                SearchHelper.AddSearchTask(2, model.Id);

                return(Json(ErrorModel.OperateSuccess));
            }

            return(Json(ErrorModel.OperateFailed));
        }
Beispiel #2
0
        /// <summary>
        /// 当新增搜索结果时,将其写入对应关键字的搜索结果文件中(每个关键字对应一个搜索结果文件)
        /// </summary>
        /// <param name="keywords"></param>
        /// <param name="results"></param>
        private static void UpdateTraficKeywordsSearchResult(TraficKeywords keywords, IEnumerable <TraficSearchResult> results)
        {
            EnsureSearchResultFileExists(keywords);

            var filename  = PathExtension.MapPath(keywords.ResultPath);
            var seperator = "Guoli.Admin.SearchHelper.Seperator";

            using (var fs = new FileStream(filename, FileMode.Append, FileAccess.Write))
            {
                using (var writer = new StreamWriter(fs, Encoding.UTF8))
                {
                    foreach (var r in results)
                    {
                        var json = JsonHelper.Serialize(r);
                        writer.Write(json);
                        writer.Write(seperator);
                    }
                }
            }

            DataUpdateLog.SingleUpdate(nameof(TraficKeywords), keywords.Id, DataUpdateType.Update);
        }
Beispiel #3
0
        private static void EnsureSearchResultFileExists(TraficKeywords keywords)
        {
            if (keywords.ResultPath.IsNullOrEmpty())
            {
                var d            = DateTime.Now;
                var relativePath = $"{AppSettings.SearchResults}/{d.ToString("yyyy-MM-dd")}";
                var absolutePath = PathExtension.MapPath(relativePath);
                if (!Directory.Exists(absolutePath))
                {
                    Directory.CreateDirectory(absolutePath);
                }

                keywords.ResultPath = $"{relativePath}/{Guid.NewGuid()}";
            }

            var filename = PathExtension.MapPath(keywords.ResultPath);

            if (!File.Exists(filename))
            {
                File.Create(filename);
            }
        }
Beispiel #4
0
 /// <summary>
 /// 将搜索结果插入数据库
 /// </summary>
 private static void SearchResultEnqueue(Dictionary <string, string> searchResult, int fileId, TraficKeywords keywords)
 {
     lock (_lockObj)
     {
         var results = new List <TraficSearchResult>();
         foreach (var pair in searchResult)
         {
             var res = new TraficSearchResult
             {
                 KeywordsId   = keywords.Id,
                 TraficFileId = fileId,
                 SearchResult = pair.Value,
                 Position     = pair.Key
             };
             results.Add(res);
             searchResultQueue.Enqueue(res);
         }
         UpdateTraficKeywordsSearchResult(keywords, results);
     }
 }