private Document BuildDocumentFromSearchContent(T searchContent) { Document doc = new Document(); IList <SearchContentFieldInfo> fields = SearchGenericUtils.GetSearchContentFields(typeof(T), searchContent); for (int i = 0; i < fields.Count; i++) { SearchContentFieldInfo fi = fields[i]; switch (fi.FieldType) { case SearchContentFieldType.Text: doc.Add(Field.Text(fi.Name, fi.Value)); break; case SearchContentFieldType.UnStored: doc.Add(Field.UnStored(fi.Name, fi.Value)); break; case SearchContentFieldType.UnIndexed: doc.Add(Field.UnIndexed(fi.Name, fi.Value)); break; case SearchContentFieldType.Keyword: doc.Add(Field.Keyword(fi.Name, fi.Value)); break; default: break; } } return(doc); }
private void AddTypeToCache(Type t) { if (_cache.ContainsKey(t)) { return; } List <SearchContentFieldInfo> fields = new List <SearchContentFieldInfo>(); PropertyInfo[] props = t.GetProperties(BindingFlags.Public | BindingFlags.Instance); for (int i = 0; i < props.Length; i++) { PropertyInfo pi = props[i]; SearchContentFieldAttribute[] atts = (SearchContentFieldAttribute[])pi.GetCustomAttributes(typeof(SearchContentFieldAttribute), true); if (atts.Length > 0) { SearchContentFieldInfo fi = new SearchContentFieldInfo(); fi.Name = pi.Name; fi.FieldType = atts[0].FieldType; fi.IsKeyField = atts[0].IsKeyField; fi.IsResultField = atts[0].IsResultField; fi.IsQueryField = atts[0].IsQueryField; fi.PropertyInfo = pi; fields.Add(fi); } } if (fields.Count > 0) { _cache.Add(t, fields); } }
public IList <SearchContentFieldInfo> GetFields(Type t, object instance) { if (!_cache.ContainsKey(t)) { AddTypeToCache(t); } List <SearchContentFieldInfo> result = new List <SearchContentFieldInfo>(_cache[t].ToArray()); if (instance == null) { return(result); } GenericGetter getMethod; for (int i = 0; i < result.Count; i++) { SearchContentFieldInfo fi = result[i]; getMethod = CreateGetMethod(fi.PropertyInfo); if (getMethod == null) { throw new Exception(String.Format("Property \"{0}\" does not have getter!", fi.PropertyInfo.Name)); } object val = getMethod(instance); fi.Value = val == null ? String.Empty : val.ToString(); } return(result); }
public void SetSearchResultField(string fieldName, object instance, object value) { Type t = instance.GetType(); if (!_cache.ContainsKey(t)) { AddTypeToCache(t); } SearchContentFieldInfo field = _cache[t].Find( delegate(SearchContentFieldInfo fi) { return(fi.Name == fieldName); }); if (field.Name != fieldName) { throw new Exception(String.Format("Field with name \"{0}\" not found on type \"{1}\"!", fieldName, t)); } GenericSetter setter = CreateSetMethod(field.PropertyInfo); if (setter == null) { throw new Exception(String.Format("Property \"{0}\" does not have setter!", field.PropertyInfo.Name)); } setter(instance, Convert.ChangeType(value, field.PropertyInfo.PropertyType)); }
/// <summary> /// Delete existing content from the search index. /// </summary> /// <param name="searchContent"></param> public void DeleteContent(T searchContent) { if (this._rebuildIndex) { throw new InvalidOperationException("Cannot delete documents when rebuilding the index."); } else { this._indexWriter.Close(); this._indexWriter = null; // The SearchContentKey uniquely identifies a document in the index. SearchContentFieldInfo ki = SearchGenericUtils.GetSearchContentKeyFieldInfo(typeof(T), searchContent); if (String.IsNullOrEmpty(ki.Name)) { throw new Exception("SearchContentKey Field not specified on target class!"); } Term term = new Term(ki.Name, ki.Value); IndexReader rdr = IndexReader.Open(this._indexDirectory); rdr.Delete(term); rdr.Close(); } }