linq - How to group a set of filter operators in a RavenDB query? -
for filtering products in ravendb use generic list of product filters - avoid long switch.
the product filter class looks like:
public class productfilter { public string filtername { get; set; } public list<string> filtervalues { get; set; } public productfilter() { filtervalues = new list<string>(); } } the filtername equal index property name
i use index querying products:
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 => p in products c in p.categories select new { category = c, title = p.title, brand = p.brand, regularprice = p.regularprice }; } } for generic solution filter products looping list of productfilter, want achieve lucene query grouping like:
category: parent_category , (category: sub_category_1 or category: sub_category_2 or ...etc) , (brand: brand1 or brand:brand2 or ...etc) with normal linq query not retrieve index property string name, tried lucenequery follows:
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; } the lucene output query of this, like:
category: parent_category category: sub_category_1 category: sub_category_2 brand: brand1 brand: brand2 ...etc that's not expected
someone idea how handle this?
use lucenequery's .opensubclause() , .closesubclause()
Comments
Post a Comment