为了过滤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
这不是我的预期
有人知道如何处理这个问题吗?
答案 0 :(得分:1)
使用LuceneQuery的.OpenSubclause()和.CloseSubclause()