Created
August 7, 2014 14:04
-
-
Save rbaty-barr/a6b4f30d54a613e7591a to your computer and use it in GitHub Desktop.
index archetype content
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Globalization; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Web; | |
using Examine; | |
using Umbraco.Core; | |
using System.Collections.Generic; | |
public class Bootstrapper : ApplicationEventHandler | |
{ | |
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) | |
{ | |
base.ApplicationStarted(umbracoApplication, applicationContext); | |
ExamineManager.Instance.IndexProviderCollection["ExternalIndexer"] | |
.GatheringNodeData += (sender, e) => | |
{ | |
// Extract JSON properties | |
var fieldKeys = e.Fields.Keys.ToArray(); | |
foreach (var key in fieldKeys) | |
{ | |
var value = e.Fields[key]; | |
if (value.DetectIsJson()) | |
{ | |
IndexNestedObject(e.Fields, JsonConvert.DeserializeObject(value), key); | |
} | |
} | |
}; | |
} | |
private void IndexNestedObject(Dictionary<string, string> fields, object obj, string prefix) | |
{ | |
var objType = obj.GetType(); | |
if (objType == typeof (JObject)) | |
{ | |
var jObj = obj as JObject; | |
if (jObj != null) | |
{ | |
foreach (var kvp in jObj) | |
{ | |
var propKey = prefix + "_" + kvp.Key; | |
var valueType = kvp.Value.GetType(); | |
if (typeof (JContainer).IsAssignableFrom(valueType)) | |
{ | |
IndexNestedObject(fields, kvp.Value, propKey); | |
} | |
else | |
{ | |
fields.Add(propKey, kvp.Value.ToString().StripHtml()); | |
} | |
} | |
} | |
} | |
else if (objType == typeof (JArray)) | |
{ | |
var jArr = obj as JArray; | |
if (jArr != null) | |
{ | |
for (var i = 0; i < jArr.Count; i++) | |
{ | |
var itm = jArr[i]; | |
var propKey = prefix + "_" + i; | |
var valueType = itm.GetType(); | |
if (typeof(JContainer).IsAssignableFrom(valueType)) | |
{ | |
IndexNestedObject(fields, itm, propKey); | |
} | |
else | |
{ | |
fields.Add(propKey, itm.ToString().StripHtml()); | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi... so what does this do?... it creates new fields in the index for all properties?