Hi Evaldis
I've found the 8 code that loads the facet definitions from XML files - see below. That might help you.
public static IFacetGroup ConvertToFacets(JObject obj)
{
IFacetGroup facets;
string type = (string)obj["Type"];
if (!string.IsNullOrWhiteSpace(type))
{
Type indexType = Type.GetType(type);
facets = Activator.CreateInstance(indexType) as IFacetGroup;
}
else
{
facets = new FacetGroup();
}
if (facets != null)
{
// Name
string facetsName = (obj["Name"] != null) ? (string)obj["Name"] : null;
facets.Name = facetsName;
// Filename
facets.FileName = (obj["FileName"] != null) ? (string)obj["FileName"] : null; ;
// Meta
var meta = new Dictionary<string, string>();
JObject jMeta = (JObject)obj["Meta"];
if (jMeta != null)
{
foreach (var prop in jMeta.Properties())
{
meta.Add(prop.Name, (string)prop.Value);
}
}
facets.Meta = meta;
// Settings
var settings = new Dictionary<string, string>();
JObject jSettings = (JObject)obj["Settings"];
if (jSettings != null)
{
foreach (var prop in jSettings.Properties())
{
settings.Add(prop.Name, (string)prop.Value);
}
}
facets.Settings = settings;
// Source
JObject jSource = (JObject)obj["Source"];
FacetSource facetSource = new FacetSource();
if (jSource != null)
{
facetSource.Repository = (string)jSource["Repository"];
facetSource.Item = (string)jSource["Item"];
}
facets.Source = facetSource;
// Facets
var items = new List<Facet>();
JArray jItems = (JArray)obj["Items"];
if (jItems != null)
{
foreach (var jFacet in jItems)
{
string facetName = (string)jFacet["Name"];
string facetQueryParameter = (string)jFacet["QueryParameter"];
string facetTypeName = (string)jFacet["Type"];
string facetField = (string)jFacet["Field"];
Facet facet = new Facet() { Name = facetName, QueryParameter = facetQueryParameter, Type = facetTypeName, Field = facetField };
var facetOptions = new List<FacetOption>();
JArray jFacetOptions = (JArray)jFacet["Options"];
if (jFacetOptions != null)
{
foreach (var jFacetOption in jFacetOptions)
{
string facetOptionName = (string)jFacetOption["Name"];
string facetOptionTitle = (string)jFacetOption["Title"];
string facetOptionValue = (string)jFacetOption["Value"];
FacetOption facetOption = new FacetOption() { Name = facetOptionName, Value = facetOptionValue };
facetOptions.Add(facetOption);
}
}
facet.Options = facetOptions;
items.Add(facet);
}
}
facets.Items = items;
}
return facets;
}
public static IFacetGroup ConvertToFacets(XElement obj)
{
XElement root = obj;
if (root.Name.LocalName.Equals("Facets"))
{
IFacetGroup facets;
if (root.Attribute("Type") != null)
{
Type indexType = Type.GetType(root.Attribute("Type").Value);
facets = Activator.CreateInstance(indexType) as IFacetGroup;
}
else
{
facets = new FacetGroup();
}
if (facets != null)
{
// Name
string facetsName = (root.Attribute("Name") != null) ? root.Attribute("Name").Value : null;
facets.Name = facetsName;
// Filename
facets.FileName = null;
// Meta
var meta = new Dictionary<string, string>();
var metaElements = root.Elements("Meta");
if (metaElements != null)
{
foreach (var el in metaElements)
{
if (el.Attribute("Name") != null && el.Attribute("Value") != null)
{
meta.Add(el.Attribute("Name").Value, el.Attribute("Value").Value);
}
}
}
facets.Meta = meta;
// Settings
var settingsElement = root.Element("Settings");
var settings = new Dictionary<string, string>();
if (settingsElement != null)
{
foreach (var el in settingsElement.Elements())
{
settings.Add(el.Name.LocalName, el.Value);
}
}
facets.Settings = settings;
// Source
var sourceElement = root.Element("Source");
FacetSource facetSource = new FacetSource();
if (sourceElement != null)
{
facetSource.Repository = (sourceElement.Attribute("Repository") != null) ? sourceElement.Attribute("Repository").Value : null; ;
facetSource.Item = (sourceElement.Attribute("Item") != null) ? sourceElement.Attribute("Item").Value : null; ;
}
facets.Source = facetSource;
// Facets
var items = new List<Facet>();
var facetElements = root.Elements("Facet");
foreach (var facetElement in facetElements)
{
string facetName = (facetElement.Attribute("Name") != null) ? facetElement.Attribute("Name").Value : null;
string facetQueryParameter = (facetElement.Attribute("QueryParameter") != null) ? facetElement.Attribute("QueryParameter").Value : null;
string facetType = (facetElement.Attribute("Type") != null) ? facetElement.Attribute("Type").Value : null;
string facetField = (facetElement.Attribute("Field") != null) ? facetElement.Attribute("Field").Value : null;
Facet facet = new Facet() { Name = facetName, QueryParameter = facetQueryParameter, Type = facetType, Field = facetField };
var facetOptions = new List<FacetOption>();
var facetOptionElements = facetElement.Elements("Option");
if (facetOptionElements != null)
{
foreach (var facetOptionElement in facetOptionElements)
{
string facetOptionName = (facetOptionElement.Attribute("Name") != null) ? facetOptionElement.Attribute("Name").Value : null;
string facetOptionTitle = (facetOptionElement.Attribute("Title") != null) ? facetOptionElement.Attribute("Title").Value : null;
string facetOptionValue = (facetOptionElement.Attribute("Value") != null) ? facetOptionElement.Attribute("Value").Value : null;
FacetOption facetOption = new FacetOption() { Name = facetOptionName, Value = facetOptionValue };
facetOptions.Add(facetOption);
}
}
facet.Options = facetOptions;
items.Add(facet);
}
facets.Items = items;
return facets;
}
else
{
if (root.Attribute("Type") != null)
throw new Exception(string.Format("Cannot instantiate Facets of type '{0}'", root.Attribute("Type").Value));
else
throw new Exception(string.Format("Cannot instantiate Facets"));
}
}
else
{
throw new Exception(string.Format("Root is an not a 'Facets' element"));
}
}