public override string ResultGet() { JXSearchEntityResult result = new JXSearchEntityResult() { resultCode = "Fail" }; try { string szJson = HttpContext.Current.Request["para"]; if (string.IsNullOrEmpty(szJson)) { result.resultMsg = "未指定查询参数"; return(ResultResponse(result, "listKeywords")); } // 构造查询FORM RequestForm form = JsonHelper.ParseFromJson <RequestForm>(szJson); if (form == null || form.pageForm == null || form.queryForm == null) { result.resultMsg = "解析查询参数出错"; return(ResultResponse(result, "listKeywords")); } // 搜索数据 int recCount = 0; JXSearchProvider provider = JXSearchProviderCreator.CreateProvider(JXSearchType.Keywords); result = provider.Search(form.queryForm.keyword, form.pageForm.size, form.pageForm.page, out recCount); if (result == null) { result = new JXSearchEntityResult() { resultCode = "Fail", resultMsg = "无结果" } } ; else { result.total = recCount; } } catch (Exception ex) { result.resultMsg = ex.Message; } return(ResultResponse(result, "listKeywords")); }
/// <summary> /// 关键词索引 /// </summary> private static void CreateKeywordIndex() { try { Console.WriteLine("Keyword === 开始生成索引"); // 查询关键词 int recordCount = 0; string strWhere = " AND k.Status=0"; DataTable dTable = JXAPI.Component.BLL.SqlBLL.Instance.Keyword_GetListForSearch(strWhere); if (dTable == null || dTable.Rows.Count <= 0) { Console.WriteLine("Keyword === 未找到需要索引数据"); return; } Console.WriteLine(string.Format("Keyword === 准备索引:{0}条数据", dTable.Rows.Count)); // 设置索引 string IndexName = currDate.ToString("yyyyMMdd"); JXSearchProvider provider = JXSearchProviderCreator.CreateProvider(JXSearchType.Keywords); provider.CreateIndex(GetIndexPath(provider.INDEX_DIR, out IndexName)); provider.MaxMergeFactor = 301; provider.MinMergeDocs = 301; // 定义商品搜索 int recCount = 0; JXSearchProductResult result = null; JXSearchProvider providerProduct = JXSearchProviderCreator.CreateProvider(JXSearchType.Product); // 遍历关键词 int i = 0; recordCount = dTable.Rows.Count; foreach (DataRow row in dTable.Rows) { i++; #region 关键词下商品数 try { result = providerProduct.Search(new RequestForm() { pageForm = new PageForm() { page = 1, size = 1 }, queryForm = new QueryForm() { keyword = row["ChineseName"].ToString() } }, out recCount); row["ProductCount"] = recCount; } catch { } #endregion // 写入索引 provider.IndexString(row, JXSearchType.Keywords); if (i % 300 == 0) { provider.CloseWithoutOptimize(); provider.CreateIndex(string.Format("{0}\\{1}", provider.INDEX_DIR, IndexName)); provider.MaxMergeFactor = 301; provider.MinMergeDocs = 301; } } // 关闭流 provider.Close(); Console.WriteLine(string.Format("Keyword === 插入{0}行关键字索引数据", recordCount)); } catch (Exception ex) { Console.WriteLine(string.Format("Keyword === 生成索引失败:{0}", ex.Message.ToString())); } // 缓存索引文件名 try { SearchDictDirCacheProvider.Remove("KeywordEngine"); SearchDictDirCacheProvider.Set("KeywordEngine"); } catch (Exception ex) { Console.WriteLine(string.Format("Keyword === 缓存索引文件名失败:{0}", ex.Message.ToString())); } finally { Console.WriteLine("Keyword === 索引生成完毕\r\n\r\n"); } }
/// <summary> /// 创建商品索引 /// </summary> private static void CreateProductIndex() { try { Console.WriteLine("Product === 开始生成索引"); // 查询商品 string strWhere = " AND p.[Status] = 0 AND p.Selling=1"; DataTable dTable = JXAPI.Component.BLL.ProductBLL.Instance.Product_ListForSearch(strWhere); if (dTable == null || dTable.Rows.Count <= 0) { Console.WriteLine("Product === 未找到需要索引数据"); return; } Console.WriteLine(string.Format("Product === 准备索引:{0}条数据", dTable.Rows.Count)); // 设置索引 string IndexName = currDate.ToString("yyyyMMdd"); JXSearchProvider provider = JXSearchProviderCreator.CreateProvider(JXSearchType.Product); provider.CreateIndex(GetIndexPath(provider.INDEX_DIR, out IndexName)); provider.MaxMergeFactor = 301; provider.MinMergeDocs = 301; // 遍历商品 int recordCount = dTable.Rows.Count; for (int i = 0; i <= dTable.Rows.Count - 1; i++) { // 写入索引 provider.IndexString(dTable.Rows[i], JXSearchType.Product); if (i % 300 == 0) { provider.CloseWithoutOptimize(); provider.CreateIndex(string.Format("{0}\\{1}", provider.INDEX_DIR, IndexName)); provider.MaxMergeFactor = 301; provider.MinMergeDocs = 301; } } // 关闭流 provider.Close(); Console.WriteLine(string.Format("Product === 插入{0}行商品索引数据", recordCount)); } catch (Exception ex) { Console.WriteLine(string.Format("Product === 生成索引失败:{0}", ex.Message.ToString())); } // 缓存索引文件名 try { SearchDictDirCacheProvider.Remove("ProductEngine"); SearchDictDirCacheProvider.Set("ProductEngine"); } catch (Exception ex) { Console.WriteLine(string.Format("Product === 缓存索引文件名失败:{0}", ex.Message.ToString())); } finally { Console.WriteLine("Product === 索引生成完毕\r\n\r\n"); } }
/// <summary> /// 更新搜索关键词 /// </summary> private static void UpdateSearchKeyword() { try { Console.WriteLine("SearchKeyword === 开始执行搜索关键词推荐商品"); // 查询关键词 int recordCount = 0; DataTable dTable = JXAPI.Component.BLL.SqlBLL.Instance.SearchKeyword_GetListForSearch(); if (dTable == null || dTable.Rows.Count <= 0) { Console.WriteLine("SearchKeyword === 未找到搜索关键词"); return; } Console.WriteLine(string.Format("SearchKeyword === 准备:{0}条数据", dTable.Rows.Count)); // 定义商品搜索 int recCount = 0; JXSearchProductResult result = null; JXSearchProvider providerProduct = JXSearchProviderCreator.CreateProvider(JXSearchType.Product); // 定义关键词搜索 JXSearchEntityResult entityResult = null; JXSearchProvider providerKeyword = JXSearchProviderCreator.CreateProvider(JXSearchType.Keywords); // string related = string.Empty, salled = string.Empty, recommend = string.Empty, preferential = string.Empty, hotSalled = string.Empty; // 遍历关键词 foreach (DataRow row in dTable.Rows) { #region 关键词相关 try { entityResult = providerKeyword.Search(row["ChineseName"].ToString(), 10, 1, out recCount); if (entityResult == null || entityResult.listKeyword == null || entityResult.listKeyword.Count <= 0) { related = string.Empty; } else { related = string.Join(",", entityResult.listKeyword.Select(i => i.chineseName).ToArray()); } } catch (Exception ex) { Console.WriteLine(string.Format("SearchKeyword === 推荐(0):{0}", ex.Message)); } #endregion #region 关键词下商品 try { result = providerProduct.Search(new RequestForm() { pageForm = new PageForm() { page = 1, size = 100 }, queryForm = new QueryForm() { keyword = row["ChineseName"].ToString() } }, out recCount); if (result == null || result.listProductList == null || result.listProductList.Count <= 0) { salled = string.Empty; recommend = string.Empty; preferential = string.Empty; hotSalled = string.Empty; } else { salled = string.Join(",", result.listProductList.OrderByDescending(i => i.sellCount).Take(10).Select(i => i.productID).ToArray()); recommend = string.Join(",", result.listProductList.OrderByDescending(i => i.recommend).Take(10).Select(i => i.productID).ToArray()); preferential = string.Join(",", result.listProductList.OrderByDescending(i => i.preferential).Take(10).Select(i => i.productID).ToArray()); hotSalled = string.Join(",", result.listProductList.OrderByDescending(i => i.preferential).ThenBy(i => i.sellCount).Take(10).Select(i => i.productID).ToArray()); } } catch (Exception ex) { Console.WriteLine(string.Format("SearchKeyword === 推荐(1):{0}", ex.Message)); } #endregion #region 保存 try { JXAPI.Component.BLL.SqlBLL.Instance.Update_SearchKeyword(related, salled, recommend, preferential, hotSalled, recCount, int.Parse(row["keywordID"].ToString())); } catch (Exception ex) { Console.WriteLine(string.Format("SearchKeyword === 推荐(2):{0}", ex.Message)); } #endregion } Console.WriteLine(string.Format("SearchKeyword === {0}条搜索关键词,推荐完成", recordCount)); } catch (Exception ex) { Console.WriteLine(string.Format("SearchKeyword === 搜索关键词推荐商品失败:{0}", ex.Message.ToString())); } }
/// <summary> /// 搜索关键词索引 /// </summary> private static void CreateSearchKeywordIndex() { try { Console.WriteLine("SearchKeyword === 开始生成索引"); // 查询关键词 int recordCount = 0; DataTable dTable = JXAPI.Component.BLL.SqlBLL.Instance.SearchKeyword_GetListForSearch(); if (dTable == null || dTable.Rows.Count <= 0) { Console.WriteLine("SearchKeyword === 未找到需要索引数据"); return; } Console.WriteLine(string.Format("SearchKeyword === 准备索引:{0}条数据", dTable.Rows.Count)); // 设置索引 string IndexName = currDate.ToString("yyyyMMdd"); JXSearchProvider provider = JXSearchProviderCreator.CreateProvider(JXSearchType.Keywords); provider.CreateIndex(GetIndexPath(provider.INDEX_DIR, out IndexName)); provider.MaxMergeFactor = 301; provider.MinMergeDocs = 301; // 遍历关键词 int i = 0; recordCount = dTable.Rows.Count; foreach (DataRow row in dTable.Rows) { i++; // 写入索引 provider.IndexString(row, JXSearchType.Keywords); if (i % 300 == 0) { provider.CloseWithoutOptimize(); provider.CreateIndex(string.Format("{0}\\{1}", provider.INDEX_DIR, IndexName)); provider.MaxMergeFactor = 301; provider.MinMergeDocs = 301; } } // 关闭流 provider.Close(); Console.WriteLine(string.Format("SearchKeyword === 插入{0}行关键字索引数据", recordCount)); } catch (Exception ex) { Console.WriteLine(string.Format("SearchKeyword === 生成索引失败:{0}", ex.Message.ToString())); } // 缓存索引文件名 try { SearchDictDirCacheProvider.Remove("KeywordEngine"); SearchDictDirCacheProvider.Set("KeywordEngine"); } catch (Exception ex) { Console.WriteLine(string.Format("SearchKeyword === 缓存索引文件名失败:{0}", ex.Message.ToString())); } finally { Console.WriteLine("SearchKeyword === 索引生成完毕\r\n\r\n"); } }