【问题标题】:Azure Search Using Multiple filters使用多个筛选器的 Azure 搜索
【发布时间】:2020-06-14 19:16:34
【问题描述】:

我有三个过滤器,即需要应用于搜索查询的类别、子类别和产品类型。我目前正在使用 Azure 搜索。截至目前,我正在使用 odata 过滤器,如下面的代码所示。

SearchParameters parameters = new SearchParameters();

        string searchText = Request.Query["query"];
        string category = Request.Query["c"];
        string subCategory = Request.Query["sc"];
        string productType = Request.Query["pt"];

        parameters.Filter = $"Category/Id eq '{category}' or SubCategory/Id eq '{subCategory}'" +
                              $"or ProductType/Id eq '{productType}'";

我如何获得一个结果,如果用户应用 categoryId,那么它应该显示与该 Category Id 相关的结果。如果用户同时应用 categoryId 和 subcategoryId,他应该能够看到与指定匹配的两个产品过滤类别和子类别。如果只提交 producttypeId,那么他应该能够看到与该 producttypeId 相关的产品。当然,我可以使用 if 和 else 并使用“逻辑或”和“逻辑与”构建我的过滤器。 如果有任何 Odata 库可以在这方面帮助我?

【问题讨论】:

    标签: c# azure-cognitive-search asp.net-core-2.2


    【解决方案1】:

    感谢您的建议。我使用 OData FilterString 解决了这个问题

     string filterquery = FilterString.Generate<Product>(p => p.Category.Id == category && 
                p.SubCategory.Id == subCategory &&                            
                p.ProductType.Id == productType);
    
     parameters.Filter = filterQuery;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-27
      • 2021-06-01
      • 2021-09-02
      • 2020-01-02
      • 1970-01-01
      • 2021-03-06
      相关资源
      最近更新 更多