2011-02-26 04:29:21 +00:00
|
|
|
|
using System.Collections.Specialized;
|
|
|
|
|
using System.Configuration;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using Lucene.Net.Analysis;
|
|
|
|
|
using Lucene.Net.Analysis.Standard;
|
|
|
|
|
using Lucene.Net.Index;
|
|
|
|
|
using Lucene.Net.QueryParsers;
|
|
|
|
|
using Lucene.Net.Store;
|
|
|
|
|
using Microsoft.Practices.Unity;
|
|
|
|
|
using Microsoft.Practices.Unity.InterceptionExtension;
|
|
|
|
|
using Portoa.Lucene;
|
|
|
|
|
using Portoa.Persistence;
|
|
|
|
|
using Portoa.Search;
|
|
|
|
|
using Portoa.Web.Unity;
|
|
|
|
|
using Portoa.Web.Unity.Lifetime;
|
|
|
|
|
using VideoGameQuotes.Api;
|
|
|
|
|
using VideoGameQuotes.Web.Search;
|
|
|
|
|
using Directory = Lucene.Net.Store.Directory;
|
|
|
|
|
using Version = Lucene.Net.Util.Version;
|
|
|
|
|
|
|
|
|
|
namespace VideoGameQuotes.Web.Configuration {
|
|
|
|
|
public class EnableSearchWithLucene : UnityContainerExtension {
|
|
|
|
|
protected override void Initialize() {
|
|
|
|
|
var indexWriterLifetimeManager = new ExplicitlyDisposableLifetimeManager<IndexWriter>(
|
|
|
|
|
new ContainerControlledLifetimeManager(),
|
|
|
|
|
indexWriter => indexWriter.Close()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
Container
|
|
|
|
|
.RegisterType<Directory>(new ContainerControlledLifetimeManager(), new InjectionFactory(CreateIndexDirectory))
|
|
|
|
|
.RegisterType<IndexWriter>(indexWriterLifetimeManager, new InjectionFactory(CreateIndexWriter))
|
|
|
|
|
.RegisterInstance(Version.LUCENE_29)
|
|
|
|
|
.RegisterType<Analyzer, StandardAnalyzer>(new InjectionConstructor(typeof(Version)))
|
|
|
|
|
.RegisterType<QueryParser>(new InjectionFactory(CreateQueryParser))
|
2011-03-02 20:18:33 +00:00
|
|
|
|
.RegisterAndIntercept(typeof(ISearcher<,>), typeof(LuceneEntitySearcher<,>))
|
|
|
|
|
.RegisterAndIntercept(typeof(ISearchService<,>), typeof(SearchService<,>))
|
2011-02-26 04:29:21 +00:00
|
|
|
|
.RegisterAndIntercept<ILuceneDocumentHandler<Quote>, QuoteDocumentHandler>()
|
2011-03-02 20:18:33 +00:00
|
|
|
|
.RegisterAndIntercept(typeof(ISearchIndexBuilder<,>), typeof(LuceneEntityIndexBuilder<,>));
|
2011-02-26 04:29:21 +00:00
|
|
|
|
|
|
|
|
|
Container
|
|
|
|
|
.Configure<Interception>()
|
|
|
|
|
.AddPolicy("UpdateSearchIndexPolicy")
|
|
|
|
|
.AddCallHandler<UpdateSearchIndexCallHandler>()
|
2011-03-01 00:11:17 +00:00
|
|
|
|
.AddMatchingRule(new QuoteUpdatedMatchingRule());
|
2011-03-04 08:20:20 +00:00
|
|
|
|
|
|
|
|
|
Container
|
|
|
|
|
.Configure<Interception>()
|
|
|
|
|
.AddPolicy("DeleteSearchIndexPolicy")
|
|
|
|
|
.AddCallHandler<DeleteSearchIndexCallHandler>()
|
|
|
|
|
.AddMatchingRule(new QuoteDeletedMatchingRule());
|
2011-02-26 04:29:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region lucene-related factories
|
|
|
|
|
private static QueryParser CreateQueryParser(IUnityContainer container) {
|
|
|
|
|
return new QueryParser(container.Resolve<Version>(), "text", container.Resolve<Analyzer>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Directory CreateIndexDirectory(IUnityContainer container) {
|
|
|
|
|
var indexDirectory = ((NameValueCollection)ConfigurationManager.GetSection("vgquotes"))["luceneIndexDirectory"];
|
|
|
|
|
return new SimpleFSDirectory(new DirectoryInfo(indexDirectory));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static IndexWriter CreateIndexWriter(IUnityContainer container) {
|
2011-02-28 22:30:03 +00:00
|
|
|
|
return new IndexWriter(container.Resolve<Directory>(), container.Resolve<Analyzer>(), true, IndexWriter.MaxFieldLength.UNLIMITED);
|
2011-02-26 04:29:21 +00:00
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
2011-03-04 08:20:20 +00:00
|
|
|
|
#region matching rules
|
2011-03-01 00:11:17 +00:00
|
|
|
|
private class QuoteUpdatedMatchingRule : IMatchingRule {
|
2011-02-26 04:29:21 +00:00
|
|
|
|
private static readonly MethodBase saveMethod = typeof(IRepository<Quote, int>).GetMethod("Save", new[] { typeof(Quote) });
|
|
|
|
|
|
|
|
|
|
public bool Matches(MethodBase member) {
|
|
|
|
|
return member == saveMethod;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-03-04 08:20:20 +00:00
|
|
|
|
|
|
|
|
|
private class QuoteDeletedMatchingRule : IMatchingRule {
|
|
|
|
|
private static readonly MethodBase deleteMethod = typeof(IRepository<Quote, int>).GetMethod("Delete", new[] { typeof(int) });
|
|
|
|
|
|
|
|
|
|
public bool Matches(MethodBase member) {
|
|
|
|
|
return member == deleteMethod;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
2011-02-26 04:29:21 +00:00
|
|
|
|
}
|
|
|
|
|
}
|