用ElasticSearch过滤产品会过滤很多过滤器

时间:2018-07-19 21:27:49

标签: c# elasticsearch filtering nest

不幸的是,我已经尝试使用Elasticsearch过滤产品了几个小时,但无济于事。 我需要找到属于某些类别的产品,同时选择了几种品牌和一种尺寸。

帮助:(

json screen

querycontainer构建方法

    private QueryContainer CreateOrQueryFromFilter(QueryContainer queryContainer, SortedSet<string> filter, string fieldName)
    {
        if (filter != null && filter.Count > 0)
        {
            foreach (var item in filter)
            {
                queryContainer |= new TermQuery()
                {
                    Name = fieldName + "named_query",
                    Boost = 1.1,
                    Field = fieldName,
                    Value = item
                };
            }
        }
        return queryContainer;
    }

和搜索方法

  public ResultModel SearchRequest(RequestModel r)
    {
        string key = string.Format("search-{0}-{1}", r.CacheKey + "-" + ProductCatalog.Model.Extension.StringHelper.UrlFriendly(r.SearchText), r.Prefix);

        node = new Uri("http://xxxx:9200/");
        settings = new ConnectionSettings(node);
        settings.DisableDirectStreaming();
        settings.DefaultIndex("products");

        client = new ElasticClient(settings);


        //   return AppCache.Get(key, () =>
        //  {
        DateTime start = DateTime.Now;

        ResultModel result = new ResultModel(r.Canonical, r.RouteObject);

        if (!string.IsNullOrEmpty(r.Prefix))
        {
            result.Prefix = r.Prefix;
        }

        QueryContainer c = new QueryContainer();


        if (r.CategoryFilterChilds != null && r.CategoryFilterChilds.Count > 0)
        {
            var a1 = new SortedSet<string>(r.CategoryFilterChilds.Select(a => (string)a.ToString()));
            c = CreateOrQueryFromFilter(c, a1, "categories");

        }
        else
        {
            if (r.CategoryFilterRoots != null && r.CategoryFilterRoots.Count > 0)
            {
                var a1 = new SortedSet<string>(r.CategoryFilterRoots.Select(a => (string)a.ToString()));
                c = CreateOrQueryFromFilter(c, a1, "categories");
            }
            else
            {
                // null
            }
        }



        var filters = new AggregationDictionary();


        if (r.IsBrandFilter)
        {
            c = CreateOrQueryFromFilter(c, r.SelectedBrands, "brands");
        }

        if (r.IsColorFilter)
        {
            c = CreateOrQueryFromFilter(c, r.SelectedBrands, "colors");
        }


        int skip = (r.Page * r.PageSize) - r.PageSize;

        ISearchRequest r2 = new SearchRequest("products");

        r2.From = 1;
        r2.Size = r.PageSize;
        r2.Query = c;


        string[] Fields = new[] { "brands", "shopId" };
        AggregationBase aggregations = null;
        foreach (string sField in Fields)
        {
            var termsAggregation = new TermsAggregation("agg_" + sField)
            {
                Field = sField,
                Size = 120,
                Order = new List<TermsOrder> { TermsOrder.TermDescending }
            };

            if (aggregations == null)
            {
                aggregations = termsAggregation;
            }
            else
            {
                aggregations &= termsAggregation;
            }
        }

        r2.Aggregations = aggregations;



        var c2 = client.Search<ShopProductElastic>(r2);



        var ShopsBuf = (Nest.BucketAggregate)(c2.Aggregations["agg_brands"]);
        var ShopsCount = ShopsBuf.Items.Count();
        var results = c2;

        result.BrandsRequest = new SortedDictionary<string, int>();

        foreach (Nest.KeyedBucket<object> item in ShopsBuf.Items)
        {
            result.BrandsRequest.Add((string)item.Key, (int)(item.DocCount ?? 0));
        }

        result.CategorySelected = r.CategoryCurrent;

        result.TotalCount = 10;

        var costam = results.Documents.ToList();

        var targetInstance = Mapper.Map<List<Products>>(costam);

        result.Products = targetInstance;

        result.Page = r.Page;
        result.PageSize = r.PageSize;

        result.IsBrandFilter = r.IsBrandFilter;
        result.IsColorFilter = r.IsColorFilter;
        result.IsPatternFilter = r.IsPatternFilter;
        result.IsSeasonFilter = r.IsSeasonFilter;
        result.IsShopFilter = r.IsShopFilter;
        result.IsSizeFilter = r.IsSizeFilter;
        result.IsStyleFilter = r.IsStyleFilter;
        result.IsTextileFilter = r.IsTextileFilter;

        DateTime stop = DateTime.Now;


        result.SearchTime = stop - start;
        result.SearchText = r.SearchText;

        return result;
        // }, TimeSpan.FromHours(8));

    }

我拥有具有给定品牌或类别的所有产品。但是,我需要选定类别中选定品牌的所有产品

0 个答案:

没有答案