RavendDB faceted search results formatting
-
11-11-2019 - |
Question
Currently I'm playing with faceted search after reading RavenDB doc about it.
The result returned is OK, but there's a small problem. Since the result comes as
IDictionary<string, IEnumerable<FacetValue>>
it's necessary to iterate over it and do some fancy string manipulation to format the result and show it in a PartialView. More specifically this facet:
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
code:
@fv.Range
This is this "beautiful" string that gets output on the view: [Dx400.0 TO Dx600.0]
RavenDB uses the Dx prefix above to do a number to string conversion.
Controller
code where the facet result is passed to a specific 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);
Is there any other/better way of getting results from a faceted search so that no string manipulation must be done to format the returned data?
After Ayende's suggestion, I did this in my controller:
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];
No correct solution