user1829226
user1829226

Reputation: 145

RavenDB Query not working

I can't seem to get this to work.

Basically I have a site that got a list of excluded brands and categories. which are stored like brands/1 category/123

I'm trying to query my product document and return first 20 results that don't have any excluded categories/ brands.

using (var session = documentStore.OpenSession())
 {
      var site = session.Load<Site>(193);


      List<string> excludedCategories =  session.Load<Category>(site.ExcludedCategories).Select(a => string.Format("brands/{0}",a.Id)).ToList();
      var excludedBrands = session.Load<Brand>(site.ExcludedBrands).Select(a => string.Format("categories/{0}",a.Id)).ToList();


      List<Product> ps = session.Query<Product>()
                                .Where(prod => excludedBrands.Any(a => !prod.Brands.Contains(a)) 
                                            && excludedCategories.Any(a => !prod.Categories.Contains(a)))
                                .OrderBy(a=>a.ProductGroup)
                                .Take(20)
                                .ToList();
}

Anyone let me know if I'm on the right lines?

Currently getting the following error:

Lucene.Net.QueryParsers.ParseException: Could not parse: '(: AND) AND -(: AND)' ---> Lucene.Net.QueryParsers.ParseException: Cannot parse '(: AND) AND -(: AND)': Encountered " ")" ") "" at line 1, column 8. Was expecting one of: ... "+" ... "-" ... "(" ... "" ... ... ... ... ... "[" ... "{" ... ... ... "" ... ---> Lucene.Net.QueryParsers.ParseException: Encountered " ")" ") "" at line 1, column 8. Was expecting one of: ... "+" ... "-" ... "(" ... "*" ... ... ... ... ... "[" ... "{" ... ... ...

Upvotes: 0

Views: 286

Answers (1)

Ayende Rahien
Ayende Rahien

Reputation: 22956

You cannot do a Contains operation during a query, that require computation and doesn't use an index. Use the query like so:

  List<Product> ps = session.Query<Product>()
                            .Where(prod => !prod.Brands.In(excludedBrands) && 
                            !prod.Categories.In(excludedCategories))
                            .OrderBy(a=>a.ProductGroup)
                            .Take(20)
                            .ToList();

If you still have an issue, call ToString() on the query and see what it actually does.

Upvotes: 1

Related Questions