mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-25 10:59:18 +08:00
Implementing user-configurable Lucene indexing analyzer selection, resolves #3887
This commit is contained in:
@@ -14,7 +14,6 @@ using Orchard.ContentManagement.MetaData.Builders;
|
||||
using Orchard.ContentManagement.Records;
|
||||
using Orchard.Core.Common.Handlers;
|
||||
using Orchard.Core.Common.Models;
|
||||
using Orchard.Data;
|
||||
using Orchard.Environment;
|
||||
using Orchard.Environment.Configuration;
|
||||
using Orchard.Environment.Extensions;
|
||||
@@ -69,6 +68,7 @@ namespace Orchard.Tests.Modules.Indexing {
|
||||
builder.RegisterType<IndexingTaskManager>().As<IIndexingTaskManager>();
|
||||
builder.RegisterType<DefaultContentManager>().As<IContentManager>();
|
||||
builder.RegisterType<StubCacheManager>().As<ICacheManager>();
|
||||
builder.RegisterType<StubWorkContextAccessor>().As<IWorkContextAccessor>();
|
||||
builder.RegisterType<Signals>().As<ISignals>();
|
||||
builder.RegisterType<DefaultContentManagerSession>().As<IContentManagerSession>();
|
||||
builder.RegisterInstance(_contentDefinitionManager.Object);
|
||||
|
||||
@@ -9,6 +9,7 @@ using Orchard.Environment.Configuration;
|
||||
using Orchard.FileSystems.AppData;
|
||||
using Orchard.Indexing;
|
||||
using Orchard.Tests.FileSystems.AppData;
|
||||
using Orchard.Tests.Stubs;
|
||||
|
||||
namespace Orchard.Tests.Modules.Indexing {
|
||||
public class LuceneIndexProviderTests {
|
||||
@@ -35,6 +36,7 @@ namespace Orchard.Tests.Modules.Indexing {
|
||||
_appDataFolder = AppDataFolderTests.CreateAppDataFolder(_basePath);
|
||||
|
||||
var builder = new ContainerBuilder();
|
||||
builder.RegisterType<StubWorkContextAccessor>().As<IWorkContextAccessor>();
|
||||
builder.RegisterType<DefaultLuceneAnalyzerProvider>().As<ILuceneAnalyzerProvider>();
|
||||
builder.RegisterType<DefaultLuceneAnalyzerSelector>().As<ILuceneAnalyzerSelector>();
|
||||
builder.RegisterType<LuceneIndexProvider>().As<IIndexProvider>();
|
||||
|
||||
@@ -8,6 +8,7 @@ using Orchard.Environment.Configuration;
|
||||
using Orchard.FileSystems.AppData;
|
||||
using Orchard.Indexing;
|
||||
using Orchard.Tests.FileSystems.AppData;
|
||||
using Orchard.Tests.Stubs;
|
||||
|
||||
namespace Orchard.Tests.Modules.Indexing {
|
||||
public class LuceneSearchBuilderTests {
|
||||
@@ -34,6 +35,7 @@ namespace Orchard.Tests.Modules.Indexing {
|
||||
_appDataFolder = AppDataFolderTests.CreateAppDataFolder(_basePath);
|
||||
|
||||
var builder = new ContainerBuilder();
|
||||
builder.RegisterType<StubWorkContextAccessor>().As<IWorkContextAccessor>();
|
||||
builder.RegisterType<DefaultLuceneAnalyzerProvider>().As<ILuceneAnalyzerProvider>();
|
||||
builder.RegisterType<DefaultLuceneAnalyzerSelector>().As<ILuceneAnalyzerSelector>();
|
||||
builder.RegisterType<LuceneIndexProvider>().As<IIndexProvider>();
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
using Lucene.Models;
|
||||
using Lucene.Services;
|
||||
using Lucene.ViewModels;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.Drivers;
|
||||
using Orchard.Indexing;
|
||||
using Orchard.Localization;
|
||||
using Orchard.UI.Notify;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web.Mvc;
|
||||
|
||||
namespace Lucene.Drivers {
|
||||
public class LuceneSettingsPartDriver : ContentPartDriver<LuceneSettingsPart> {
|
||||
private readonly IIndexManager _indexManager;
|
||||
private readonly IEnumerable<ILuceneAnalyzerSelector> _analyzerSelectors;
|
||||
private readonly INotifier _notifier;
|
||||
|
||||
public Localizer T { get; set; }
|
||||
|
||||
public LuceneSettingsPartDriver(IIndexManager indexManager, IEnumerable<ILuceneAnalyzerSelector> analyzerSelectors, INotifier notifier) {
|
||||
_indexManager = indexManager;
|
||||
_analyzerSelectors = analyzerSelectors;
|
||||
_notifier = notifier;
|
||||
T = NullLocalizer.Instance;
|
||||
}
|
||||
|
||||
protected override DriverResult Editor(LuceneSettingsPart part, dynamic shapeHelper) {
|
||||
return ContentShape("Parts_LuceneSettings_Edit", () => {
|
||||
MaintainMappings(part);
|
||||
return shapeHelper.EditorTemplate(
|
||||
TemplateName: "Parts.LuceneSettings",
|
||||
Model: new LuceneSettingsPartEditViewModel {
|
||||
LuceneAnalyzerSelectorMappings = part.LuceneAnalyzerSelectorMappings.ToArray(),
|
||||
LuceneAnalyzerSelectors = _analyzerSelectors.Select(analyzerSelector =>
|
||||
new SelectListItem { Text = T(analyzerSelector.Name).Text, Value = analyzerSelector.Name })
|
||||
},
|
||||
Prefix: Prefix);
|
||||
});
|
||||
}
|
||||
|
||||
protected override DriverResult Editor(LuceneSettingsPart part, IUpdateModel updater, dynamic shapeHelper) {
|
||||
var viewModel = new LuceneSettingsPartEditViewModel();
|
||||
if (updater.TryUpdateModel(viewModel, Prefix, null, null)) {
|
||||
_notifier.Warning(T("Don't forget to rebuild your index in case you have changed its analyzer."));
|
||||
part.LuceneAnalyzerSelectorMappings = viewModel.LuceneAnalyzerSelectorMappings;
|
||||
MaintainMappings(part);
|
||||
}
|
||||
|
||||
return Editor(part, shapeHelper);
|
||||
}
|
||||
|
||||
private void MaintainMappings(LuceneSettingsPart part) {
|
||||
var analyzerProviderNames = _analyzerSelectors.Select(analyzerProvider => analyzerProvider.Name);
|
||||
var indexNames = _indexManager.GetSearchIndexProvider().List();
|
||||
var maintainedMappings = part.LuceneAnalyzerSelectorMappings.ToList();
|
||||
// Removing mappings which contain a removed/invalid index or analyzer provider.
|
||||
maintainedMappings.RemoveAll(mapping => !indexNames.Contains(mapping.IndexName) || !analyzerProviderNames.Contains(mapping.AnalyzerName));
|
||||
// Adding new mappings for the new indexes.
|
||||
foreach (var indexName in indexNames) {
|
||||
if (!maintainedMappings.Any(mapping => mapping.IndexName == indexName)) {
|
||||
maintainedMappings.Add(new LuceneAnalyzerSelectorMapping { IndexName = indexName, AnalyzerName = "Default" });
|
||||
}
|
||||
}
|
||||
|
||||
part.LuceneAnalyzerSelectorMappings = maintainedMappings;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
using Lucene.Models;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.Handlers;
|
||||
using Orchard.Localization;
|
||||
using Orchard.Services;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Lucene.Handlers {
|
||||
public class LuceneSettingsPartHandler : ContentHandler {
|
||||
public Localizer T { get; set; }
|
||||
|
||||
public LuceneSettingsPartHandler(IJsonConverter jsonConverter) {
|
||||
T = NullLocalizer.Instance;
|
||||
|
||||
Filters.Add(new ActivatingFilter<LuceneSettingsPart>("Site"));
|
||||
|
||||
OnActivated<LuceneSettingsPart>((context, part) => {
|
||||
part.LuceneAnalyzerSelectorMappingsField.Loader(() => {
|
||||
return string.IsNullOrEmpty(part.LuceneAnalyzerSelectorMappingsSerialized)
|
||||
? new List<LuceneAnalyzerSelectorMapping>()
|
||||
: jsonConverter.Deserialize<List<LuceneAnalyzerSelectorMapping>>(part.LuceneAnalyzerSelectorMappingsSerialized);
|
||||
});
|
||||
|
||||
part.LuceneAnalyzerSelectorMappingsField.Setter((value) => {
|
||||
part.LuceneAnalyzerSelectorMappingsSerialized = value == null ? "[]" : jsonConverter.Serialize(value);
|
||||
return value;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
protected override void GetItemMetadata(GetContentItemMetadataContext context) {
|
||||
if (context.ContentItem.ContentType != "Site") return;
|
||||
|
||||
base.GetItemMetadata(context);
|
||||
|
||||
context.Metadata.EditorGroupInfo.Add(new GroupInfo(T("Lucene Settings")) { Id = "LuceneSettings" });
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -97,8 +97,12 @@
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Drivers\LuceneSettingsPartDriver.cs" />
|
||||
<Compile Include="Handlers\LuceneSettingsPartHandler.cs" />
|
||||
<Compile Include="Models\LuceneAnalyzerSelectorMapping.cs" />
|
||||
<Compile Include="Models\LuceneDocumentIndex.cs" />
|
||||
<Compile Include="Models\LuceneSearchHit.cs" />
|
||||
<Compile Include="Models\LuceneSettingsPart.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Services\DefaultLuceneAnalyzerSelector.cs" />
|
||||
<Compile Include="Services\DefaultLuceneAnalyzerProvider.cs" />
|
||||
@@ -108,6 +112,7 @@
|
||||
<Compile Include="Services\LuceneIndexProvider.cs" />
|
||||
<Compile Include="Services\LuceneSearchBuilder.cs" />
|
||||
<Compile Include="Services\SearchBits.cs" />
|
||||
<Compile Include="ViewModels\LuceneSettingsPartEditViewModel.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="Module.txt" />
|
||||
@@ -127,6 +132,13 @@
|
||||
<ItemGroup>
|
||||
<Content Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="Placement.info" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="Views\EditorTemplates\Parts.LuceneSettings.cshtml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup />
|
||||
<PropertyGroup>
|
||||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
|
||||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Lucene.Models {
|
||||
public class LuceneAnalyzerSelectorMapping {
|
||||
public string IndexName { get; set; }
|
||||
public string AnalyzerName { get; set; }
|
||||
}
|
||||
}
|
||||
21
src/Orchard.Web/Modules/Lucene/Models/LuceneSettingsPart.cs
Normal file
21
src/Orchard.Web/Modules/Lucene/Models/LuceneSettingsPart.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using Lucene.Models;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.Utilities;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Lucene.Models {
|
||||
public class LuceneSettingsPart : ContentPart {
|
||||
public string LuceneAnalyzerSelectorMappingsSerialized {
|
||||
get { return this.Retrieve(x => x.LuceneAnalyzerSelectorMappingsSerialized); }
|
||||
set { this.Store(x => x.LuceneAnalyzerSelectorMappingsSerialized, value); }
|
||||
}
|
||||
|
||||
private readonly LazyField<IEnumerable<LuceneAnalyzerSelectorMapping>> _luceneAnalyzerSelectorMappings = new LazyField<IEnumerable<LuceneAnalyzerSelectorMapping>>();
|
||||
internal LazyField<IEnumerable<LuceneAnalyzerSelectorMapping>> LuceneAnalyzerSelectorMappingsField { get { return _luceneAnalyzerSelectorMappings; }
|
||||
}
|
||||
public IEnumerable<LuceneAnalyzerSelectorMapping> LuceneAnalyzerSelectorMappings {
|
||||
get { return _luceneAnalyzerSelectorMappings.Value; }
|
||||
set { _luceneAnalyzerSelectorMappings.Value = value; }
|
||||
}
|
||||
}
|
||||
}
|
||||
5
src/Orchard.Web/Modules/Lucene/Placement.info
Normal file
5
src/Orchard.Web/Modules/Lucene/Placement.info
Normal file
@@ -0,0 +1,5 @@
|
||||
<Placement>
|
||||
<Match ContentType="Site">
|
||||
<Place Parts_LuceneSettings_Edit="Content: 1@LuceneSettings" />
|
||||
</Match>
|
||||
</Placement>
|
||||
@@ -1,34 +1,46 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Lucene.Models;
|
||||
using Lucene.Net.Analysis;
|
||||
using Lucene.Net.Analysis.Standard;
|
||||
using Orchard;
|
||||
using Orchard.ContentManagement;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Lucene.Services {
|
||||
public class DefaultLuceneAnalyzerProvider : ILuceneAnalyzerProvider {
|
||||
|
||||
private readonly IWorkContextAccessor _wca;
|
||||
private IEnumerable<ILuceneAnalyzerSelector> _analyzerSelectors;
|
||||
|
||||
public DefaultLuceneAnalyzerProvider(IEnumerable<ILuceneAnalyzerSelector> analyzerSelectors) {
|
||||
public DefaultLuceneAnalyzerProvider(IEnumerable<ILuceneAnalyzerSelector> analyzerSelectors, IWorkContextAccessor wca) {
|
||||
_analyzerSelectors = analyzerSelectors;
|
||||
_wca = wca;
|
||||
}
|
||||
|
||||
public Analyzer GetAnalyzer(string indexName) {
|
||||
var luceneSettingsPart = _wca
|
||||
.GetContext()
|
||||
.CurrentSite
|
||||
.As<LuceneSettingsPart>();
|
||||
if (luceneSettingsPart == null) {
|
||||
return new StandardAnalyzer(LuceneIndexProvider.LuceneVersion);
|
||||
}
|
||||
|
||||
var currentIndexMapping = luceneSettingsPart
|
||||
.LuceneAnalyzerSelectorMappings
|
||||
.FirstOrDefault(mapping => mapping.IndexName == indexName);
|
||||
if (currentIndexMapping == null) {
|
||||
return new StandardAnalyzer(LuceneIndexProvider.LuceneVersion);
|
||||
}
|
||||
|
||||
var analyzer = _analyzerSelectors
|
||||
.Where(x => x.Name == currentIndexMapping.AnalyzerName)
|
||||
.Select(x => x.GetLuceneAnalyzer(indexName))
|
||||
.Where(x => x != null)
|
||||
.OrderByDescending(x => x.Priority)
|
||||
.Select(x => x.Analyzer)
|
||||
.FirstOrDefault();
|
||||
|
||||
if (analyzer != null) {
|
||||
return analyzer;
|
||||
}
|
||||
|
||||
return new StandardAnalyzer(LuceneIndexProvider.LuceneVersion);
|
||||
return analyzer != null ? analyzer : new StandardAnalyzer(LuceneIndexProvider.LuceneVersion);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,5 +12,7 @@ namespace Lucene.Services {
|
||||
Analyzer = new StandardAnalyzer(LuceneIndexProvider.LuceneVersion)
|
||||
};
|
||||
}
|
||||
|
||||
public string Name { get { return "Default"; } }
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using Orchard;
|
||||
using Orchard;
|
||||
|
||||
namespace Lucene.Services {
|
||||
public interface ILuceneAnalyzerSelector : IDependency {
|
||||
LuceneAnalyzerSelectorResult GetLuceneAnalyzer(string indexName);
|
||||
string Name { get; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using Lucene.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
|
||||
namespace Lucene.ViewModels {
|
||||
public class LuceneSettingsPartEditViewModel {
|
||||
public LuceneAnalyzerSelectorMapping[] LuceneAnalyzerSelectorMappings { get; set; }
|
||||
|
||||
public IEnumerable<SelectListItem> LuceneAnalyzerSelectors { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
@model Lucene.ViewModels.LuceneSettingsPartEditViewModel
|
||||
|
||||
@using System.Linq
|
||||
|
||||
<fieldset>
|
||||
<legend>@T("Lucene Settings")</legend>
|
||||
@if (!Model.LuceneAnalyzerSelectorMappings.Any()) {
|
||||
@T("There is currently no mapping. Create an index first.")
|
||||
}
|
||||
else {
|
||||
<ul>
|
||||
@for (int i = 0; i < Model.LuceneAnalyzerSelectorMappings.Length; i++) {
|
||||
<li>
|
||||
@Html.HiddenFor(m => Model.LuceneAnalyzerSelectorMappings[i].IndexName)
|
||||
@Html.LabelFor(m => Model.LuceneAnalyzerSelectorMappings[i].AnalyzerName, T("Analyzer name for the \"{0}\" index", Model.LuceneAnalyzerSelectorMappings[i].IndexName))
|
||||
@Html.DropDownListFor(m => Model.LuceneAnalyzerSelectorMappings[i].AnalyzerName, new SelectList(
|
||||
Model.LuceneAnalyzerSelectors,
|
||||
"Text",
|
||||
"Value",
|
||||
Model.LuceneAnalyzerSelectorMappings[i].AnalyzerName
|
||||
))
|
||||
</li>
|
||||
}
|
||||
</ul>
|
||||
}
|
||||
</fieldset>
|
||||
Reference in New Issue
Block a user