为了过滤 RavenDB 中的产品,我使用了一个通用的产品过滤器列表——以避免长时间切换。
产品过滤器类如下所示:
public class ProductFilter
{
public string FilterName { get; set; }
public List<string> FilterValues { get; set; }
public ProductFilter()
{
FilterValues = new List<string>();
}
}
FilterName 等于索引属性名称
我使用这个索引来查询产品:
public class CategoryProducts_Index : AbstractIndexCreationTask<Product, CategoryProducts_Index.ReduceResult>
{
public class ReduceResult
{
public string Category { get; set; }
public string Title { get; set; }
public string Brand { get; set; }
public decimal RegularPrice { get; set; }
}
public CategoryProducts_Index()
{
Map = products =>
from p in products
from c in p.Categories
select new
{
Category = c,
Title = p.Title,
Brand = p.Brand,
RegularPrice = p.RegularPrice
};
}
}
对于通过循环 ProductFilter 列表来过滤产品的通用解决方案,我想实现一个 Lucene 查询分组,例如:
Category: parent_category AND (Category: sub_category_1 OR Category: sub_category_2 OR ...etc) AND (Brand: brand1 OR Brand:brand2 OR ...etc)
使用普通的 Linq 查询,我无法通过字符串名称检索索引属性,因此我尝试使用 LuceneQuery 执行此操作,如下所示:
public IDocumentQuery<Product> GetProductsBySelectedFilters(string category, List<ProductFilter> productFilters)
{
IDocumentQuery<Product> products;
using (var session = DocumentStore.OpenSession())
{
products = session.Advanced.LuceneQuery<Product>("CategoryProducts/Index", isMapReduce: true)
.WhereStartsWith("Category", category);
foreach (var filter in productFilters)
{
products.UsingDefaultOperator(QueryOperator.And);
foreach (var value in filter.FilterValues)
{
products.WhereEquals(filter.FilterName, value);
products.UsingDefaultOperator(QueryOperator.Or);
}
}
}
return products;
}
这个的 Lucene 输出查询是这样的:
Category: parent_category Category: sub_category_1 Category: sub_category_2 Brand: brand1 Brand: brand2 ...etc
这不是我所期望的
有人知道如何处理吗?