Skip to content

Instantly share code, notes, and snippets.

@Yegoroff
Created April 9, 2012 21:49
Show Gist options
  • Save Yegoroff/2346762 to your computer and use it in GitHub Desktop.
Save Yegoroff/2346762 to your computer and use it in GitHub Desktop.
Custom Property Mapping
var doc = new EsDocument(); // Some complex object that holds dictionary of runtime user-defined fields.
// doc.Fields = Dictionary<string, UserDefinedField>;
// where UserDefinedField.Name is equal to dictionary Key
// e.g. Fields["FName"] = new UserDefinedField{Name = "FName"};
// and UserDefinedField.Attributes is user-defined attributes that controls mapping.
// and UserDefinedField.ContainsAttribute(attribute) returns true if such attribute set for this field.
// Here we define custom field mapping for each user-defined field in doc object.
var properties = new List<CustomPropertyMap>();
foreach(var field in doc.Fields.Values)
{
var prop = new CustomPropertyMap(field.Name, field.Type) //Here ES type will be inferred from System.Type
.Store(field.ContainsAttribute(IndexStore.YES))
.When(field.ContainsAttribute(IndexType.NOT_ANALYZED), p => p.Index(Index.not_analyzed))
.When(field.ContainsAttribute(IndexType.NO), p => p.Index(Index.no));
properties.Add(prop);
}
// Build JSON map for EsDocument.
string map =
new MapBuilder<EsDocument>()
.RootObject("document_index", r => r
.Properties(p => p
.String(doc => doc.StringProperty, opt => opt.Analyzer(fulltext))
.CustomProperties( properties ) // Here we add custom properties mapping to whole object mapping.
)
).Build();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment