Skip to content

Instantly share code, notes, and snippets.

@BartDM
Created January 13, 2014 15:54
Show Gist options
  • Save BartDM/8402642 to your computer and use it in GitHub Desktop.
Save BartDM/8402642 to your computer and use it in GitHub Desktop.
protected SearchResult Search<T>(string field, string searchQuery) where T : IDocument
{
Log.DebugFormat("Searching for Type: {0} with query \"{1}\" for field \"{2}\"", typeof(T), searchQuery, field);
//Fetch the possible fields to search on
PropertyInfo[] properties = typeof(T).GetProperties();
var fields = new List<string>();
var fieldsToSearchOn = new List<string>();
foreach (PropertyInfo property in properties)
{
var attributes = property.GetCustomAttributes(true);
foreach (var o in attributes)
{
var attr = o as SearchField;
if (attr != null)
{
fields.Add(property.Name);
if (attr.CombinedSearchFields.Any() && field == property.Name)
{
fieldsToSearchOn.Add(property.Name);
for (int i = 0; i < attr.CombinedSearchFields.Count(); i++)
{
fieldsToSearchOn.Add(attr.CombinedSearchFields[i]);
}
}
else if (field == property.Name)
{
fieldsToSearchOn.Add(property.Name);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment