/// <summary>
        /// 创建索引
        /// </summary>
        /// <param name="info">索引信息</param>
        /// <returns></returns>
        private Document CreateKeywordDocument(DataRow info)
        {
            Document doc = new Document();
            //  名称
            Field field = new Field("ChineseName", info["ChineseName"].ToString(), Field.Store.YES, Field.Index.ANALYZED);

            field.SetBoost(30f);
            doc.Add(field);

            //  拼音
            field = new Field("PinyinName", PinyinUtil.ConvertToPinyin(info["ChineseName"].ToString()), Field.Store.NO, Field.Index.ANALYZED);
            field.SetBoost(10f);
            doc.Add(field);

            //  其他索引信息
            doc.Add(new Field("ProductCount", info["ProductCount"].ToString(), Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field("TypeID", info["TypeID"].ToString(), Field.Store.YES, Field.Index.ANALYZED));

            //  商品基本信息
            doc.Add(new Field("KeywordID", info["KeywordID"].ToString(), Field.Store.YES, Field.Index.ANALYZED));
            return(doc);
        }
        /// <summary>
        /// 创建索引文档
        /// </summary>
        /// <param name="info">商品信息</param>
        /// <returns></returns>
        private Document CreateDocument(DataRow info)
        {
            Document doc = new Document();

            //  商品名称
            Field field = new Field("ChineseName", info["ChineseName"].ToString(), Field.Store.YES, Field.Index.ANALYZED);
            float boost = 30f;

            if (info["ProductType"].ToString() == "0" || info["ProductType"].ToString() == "3")
            {
                boost -= 20f;
            }
            field.SetBoost(boost);
            doc.Add(field);

            //  商品标签
            field = new Field("ProductTag", info["ProductTag"].ToString(), Field.Store.NO, Field.Index.ANALYZED);
            field.SetBoost(10f);
            doc.Add(field);

            //  功能主治
            doc.Add(new Field("Efficacy", info["Efficacy"].ToString(), Field.Store.NO, Field.Index.ANALYZED));

            //  关键字
            string pattern  = "[“”;。、,. %[ ]()()Ⅱ,*:X+-]";
            string keywords = Regex.Replace(info["Keywords"].ToString(), pattern, " ");

            field = new Field("Keywords", keywords, Field.Store.NO, Field.Index.ANALYZED);
            doc.Add(field);

            //  通用名
            string cadn = Regex.Replace(info["CADN"].ToString(), pattern, " ");

            field = new Field("CADN", cadn, Field.Store.YES, Field.Index.ANALYZED);
            doc.Add(field);

            //  拼音  【商品名称、通用名】
            string chineseName = Regex.Replace(info["ChineseName"].ToString(), pattern, " ");

            pattern = "[a-zA-Z0-9袋盒片剂粒]";
            string combinName = Regex.Replace(string.Format("{0} {1}", chineseName, cadn), pattern, " ");

            field = new Field("PinyinName", PinyinUtil.ConvertToPinyin(GetKeyWordsSplitFilter(combinName, new PanGuTokenizer())), Field.Store.YES, Field.Index.ANALYZED);
            doc.Add(field);

            //  名称截取
            field = new Field("ChineseNameWord", GetWordByChineseName(chineseName), Field.Store.YES, Field.Index.ANALYZED);
            doc.Add(field);

            //  长名称、厂商
            doc.Add(new Field("LongName", info["LongName"].ToString(), Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field("Manufacturer", StringUtil.FilterManufacter(info["Manufacturer"].ToString()), Field.Store.YES, Field.Index.ANALYZED));

            //  其他索引信息  【评论、上架、上架时间、销量】
            doc.Add(new Field("Favorite", info["Favorite"].ToString(), Field.Store.YES, Field.Index.NO));
            doc.Add(new Field("Comments", info["Comments"].ToString(), Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field("Selling", info["Selling"].ToString(), Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field("SellingTime", info["SellingTime"].ToString(), Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field("Stock", (int.Parse(info["Stock"].ToString()) > 0 ? "1" : "0"), Field.Store.NO, Field.Index.ANALYZED));
            doc.Add(new Field("SellCount", info["SellCount"].ToString(), Field.Store.YES, Field.Index.NO));
            //  精品推荐(毛利率)
            decimal tradePrice = decimal.Parse(info["TradePrice"].ToString());
            decimal profit     = tradePrice - decimal.Parse(info["CostPrice"].ToString());

            doc.Add(new Field("Recommend", profit.ToString("0.00"), Field.Store.YES, Field.Index.NO));
            //  优惠推荐(折扣)
            decimal discount = 0;

            if (tradePrice > 0)
            {
                discount = tradePrice / decimal.Parse(info["MarketPrice"].ToString());
            }
            doc.Add(new Field("Preferential", discount.ToString("0.00"), Field.Store.YES, Field.Index.NO));

            //  商品基本信息
            doc.Add(new Field("ProductID", info["ProductID"].ToString(), Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field("ProductCode", info["ProductCode"].ToString(), Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field("ProductType", info["ProductType"].ToString(), Field.Store.YES, Field.Index.NO));
            doc.Add(new Field("Specifications", info["Specifications"].ToString(), Field.Store.YES, Field.Index.NO));
            doc.Add(new Field("Images", info["Images"].ToString(), Field.Store.YES, Field.Index.NO));
            doc.Add(new Field("Actions", info["Actions"].ToString(), Field.Store.YES, Field.Index.NO));
            doc.Add(new Field("MarketPrice", info["MarketPrice"].ToString(), Field.Store.YES, Field.Index.NO));
            doc.Add(new NumericField("TradePrice", Field.Store.YES, true).SetDoubleValue(double.Parse(info["TradePrice"].ToString())));

            //  品牌名称
            doc.Add(new Field("BrandID", info["BrandID"].ToString(), Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field("BrandName", info["BrandName"].ToString(), Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field("BrandLetter", info["BrandLetter"].ToString().ToUpper(), Field.Store.YES, Field.Index.NO));

            //  一级类目
            doc.Add(new Field("CFID1", info["CFID1"].ToString(), Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field("ParentID1", info["ParentID1"].ToString(), Field.Store.YES, Field.Index.NO));
            field = new Field("CFName1", info["CFName1"].ToString(), Field.Store.YES, Field.Index.ANALYZED);
            field.SetBoost(10f);
            doc.Add(field);
            //  二级类目
            doc.Add(new Field("CFID2", info["CFID2"].ToString(), Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field("ParentID2", info["ParentID2"].ToString(), Field.Store.YES, Field.Index.NO));
            field = new Field("CFName2", info["CFName2"].ToString(), Field.Store.YES, Field.Index.ANALYZED);
            field.SetBoost(10f);
            doc.Add(field);
            //  三级类目
            doc.Add(new Field("CFID3", info["CFID3"].ToString(), Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field("ParentID3", info["ParentID3"].ToString(), Field.Store.YES, Field.Index.NO));
            field = new Field("CFName3", info["CFName3"].ToString(), Field.Store.YES, Field.Index.ANALYZED);
            field.SetBoost(10f);
            doc.Add(field);

            return(doc);
        }
Exemple #3
0
        /// <summary>
        /// 关键词多条件搜索
        /// </summary>
        /// <param name="keyword">关键词</param>
        /// <param name="brand">品牌</param>
        /// <param name="cfid">分类</param>
        /// <param name="pricelimit">价格区间</param>
        /// <param name="store">是否有货</param>
        /// <param name="order">排序</param>
        /// <param name="recCount">总记录数</param>
        /// <param name="pageLen">条数</param>
        /// <param name="pageNo">页码</param>
        /// <returns>搜索结果</returns>
        public override JXSearchProductResult Search(RequestForm form, out int recCount)
        {
            recCount = 0;
            JXSearchProductResult result = new JXSearchProductResult();
            IndexSearcher         search = new IndexSearcher(CURR_INDEX_DIR);

            try
            {
                #region 搜索条件
                Query        query1 = null;
                BooleanQuery query  = new BooleanQuery();
                if (StringUtil.IsNatural_Number(form.queryForm.keyword))
                {
                    //  商品ID、金象码、拼音
                    BooleanClause.Occur[] flags = new BooleanClause.Occur[fieldsAbbr.Length];
                    for (int n = 0; n < fieldsAbbr.Length; n++)
                    {
                        flags[n] = BooleanClause.Occur.SHOULD;
                    }
                    query1 = MultiFieldQueryParser.Parse(Lucene.Net.Util.Version.LUCENE_29, form.queryForm.keyword, fieldsAbbr, flags, new PanGuAnalyzer(true));
                    query.Add(query1, BooleanClause.Occur.MUST);
                }
                else
                {
                    //  关键词检索字段\t\r\n\\t
                    BooleanClause.Occur[] flags = new BooleanClause.Occur[fields.Length];
                    for (int n = 0; n < fields.Length; n++)
                    {
                        flags[n] = BooleanClause.Occur.SHOULD;
                    }
                    //  转换关键词拼音
                    string pinyinName = PinyinUtil.ConvertToPinyin(GetKeyWordsSplitFilter(form.queryForm.keyword, new PanGuTokenizer()));

                    //  关键词检索
                    //  BooleanClause.Occur.MUST 表示 and
                    //  BooleanClause.Occur.MUST_NOT 表示 not
                    //  BooleanClause.Occur.SHOULD 表示 or
                    string q = GetKeyWordsSplitBySpace(string.Format("{0} {1}", form.queryForm.keyword, pinyinName), new PanGuTokenizer());
                    query1 = MultiFieldQueryParser.Parse(Lucene.Net.Util.Version.LUCENE_29, q, fields, flags, new PanGuAnalyzer(true));
                    query.Add(query1, BooleanClause.Occur.MUST);
                }

                //  品牌
                if (form.queryForm.brandIds != null && form.queryForm.brandIds.Count() > 0)
                {
                    BooleanQuery tmpBQ = new BooleanQuery();
                    foreach (int key in form.queryForm.brandIds)
                    {
                        tmpBQ.Add(new TermQuery(new Term("BrandID", key.ToString())), BooleanClause.Occur.SHOULD);
                    }
                    query.Add(tmpBQ, BooleanClause.Occur.MUST);
                }

                //  分类
                if (form.queryForm.cfid > 0)
                {
                    BooleanQuery tmpBQ = new BooleanQuery();
                    tmpBQ.Add(new TermQuery(new Term("CFID1", form.queryForm.cfid.ToString())), BooleanClause.Occur.SHOULD);
                    tmpBQ.Add(new TermQuery(new Term("CFID2", form.queryForm.cfid.ToString())), BooleanClause.Occur.SHOULD);
                    tmpBQ.Add(new TermQuery(new Term("CFID3", form.queryForm.cfid.ToString())), BooleanClause.Occur.SHOULD);
                    query.Add(tmpBQ, BooleanClause.Occur.MUST);
                }

                //  库存
                if (form.queryForm.stock > 0)
                {
                    query.Add(new TermQuery(new Term("Stock", "1")), BooleanClause.Occur.MUST);
                }

                //  价格区间
                if (!string.IsNullOrEmpty(form.queryForm.pricelimit))
                {
                    string[] price = form.queryForm.pricelimit.Split(",".ToCharArray());
                    if (price.Length > 0 && double.Parse(price[1]) > 0)
                    {
                        query.Add(NumericRangeQuery.NewDoubleRange("TradePrice", double.Parse(price[0]), double.Parse(price[1]), true, false), BooleanClause.Occur.MUST);
                    }
                }

                //  是否有货    Selling 是否在卖 1:在卖 0:下架 2:暂无库存
                if (form.queryForm.store == 1)
                {
                    query.Add(new TermQuery(new Term("Selling", form.queryForm.store.ToString())), BooleanClause.Occur.MUST);
                }

                Hits hits = search.Search(query);
                #endregion

                //  构造商品结果
                result.listProductList = ProductBinding(hits, form.queryForm.keyword, form.queryForm.order, form.pageForm.page, form.pageForm.size, out recCount);

                #region 构造结果参数 品牌/分类
                //  排除搜索条件重新搜索
                if ((form.queryForm.brandIds != null && form.queryForm.brandIds.Count() > 0) || form.queryForm.cfid > 0 || form.queryForm.store == 1 || form.queryForm.order > 0 || !string.IsNullOrEmpty(form.queryForm.pricelimit))
                {
                    BooleanQuery booleanQuery = new BooleanQuery();
                    booleanQuery.Add(query1, BooleanClause.Occur.MUST);
                    hits = search.Search(query);
                }

                //  构造分类、品牌结果
                IList <JXSearchEntity> list = ProductParaList(hits);
                if (list != null)
                {
                    //  品牌
                    result.listBrand = list.Where(g => g.typeID == 5).ToList();

                    //  一级分类
                    IList <JXSearchEntity> categoryList = list.Where(g => g.typeID == 2 && g.parentID == 0).ToList();
                    if (categoryList != null)
                    {
                        foreach (JXSearchEntity item in categoryList)
                        {
                            //  二级分类
                            item.listSubClassification = list.Where(g => g.typeID == 2 && g.parentID == item.id).ToList();
                            foreach (JXSearchEntity subItem in item.listSubClassification)
                            {
                                //  三级分类
                                subItem.listSubClassification = list.Where(g => g.typeID == 2 && g.parentID == subItem.id).ToList();
                            }
                        }
                    }
                    result.listClassification = categoryList;
                }
                #endregion

                //  页数
                result.totalPage  = Convert.ToInt32(Math.Ceiling((double)recCount / form.pageForm.size));
                result.resultCode = "SUCCEED";
                result.resultMsg  = "SUCCEED";
            }
            catch { }
            finally
            {
                search.Close();
                search.Dispose();
            }
            return(result);
        }