RavendDB分面搜索结果格式化

时间:2012-02-23 13:32:35

标签: asp.net-mvc-4 formatting ravendb faceted-search

目前,在阅读RavenDB doc之后,我正在玩分面搜索。

返回的结果还可以,但是有一个小问题。由于结果是

IDictionary<string, IEnumerable<FacetValue>>

有必要迭代它并进行一些奇特的字符串操作来格式化结果并在PartialView中显示它。更具体地说,这个方面:

new Facet
    {
        Name = "Value_Range",
        Mode = FacetMode.Ranges,
        Ranges =
            {
                "[NULL TO Dx500.0]",
                "[Dx500.0 TO Dx1000.0]",
                "[Dx1000.0 TO Dx2500.0]",
                "[Dx2500.0 TO Dx5000.0]",
                "[Dx5000.0 TO NULL]",
            }
    } 

View代码:

@fv.Range

这是“漂亮的”字符串,可在视图中输出:[Dx400.0 TO Dx600.0]

RavenDB使用上面的Dx前缀来执行number to string conversion

将facet结果传递给特定Controller

ViewModel代码:

var facetResults = DocumentSession.Query<Realty>("RealtyFacets")
            //.Where(x => x.Value >= 100 && x.Value <= 1000)
            .ToFacets("facets/RealtyFacets").ToArray();

var model = new RealtyFacetsViewModel();

model.Cities = facetResults[0];
model.Purposes = facetResults[1];
model.Types = facetResults[2];
model.Values = facetResults[3];

return PartialView("RealtyFacets", model);

是否有其他/更好的方法从分面搜索中获取结果,以便不必对字符串进行操作以格式化返回的数据?

在Ayende的建议之后,我在我的控制器中做到了这一点:

foreach (var val in facetResults[3].Value)
{
    switch(val.Range)
    {
        case "[Dx0.0 TO Dx200.0]":
            val.Range = string.Format("{0:C2} {1} {2:C2}",
                                        0, @Localization.to, 200);
            break;
        case "[Dx200.0 TO Dx400.0]":
            val.Range = string.Format("{0:C2} {1} {2:C2}",
                                        200, @Localization.to, 400);
            break;
        case "[Dx400.0 TO Dx600.0]":
            val.Range = string.Format("{0:C2} {1} {2:C2}",
                                        400, @Localization.to, 600);
            break;
        case "[Dx600.0 TO Dx800.0]":
            val.Range = string.Format("{0:C2} {1} {2:C2}",
                                        600, @Localization.to, 800);
            break;
        case "[Dx800.0 TO Dx1000000.0]":
            val.Range = string.Format("{0:C2} {1} {2:C2}",
                                        800, @Localization.to, 1000000);
            break;
    }
}

model.Values = facetResults[3];

2 个答案:

答案 0 :(得分:2)

根据@MattWarren suggestion,我最终使用了:

foreach (var val in facetResults[3].Value)
{
    // Original string format: [Dx5000.0 TO Dx10000.0]
    var limits = val.Range.Split(new string[] { "TO", "[", "]", " " },
                                 StringSplitOptions.RemoveEmptyEntries);

    // Leveraging RavenDB NumberUtil class...
    val.Range = string.Format("{0:C0} {1} {2:C0}",
    Raven.Abstractions.Indexing.NumberUtil.StringToNumber(limits.ElementAt(0)),
    @Localization.to,
    Raven.Abstractions.Indexing.NumberUtil.StringToNumber(limits.ElementAt(1)));      
}

答案 1 :(得分:1)

Leniel, 在您的代码中,创建一个字典,该字典将在构面值和显示字符串之间进行映射。 RavenDB目前无法影响方面的价值。