Beispiel #1
0
        public void SearchProducts(SearchParamModel model)
        {
            var data = new PagerData <object>();

            var response = SearchEsProducts(model);

            data.ItemCount = (int)(response?.Total ?? 0);
            var datalist = response?.Hits?.Select(x => x as Hit <ProductListV2>).Where(x => x != null).Select(x => x.Source).Where(x => x != null).ToList();

            //聚合的数据
            var aggs = GetAggs(response);

            data.DataList = ConvertHelper.NotNullList(data.DataList);
        }
Beispiel #2
0
        private SortDescriptor <ProductListV2> BuildSort(SearchParamModel model)
        {
            var sort = new SortDescriptor <ProductListV2>();

            sort = sort.Descending(x => x.InventoryStatus);
            if (model.order_rule == "1")
            {
                sort = sort.Descending(x => x.SalesVolume);
            }
            else if (model.order_rule == "2")
            {
                sort = sort.Ascending(x => x.SalesPrice);
            }
            else
            {
                sort = sort.Descending(x => x.SalesPrice);
            }
            return(sort);
        }
Beispiel #3
0
        public ISearchResponse <ProductListV2> SearchEsProducts(SearchParamModel model)
        {
            ISearchResponse <ProductListV2> response = null;
            var temp = new ProductListV2();

            PrepareES(client =>
            {
                var sd = new SearchDescriptor <ProductListV2>();

                sd = sd.Index(ES_PRODUCTLIST_INDEX);

                sd = sd.Query(q => BuildQuery(model));

                var NAMEOF_ShowCatalogIdList = nameof(temp.ShowCatalogIdList);
                var NAMEOF_BrandId           = nameof(temp.BrandId);
                var NAMEOF_ProductAttributes = nameof(temp.ProductAttributes);

                sd = sd.Aggregations(agg => agg
                                     .Terms(NAMEOF_ShowCatalogIdList, av => av.Field(NAMEOF_ShowCatalogIdList).Size(1000))
                                     .Terms(NAMEOF_BrandId, av => av.Field(NAMEOF_BrandId).Size(1000))
                                     .Terms(NAMEOF_ProductAttributes, av => av.Field(NAMEOF_ProductAttributes).Size(1000)));

                sd = sd.Sort(x => BuildSort(model));

                //var range = PagerHelper.GetQueryRange(model.page, model.pagesize);
                //sd = sd.Skip(range[0]).Take(range[1]);
                sd = sd.QueryPage_(model.page, model.pagesize);

                response = client.Search <ProductListV2>(x => sd);

                var mx = response.Aggs.Max("");

                return(true);
            });
            if (response == null || !response.IsValid || response.OriginalException != null)
            {
                throw new Exception("ES 挂了");
            }
            return(response);
        }
Beispiel #4
0
        private SortDescriptor <ProductListV2> BuildSort(SearchParamModel model)
        {
            var sort = new SortDescriptor <ProductListV2>();

            sort = sort.Descending(x => x.InventoryStatus);
            if (model.order_rule == "1")
            {
                sort = sort.Descending(x => x.SalesVolume);
            }
            else if (model.order_rule == "2")
            {
                sort = sort.Ascending(x => x.SalesPrice);
            }
            else
            {
                sort = sort.Descending(x => x.SalesPrice);
            }

            //更具坐标排序
            sort = sort.GeoDistance(x => x.Field(f => f.IsGroup).PinTo(new GeoLocation(52.310551, 4.404954)).Ascending());

            return(sort);
        }
Beispiel #5
0
        private QueryContainer BuildQuery(SearchParamModel model)
        {
            var temp = new ProductListV2();
            var qc   = new QueryContainer();
            {
                var traderlist = new List <string>();
                if (!ValidateHelper.IsPlumpString(model.province))
                {
                    throw new Exception("缺少区域信息");
                }
                if (ValidateHelper.IsPlumpString(model.trader))
                {
                    if (traderlist.Contains(model.trader))
                    {
                        traderlist.Clear();
                        traderlist.Add(model.trader);
                    }
                    else
                    {
                        traderlist.Clear();
                    }
                }
                if (!ValidateHelper.IsPlumpList(traderlist))
                {
                    traderlist = new List <string>()
                    {
                        "构造一个不可能存在的值"
                    };
                }
                qc = qc && new TermsQuery()
                {
                    Field = nameof(temp.TraderId), Terms = traderlist
                };
            }
            var idlist = new string[] { };

            if (!new string[] { "2", "4" }.Contains(model.CustomerType))
            {
                qc = qc && (!new TermsQuery()
                {
                    Field = nameof(temp.UKey), Terms = idlist
                });
            }
            else
            {
                qc = qc && (!new TermsQuery()
                {
                    Field = nameof(temp.UKey), Terms = idlist
                });
            }
            if (ValidateHelper.IsPlumpString(model.brand))
            {
                var brand_sp = ConvertHelper.GetString(model.brand).Split(',').Where(x => ValidateHelper.IsPlumpString(x)).ToArray();
                qc = qc && new TermsQuery()
                {
                    Field = nameof(temp.BrandId), Terms = brand_sp
                };
            }
            if (ValidateHelper.IsPlumpString(model.catalog))
            {
                qc = qc && (new TermQuery()
                {
                    Field = nameof(temp.PlatformCatalogId), Value = model.catalog
                } ||
                            new TermsQuery()
                {
                    Field = nameof(temp.PlatformCatalogIdList), Terms = new object[] { model.catalog }
                } ||
                            new TermsQuery()
                {
                    Field = nameof(temp.ShowCatalogIdList), Terms = new object[] { model.catalog }
                });
            }
            if (model.min_price >= 0)
            {
                qc = qc && new NumericRangeQuery()
                {
                    Field = nameof(temp.SalesPrice), GreaterThanOrEqualTo = (double)model.min_price
                };
            }
            if (model.max_price >= 0)
            {
                qc = qc && new NumericRangeQuery()
                {
                    Field = nameof(temp.SalesPrice), LessThanOrEqualTo = (double)model.max_price
                };
            }

            new GeoDistanceQuery()
            {
            };
            qc = qc && new GeoDistanceRangeQuery()
            {
                Field             = "Location",
                Location          = new GeoLocation(32, 43),
                LessThanOrEqualTo = Distance.Kilometers(1)
            };

            try
            {
                if (!ValidateHelper.IsPlumpString(model.attr))
                {
                    model.attr = "[]";
                }
                var attr_list = model.attr.JsonToEntity <List <AttrParam> >();

                /*
                 * if (ValidateHelper.IsPlumpList(attr_list))
                 * {
                 *  var attr_query = new QueryContainer();
                 *  foreach (var attr in attr_list)
                 *  {
                 *      attr_query = attr_query || new TermQuery() { Field = $"{nameof(template.ProductAttributes)}.{attr.UID}", Value = attr.value };
                 *  }
                 *  qc = qc && new NestedQuery() { Path = nameof(template.ProductAttributes), Query = attr_query };
                 * }
                 */
                if (ValidateHelper.IsPlumpList(attr_list))
                {
                    //qc = qc && new TermsQuery() { Field = nameof(temp.ProductAttributes), Terms = attr_list.Select(attr => $"{attr.UID}@$@{attr.value}") };
                    foreach (var attr_key in attr_list.Select(x => x.UID).Distinct())
                    {
                        qc = qc && new TermsQuery()
                        {
                            Field = nameof(temp.ProductAttributes), Terms = attr_list.Where(x => x.UID == attr_key).Select(attr => $"{attr.UID}@$@{attr.value}")
                        };
                    }
                }
            }
            catch { }
            if (model.isGroup)
            {
                qc = qc && new TermQuery()
                {
                    Field = nameof(temp.IsGroup), Value = 1
                };
            }
            if (ValidateHelper.IsPlumpString(model.qs))
            {
                qc = qc && (new MatchQuery()
                {
                    Field = nameof(temp.ShopName), Query = model.qs, Operator = Operator.Or, MinimumShouldMatch = "100%"
                } ||
                            new MatchQuery()
                {
                    Field = nameof(temp.SeachTitle), Query = model.qs, Operator = Operator.Or, MinimumShouldMatch = "100%"
                });
            }

            qc = qc && new TermQuery()
            {
                Field = nameof(temp.PAvailability), Value = 1
            };
            qc = qc && new TermQuery()
            {
                Field = nameof(temp.UpAvailability), Value = 1
            };
            qc = qc && new TermQuery()
            {
                Field = nameof(temp.PIsRemove), Value = 0
            };
            qc = qc && new TermQuery()
            {
                Field = nameof(temp.UpIsRemove), Value = 0
            };
            qc = qc && new NumericRangeQuery()
            {
                Field = nameof(temp.SalesPrice), GreaterThan = 0
            };

            return(qc);
        }