1

我正在尝试实现这个多面搜索逻辑:http ://ravendb.net/docs/2.0/client-api/faceted-search 。但是从数据库返回的每个价格范围的命中都没有按预期工作;所有命中都在最后一个价格范围内分组。

我的产品文档如下所示:

{
  "Sku": "000000000000069673",
  "Title": "APPLE ME186",
  "Brand": "APPLE",
  "RegularPrice": 84.99,
  "ReferencePrice": 0.0,
  "YouSavePrice": 0.0,
  "ShortDescription": "",
  "Description": "",
  "CategoryHierarchyPath": "Beeld en geluid/Hoofdtelefoons/In-ear koptelefoon",
  "Categories": [
    "0/Beeld en geluid",
    "1/Beeld en geluid/Hoofdtelefoons",
    "2/Beeld en geluid/Hoofdtelefoons/In-ear koptelefoon"
  ],
  "Stocks": [
    {
      "Quantity": ,
      "Branch": ""
    }
  ],
  "Images": [],
  "Attributes": [
    {
      "Name": "",
      "Value": ""
    }
  ]
}

我要查询的索引:

public class CategoryProducts : AbstractIndexCreationTask<Product, CategoryProducts.ReduceResult>
{
    public class ReduceResult
    {
        public string Category { get; set; }
        public string Title { get; set; }
        public decimal RegularPrice { get; set; }
        public string ShortDescription { get; set; }
        public int Description { get; set; }
    }

    public CategoryProducts()
    {
        Map = products =>
              from p in products
              from c in p.Categories
              select new
              {
                  Category = c,
                  Title = p.Title,
                  RegularPrice = p.RegularPrice,
                  ShortDescription = p.ShortDescription,
                  Description = p.Description
              };
    }
}

方面设置:

Facets = new List<Facet>
{
new Facet
    {
        Name = "RegularPrice",
        Mode = FacetMode.Ranges,
        Ranges =
            {
                "[NULL TO Dx200.0]",
                "[Dx200.0 TO Dx400.0]",
                "[Dx400.0 TO Dx600.0]",
                "[Dx600.0 TO Dx800.0]",
                "[Dx800.0 TO NULL]",
            }
    }
}

查询:

var priceRangeFacets = Session.Query<CategoryProducts.ReduceResult, CategoryProducts>()
            .Where(r => r.Category.StartsWith("1/Beeld en   geluid/Hoofdtelefoons")).ToFacets("facets/PriceRanges")

结果:

[0] = {Range: [NULL TO Dx200.0], Hits: 0}
[1] = {Range: [Dx200.0 TO Dx400.0], Hits: 0}
[2] = {Range: [Dx400.0 TO Dx600.0], Hits: 0}
[3] = {Range: [Dx600.0 TO Dx800.0], Hits: 0}
[4] = {Range: [Dx800.0 TO NULL], Hits: 77}

请注意,我在数据库中有比 800 便宜的产品。

PS 我正在使用 RavenDB-Build-2330

4

1 回答 1

1

Resolved after Ayende's correction for the failing test I had created:

  • When doing range searches on a numeric field, you need to use the _Range postfix
  • You need to do [NULL TO Dx200.0}

[ Is inclusive, } is exclusive.

So our facet setup should be declared like:

Facets = new List<Facet>
{
    new Facet
    {
        Name = "RegularPrice_Range",
        Mode = FacetMode.Ranges,
        Ranges =
        {
            "[NULL TO Dx200.0}",
            "[Dx200.0 TO Dx400.0}",
            "[Dx400.0 TO Dx600.0}",
            "[Dx600.0 TO Dx800.0}",
            "[Dx800.0 TO NULL}",
        }
    }
}
于 2013-05-13T07:56:18.283 回答