How do I construct my RavenDb where clause for this document, given these requirements?

Go To StackoverFlow.com

0

This question builds on the following question (s)

Indexing : How do I construct my RavenDb static indexes for this document, given these requirements?

Simple Where clause with paging : How to construct a proper WHERE clause with RavenDb

The essence of the question is how do I dynamically add or remove fields to participate in a where clause?

Document:

[Serializable]
public class Product
{
    public string AveWeight { get; set; }

    public string BrandName { get; set; }

    public string CasePack { get; set; }

    public string Catalog { get; set; }

    public decimal CatalogId { get; set; }

    public decimal CategoryId { get; set; }

    public string Info { get; set; }

    public bool IsOfflineSupplierItem { get; set; }

    public bool IsRebateItem { get; set; }

    public bool IsSpecialOrderItem { get; set; }

    public bool IsSpecialPriceItem { get; set; }

    public bool IsTieredPricingItem { get; set; }

    public string ItemNum { get; set; }

    public string ManufactureName { get; set; }

    public string ManufactureNum { get; set; }

    public decimal OffineSupplierId { get; set; }

    public string PackageRemarks { get; set; }

    public decimal Price { get; set; }

    public decimal PriceGroupId { get; set; }

    public decimal ProductId { get; set; }

    public string ProductName { get; set; }

    public int Quantity { get; set; }

    public string SupplierName { get; set; }

    public string UOM { get; set; }

    public string Upc { get; set; }

    public string Url { get; set; }

}

Index:

if (store.DatabaseCommands.GetIndex("Products_Index") == null)
{
    store.DatabaseCommands.PutIndex("Products_Index", new IndexDefinitionBuilder<Product>
    {
        Map = products => from p in products
                          select new { p.CatalogId, 
                                         p.HasPicture, 
                                         p.INFO2, 
                                         p.IsOfflineSupplierItem, 
                                         p.IsRebateItem, 
                                         p.IsSpecialOrderItem, 
                                         p.IsSpecialPriceItem, 
                                         p.IsTieredPricingItem, 
                                         p.Price },
        Indexes = 
        { 
            { x => x.INFO2, FieldIndexing.Analyzed }, 
            { x => x.CatalogId, FieldIndexing.Default},
            { x => x.HasPicture, FieldIndexing.Default},
            { x => x.IsOfflineSupplierItem, FieldIndexing.Default},
            { x => x.IsRebateItem, FieldIndexing.Default},
            { x => x.IsSpecialOrderItem, FieldIndexing.Default},
            { x => x.IsSpecialPriceItem, FieldIndexing.Default},
            { x => x.IsTieredPricingItem, FieldIndexing.Default},
            { x => x.Price, FieldIndexing.Default}
        }
    });
}

Naive Where clause

  string  t1 = "foo";
  bool    t2 = true;
  decimal t3 = 100m;

  products = DocumentSession.Query<Product>()
      .Statistics(out stats)
      .Where(p => p.INFO2.StartsWith(t1) && p.IsRebateItem == t2 && p.CatalogId = t3) 
      .OrderByField(columnToSortBy, columnToSortByAsc)
      .Skip(pageIndex * pageSize)
      .Take(pageSize)
      .ToList()
      ;

First Pass at Advanced Query

 var products = s.Advanced.LuceneQuery<Product>("Products")
    .WhereEquals("Catalog", "National Catalog")
    .ToList()
    ;

which throws an exception

A first chance exception of type 'Lucene.Net.QueryParsers.QueryParser.LookaheadSuccess' occurred in Lucene.Net.dll A first chance exception of type 'System.IO.IOException' occurred in Lucene.Net.dll

Second pass (works)

 result = s.Advanced.LuceneQuery<Product>("Products_Index")
 .Where("CatalogId:(736275001) AND HasPicture:(true) AND IsOfflineSupplierItem:(false)")
 .ToArray();

Third Pass (and fastest yet)

result = s.Advanced.LuceneQuery<Product>("Products/Index")
    .Statistics(out stats)
    .WhereStartsWith("INFO2", "ink")
    .AndAlso()
    .WhereStartsWith("INFO2", "pen")
    .AndAlso()
    .WhereEquals("CatalogId", 736275001)
    .AndAlso()
    .WhereEquals("HasPicture", true)
    .AndAlso()
    .WhereEquals("IsOfflineSupplierItem", false)
    .AndAlso()
    .WhereEquals("IsRebateItem", false)
    .AndAlso()
    .WhereEquals("IsSpecialOrderItem", false)
    .AndAlso()
    .WhereEquals("IsSpecialPriceItem", false)
    .ToArray()
    ;
2012-04-04 02:44
by Stephen Patten
Don't do if GetIndex == null then PutIndex - it won't handle index changes. See http://ravendb.net/docs/client-api/querying/static-indexes/defining-static-inde - synhershko 2012-04-08 17:16


2

If you want to do this dynamically, you can use the DocumentSession.Advanced.LuceneQuery, which allows you to pass strings as the property names for the index. That way, you don't have to deal with the strongly typed issues.

2012-04-04 08:10
by Ayende Rahien
For my case it work's perfect - Stephen Patten 2012-04-06 12:55
Ads