0

这个问题建立在以下问题之上

索引:考虑到这些要求,我如何为本文档构建我的 RavenDb 静态索引?

带分页的简单 Where 子句:如何使用 RavenDb 构造适当的 WHERE 子句

问题的本质是如何动态添加或删除字段以参与 where 子句?

文档:

[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; }

}

指数:

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}
        }
    });
}

朴素的 Where 子句

  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()
      ;

高级查询的第一关

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

抛出异常

Lucene.Net.dll 中出现“Lucene.Net.QueryParsers.QueryParser.LookaheadSuccess”类型的第一次机会异常 Lucene.Net.dll 中出现“System.IO.IOException”类型的第一次机会异常

第二关(作品)

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

第三次通过(也是最快的)

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()
    ;
4

1 回答 1

2

如果您想动态地执行此操作,您可以使用 DocumentSession.Advanced.LuceneQuery,它允许您将字符串作为索引的属性名称传递。这样,您就不必处理强类型问题。

于 2012-04-04T08:10:23.460 回答