/Was.OrcSearch/Services/SearchQueryService.cs |
@@ -1,5 +1,7 @@ |
using System.Collections.Generic; |
using Lucene.Net.Analysis.Standard; |
using System; |
using System.Collections.Generic; |
using System.Linq; |
using Lucene.Net.Analysis; |
using Lucene.Net.Index; |
using Lucene.Net.QueryParsers; |
using Lucene.Net.Search; |
@@ -7,31 +9,63 @@ |
using Was.OrcSearch.Metadata; |
using Was.OrcSearch.Metadata.Interfaces; |
using Was.OrcSearch.Services.Interfaces; |
using Version = Lucene.Net.Util.Version; |
|
namespace Was.OrcSearch.Services |
{ |
public class SearchQueryService : ISearchQueryService |
public class SearchQueryService : ISearchQueryService, IDisposable |
{ |
public string GetSearchQuery(string filter, IEnumerable<ISearchableMetadata> searchableMetadatas) |
private readonly Analyzer _analyzer; |
private readonly Version _version = LuceneDefaults.Version; |
|
/// <summary> |
/// Default constructor using a default whitespace analyzer. |
/// </summary> |
public SearchQueryService() |
{ |
if (!filter.Contains(":")) |
{ |
filter = filter.PrepareOrcSearchFilter(); |
_analyzer = new WhitespaceAnalyzer(); |
} |
|
using (var analyzer = new StandardAnalyzer(LuceneDefaults.Version)) |
{ |
var fields = new List<string>(); |
foreach (var searchableMetadata in searchableMetadatas) fields.Add(searchableMetadata.SearchName); |
/// <summary> |
/// Instantiate an analyzer using a specific Lucene version. |
/// </summary> |
/// <param name="analyzer">The analyzer to use.</param> |
/// <param name="version">The lucene version.</param> |
public SearchQueryService(Analyzer analyzer, Version version) |
{ |
_version = version; |
} |
|
var parser = new MultiFieldQueryParser(LuceneDefaults.Version, fields.ToArray(), analyzer); |
var query = parser.Parse(filter); |
filter = query.ToString(); |
} |
} |
/// <summary> |
/// Instantiate a per-field analyzer. |
/// </summary> |
/// <param name="default">The default analyzer to use.</param> |
/// <param name="fieldAnalyzers">A collection of fields to analyzers.</param> |
public SearchQueryService(Analyzer @default, Dictionary<string, Analyzer> fieldAnalyzers) |
{ |
_analyzer = new PerFieldAnalyzerWrapper(@default, fieldAnalyzers); |
} |
|
return filter; |
public void Dispose() |
{ |
_analyzer?.Dispose(); |
} |
|
public string GetSearchQuery(string filter, IEnumerable<ISearchableMetadata> searchableMetadatas) |
{ |
if (filter.Contains(":")) |
return filter; |
|
filter = filter.PrepareOrcSearchFilter(); |
|
return new MultiFieldQueryParser( |
_version, |
searchableMetadatas |
.Select(searchableMetadata => searchableMetadata.SearchName).ToArray(), |
_analyzer) |
.Parse(filter).ToString(); |
} |
|
public string GetSearchQuery(params ISearchableMetadataValue[] searchableMetadataValues) |
{ |
var query = new PhraseQuery(); |
/Was.OrcSearch/Was.OrcSearch.csproj |
@@ -35,11 +35,11 @@ |
<RootNamespace>Was.OrcSearch</RootNamespace> |
</PropertyGroup> |
<ItemGroup> |
<Reference Include="ICSharpCode.SharpZipLib, Version=0.86.0.518, Culture=neutral, PublicKeyToken=1b03e6acf1164f73, processorArchitecture=MSIL"> |
<HintPath>..\packages\SharpZipLib.0.86.0\lib\20\ICSharpCode.SharpZipLib.dll</HintPath> |
<Reference Include="ICSharpCode.SharpZipLib, Version=0.86.0.518, Culture=neutral, PublicKeyToken=1b03e6acf1164f73"> |
<HintPath>..\..\packages\SharpZipLib.0.86.0\lib\20\ICSharpCode.SharpZipLib.dll</HintPath> |
</Reference> |
<Reference Include="Lucene.Net, Version=3.0.3.0, Culture=neutral, PublicKeyToken=85089178b9ac3181"> |
<HintPath>..\..\Corrade\packages\Lucene.Net.3.0.3\lib\NET40\Lucene.Net.dll</HintPath> |
<HintPath>..\..\packages\Lucene.Net.3.0.3\lib\NET40\Lucene.Net.dll</HintPath> |
</Reference> |
<Reference Include="System" /> |
<Reference Include="System.Core" /> |