Was.OrcSearch

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 4  →  ?path2? @ 3
/Was.OrcSearch/Helpers/Extensions.cs
@@ -0,0 +1,40 @@
using System;
using System.Collections;
using System.Collections.Generic;
 
namespace Was.OrcSearch.Helpers
{
public static class Extensions
{
/// <summary>
/// Convert an object to an enumeration of strings.
/// </summary>
/// <param name="instance">the object to convert</param>
/// <returns>a series of strings</returns>
public static IEnumerable<string> Stringify(this object instance)
{
if (instance == null)
yield break;
 
// Base type will be string.
if (instance is string)
{
yield return (string) instance;
yield break;
}
 
// Support primitive types.
if (instance.GetType().IsPrimitive)
{
yield return instance.ToString();
yield break;
}
 
// Support for arrays.
if (instance is Array || instance is IList)
foreach (var element in instance as IList)
foreach (var item in Stringify(element))
yield return item;
}
}
}
/Was.OrcSearch/Metadata/AttributeMetadataCollection.cs
@@ -1,10 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Threading;
using Was.OrcSearch.Attributes;
using Was.OrcSearch.Models;
using Was.OrcSearch.Models.Interfaces;
@@ -13,7 +9,7 @@
{
public class AttributeMetadataCollection : ReflectionMetadataCollection
{
private static readonly Dictionary<Type, List<SearchableMetadata>> PropertiesCache =
private static readonly Dictionary<Type, List<SearchableMetadata>> _propertiesCache =
new Dictionary<Type, List<SearchableMetadata>>();
 
private readonly Type _targetType;
@@ -28,26 +24,30 @@
{
get
{
if (PropertiesCache.TryGetValue(_targetType, out var storedProperty))
if (_propertiesCache.TryGetValue(_targetType, out var storedProperty))
return storedProperty;
 
var searchableProperties = new List<SearchableMetadata>();
 
var properties = _targetType.GetProperties();
foreach (var property in properties)
{
if (!(property.GetCustomAttribute(typeof(SearchablePropertyAttribute), false) is
SearchablePropertyAttribute searchablePropertyAttribute)) continue;
var searchableProperty = new SearchableMetadata(property);
if (!string.IsNullOrWhiteSpace(searchablePropertyAttribute.SearchName))
searchableProperty.SearchName = searchablePropertyAttribute.SearchName;
var searchablePropertyAttribute =
property.GetCustomAttribute(typeof(SearchablePropertyAttribute), false) as
SearchablePropertyAttribute;
if (searchablePropertyAttribute != null)
{
var searchableProperty = new SearchableMetadata(property);
if (!string.IsNullOrWhiteSpace(searchablePropertyAttribute.SearchName))
searchableProperty.SearchName = searchablePropertyAttribute.SearchName;
 
searchableProperty.Analyze = searchablePropertyAttribute.Analyze;
searchableProperty.Analyze = searchablePropertyAttribute.Analyze;
 
searchableProperties.Add(searchableProperty);
searchableProperties.Add(searchableProperty);
}
}
 
PropertiesCache.Add(_targetType, searchableProperties);
_propertiesCache.Add(_targetType, searchableProperties);
return searchableProperties;
}
}
/Was.OrcSearch/Services/Extensions/ISearchServiceExtensions.cs
@@ -0,0 +1,6 @@
namespace Was.OrcSearch.Services.Extensions
{
public static class ISearchServiceExtensions
{
}
}
/Was.OrcSearch/Services/SearchServiceBase.cs
@@ -9,8 +9,8 @@
using Lucene.Net.Store;
using Was.OrcSearch.EventArgs;
using Was.OrcSearch.Extensions;
using Was.OrcSearch.Helpers;
using Was.OrcSearch.Metadata.Interfaces;
using Was.OrcSearch.Services.Extensions;
using Was.OrcSearch.Services.Interfaces;
 
namespace Was.OrcSearch.Services
@@ -118,10 +118,8 @@
foreach (var searchableMetadata in searchableMetadatas)
{
var searchableMetadataValue = searchableMetadata.GetValue(searchable.Instance);
 
// DEBUG
//Console.WriteLine("Stringifying: " + searchableMetadataValue);
// Original: ObjectToStringHelper.ToString(searchableMetadataValue);
// TODO Support more serializable types.
var searchableMetadataValueAsString =
string.Join(" ", searchableMetadataValue.Stringify());
@@ -131,7 +129,7 @@
var field = new Field(searchableMetadata.SearchName, searchableMetadataValueAsString,
Field.Store.YES,
searchableMetadata.Analyze ? Field.Index.ANALYZED : Field.Index.NOT_ANALYZED,
Field.TermVector.NO);
Field.TermVector.YES);
 
document.Add(field);
 
@@ -166,7 +164,8 @@
{
foreach (var searchable in searchables)
{
if (!_searchableIndexes.TryGetValue(searchable, out var index)) continue;
int index;
if (!_searchableIndexes.TryGetValue(searchable, out index)) continue;
 
var queryAsText = $"{IndexId}:{index}";
var parser = new QueryParser(LuceneDefaults.Version, string.Empty, analyzer);
@@ -278,15 +277,15 @@
}
}
}
catch (ParseException)
catch (ParseException ex)
{
//Log.Warning(ex, "Failed to parse search pattern");
throw;
throw ex;
}
catch (Exception)
catch (Exception ex)
{
//Log.Error(ex, "An error occurred while searching, returning default results");
throw;
throw ex;
}
finally
{
/Was.OrcSearch/Was.OrcSearch.csproj
@@ -50,6 +50,7 @@
<Compile Include="EventArgs\SearchEventArgs.cs" />
<Compile Include="Extensions\StringExtensions.cs" />
<Compile Include="Helpers\PathHelper.cs" />
<Compile Include="Helpers\Extensions.cs" />
<Compile Include="Metadata\AttributeMetadataCollection.cs" />
<Compile Include="Metadata\Interfaces\IMetadataProvider.cs" />
<Compile Include="Metadata\Interfaces\ISearchable.cs" />
@@ -77,7 +78,7 @@
<Compile Include="Models\ReflectionObjectWithMetadata.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Services\DummySearchNavigationService.cs" />
<Compile Include="Services\Extensions\SearchServiceExtensions.cs" />
<Compile Include="Services\Extensions\ISearchServiceExtensions.cs" />
<Compile Include="Services\InMemorySearchService.cs" />
<Compile Include="Services\Interfaces\ISearchNavigationService.cs" />
<Compile Include="Services\Interfaces\ISearchQueryService.cs" />