From c8b3f11498506f5072c71df96e7b820bf93eddf4 Mon Sep 17 00:00:00 2001 From: Shannon Date: Thu, 13 Dec 2018 15:04:18 +1100 Subject: [PATCH] latest examine, tweaks to UmbracoFieldDefinitionCollection, adds some test code for testing --- .../{IUmbracoIndexer.cs => IUmbracoIndex.cs} | 2 +- src/Umbraco.Examine/Umbraco.Examine.csproj | 4 +- src/Umbraco.Examine/UmbracoContentIndex.cs | 62 ++++++++++++------- src/Umbraco.Examine/UmbracoExamineIndex.cs | 4 +- src/Umbraco.Examine/UmbracoMemberIndex.cs | 4 +- src/Umbraco.Tests/Umbraco.Tests.csproj | 2 +- .../UmbracoExamine/IndexInitializer.cs | 2 +- src/Umbraco.Web.UI/Umbraco.Web.UI.csproj | 2 +- .../Editors/ExamineManagementController.cs | 2 +- src/Umbraco.Web/PublishedContentQuery.cs | 4 +- src/Umbraco.Web/Search/ExamineComponent.cs | 22 +++++-- .../Search/GenericIndexDiagnostics.cs | 2 +- .../Search/UmbracoIndexesCreator.cs | 9 +-- src/Umbraco.Web/Umbraco.Web.csproj | 2 +- 14 files changed, 74 insertions(+), 49 deletions(-) rename src/Umbraco.Examine/{IUmbracoIndexer.cs => IUmbracoIndex.cs} (94%) diff --git a/src/Umbraco.Examine/IUmbracoIndexer.cs b/src/Umbraco.Examine/IUmbracoIndex.cs similarity index 94% rename from src/Umbraco.Examine/IUmbracoIndexer.cs rename to src/Umbraco.Examine/IUmbracoIndex.cs index 00f772b1e2..e70652a342 100644 --- a/src/Umbraco.Examine/IUmbracoIndexer.cs +++ b/src/Umbraco.Examine/IUmbracoIndex.cs @@ -5,7 +5,7 @@ namespace Umbraco.Examine /// /// A Marker interface for defining an Umbraco indexer /// - public interface IUmbracoIndexer : IIndex + public interface IUmbracoIndex : IIndex { /// /// When set to true Umbraco will keep the index in sync with Umbraco data automatically diff --git a/src/Umbraco.Examine/Umbraco.Examine.csproj b/src/Umbraco.Examine/Umbraco.Examine.csproj index 66b1f09068..3c2f5a8ea6 100644 --- a/src/Umbraco.Examine/Umbraco.Examine.csproj +++ b/src/Umbraco.Examine/Umbraco.Examine.csproj @@ -48,7 +48,7 @@ - + @@ -72,7 +72,7 @@ - + diff --git a/src/Umbraco.Examine/UmbracoContentIndex.cs b/src/Umbraco.Examine/UmbracoContentIndex.cs index 910941ea7b..ac9683f3f2 100644 --- a/src/Umbraco.Examine/UmbracoContentIndex.cs +++ b/src/Umbraco.Examine/UmbracoContentIndex.cs @@ -18,6 +18,45 @@ using Examine.LuceneEngine; namespace Umbraco.Examine { + /// + /// Custom allowing dynamic creation of + /// + public class UmbracoFieldDefinitionCollection : FieldDefinitionCollection + { + + public UmbracoFieldDefinitionCollection() + : base(UmbracoExamineIndex.UmbracoIndexFieldDefinitions) + { + } + + ///// + ///// Overridden to dynamically add field definitions for culture variations + ///// + ///// + ///// + ///// + //public override bool TryGetValue(string fieldName, out FieldDefinition fieldDefinition) + //{ + // var result = base.TryGetValue(fieldName, out fieldDefinition); + // if (result) return true; + + // //if the fieldName is not suffixed with _iso-Code + // var underscoreIndex = fieldName.LastIndexOf('_'); + // if (underscoreIndex == -1) return false; + + + + // var isoCode = fieldName.Substring(underscoreIndex); + // if (isoCode.Length < 6) return false; //invalid isoCode + + // var hyphenIndex = isoCode.IndexOf('-'); + // if (hyphenIndex != 3) return false; //invalid isoCode + + // //we'll assume this is a valid isoCode + + //} + } + /// /// An indexer for Umbraco content and media /// @@ -52,7 +91,7 @@ namespace Umbraco.Examine /// public UmbracoContentIndex( string name, - IEnumerable fieldDefinitions, + FieldDefinitionCollection fieldDefinitions, Directory luceneDirectory, Analyzer defaultAnalyzer, ProfilingLogger profilingLogger, @@ -214,27 +253,6 @@ namespace Umbraco.Examine base.PerformDeleteFromIndex(nodeId, onComplete); } - - /// - /// Overridden to ensure that the variant system fields have the right value types - /// - /// - /// - /// - protected override FieldValueTypeCollection CreateFieldValueTypes(IReadOnlyDictionary> indexValueTypesFactory = null) - { - //fixme: languages are dynamic so although this will work on startup it wont work when languages are edited - foreach(var lang in LanguageService.GetAllLanguages()) - { - foreach (var field in UmbracoIndexFieldDefinitions) - { - var def = new FieldDefinition($"{field.Name}_{lang.IsoCode.ToLowerInvariant()}", field.Type); - FieldDefinitionCollection.TryAdd(def.Name, def); - } - } - - return base.CreateFieldValueTypes(indexValueTypesFactory); - } } } diff --git a/src/Umbraco.Examine/UmbracoExamineIndex.cs b/src/Umbraco.Examine/UmbracoExamineIndex.cs index 15f1a75955..8d84e417d4 100644 --- a/src/Umbraco.Examine/UmbracoExamineIndex.cs +++ b/src/Umbraco.Examine/UmbracoExamineIndex.cs @@ -23,7 +23,7 @@ namespace Umbraco.Examine /// An abstract provider containing the basic functionality to be able to query against /// Umbraco data. /// - public abstract class UmbracoExamineIndex : LuceneIndex, IUmbracoIndexer, IIndexDiagnostics + public abstract class UmbracoExamineIndex : LuceneIndex, IUmbracoIndex, IIndexDiagnostics { // note // wrapping all operations that end up calling base.SafelyProcessQueueItems in a safe call @@ -67,7 +67,7 @@ namespace Umbraco.Examine /// protected UmbracoExamineIndex( string name, - IEnumerable fieldDefinitions, + FieldDefinitionCollection fieldDefinitions, Directory luceneDirectory, Analyzer defaultAnalyzer, ProfilingLogger profilingLogger, diff --git a/src/Umbraco.Examine/UmbracoMemberIndex.cs b/src/Umbraco.Examine/UmbracoMemberIndex.cs index 28b46f72dd..43962855c9 100644 --- a/src/Umbraco.Examine/UmbracoMemberIndex.cs +++ b/src/Umbraco.Examine/UmbracoMemberIndex.cs @@ -42,8 +42,8 @@ namespace Umbraco.Examine /// /// public UmbracoMemberIndex( - string name, - IEnumerable fieldDefinitions, + string name, + FieldDefinitionCollection fieldDefinitions, Directory luceneDirectory, Analyzer analyzer, ProfilingLogger profilingLogger, diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index 513701bf6a..b31a78a08f 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -77,7 +77,7 @@ - + 1.8.9 diff --git a/src/Umbraco.Tests/UmbracoExamine/IndexInitializer.cs b/src/Umbraco.Tests/UmbracoExamine/IndexInitializer.cs index cbd335a6c4..4435a5e829 100644 --- a/src/Umbraco.Tests/UmbracoExamine/IndexInitializer.cs +++ b/src/Umbraco.Tests/UmbracoExamine/IndexInitializer.cs @@ -164,7 +164,7 @@ namespace Umbraco.Tests.UmbracoExamine var i = new UmbracoContentIndex( "testIndexer", - UmbracoExamineIndex.UmbracoIndexFieldDefinitions, + new UmbracoFieldDefinitionCollection(), luceneDir, analyzer, profilingLogger, diff --git a/src/Umbraco.Web.UI/Umbraco.Web.UI.csproj b/src/Umbraco.Web.UI/Umbraco.Web.UI.csproj index 5751e9155c..60e1324969 100644 --- a/src/Umbraco.Web.UI/Umbraco.Web.UI.csproj +++ b/src/Umbraco.Web.UI/Umbraco.Web.UI.csproj @@ -88,7 +88,7 @@ - + diff --git a/src/Umbraco.Web/Editors/ExamineManagementController.cs b/src/Umbraco.Web/Editors/ExamineManagementController.cs index 2f96ee3d45..6667510b49 100644 --- a/src/Umbraco.Web/Editors/ExamineManagementController.cs +++ b/src/Umbraco.Web/Editors/ExamineManagementController.cs @@ -75,7 +75,7 @@ namespace Umbraco.Web.Editors var results = Examine.ExamineExtensions.TryParseLuceneQuery(query) ? searcher.Search(searcher.CreateCriteria().RawQuery(query), maxResults: pageSize * (pageIndex + 1)) - : searcher.Search(query, true, maxResults: pageSize * (pageIndex + 1)); + : searcher.Search(query, maxResults: pageSize * (pageIndex + 1)); var pagedResults = results.Skip(pageIndex * pageSize); diff --git a/src/Umbraco.Web/PublishedContentQuery.cs b/src/Umbraco.Web/PublishedContentQuery.cs index 67b54330c5..f27745055c 100644 --- a/src/Umbraco.Web/PublishedContentQuery.cs +++ b/src/Umbraco.Web/PublishedContentQuery.cs @@ -243,8 +243,8 @@ namespace Umbraco.Web var searcher = index.GetSearcher(); var results = skip == 0 && take == 0 - ? searcher.Search(term, true) - : searcher.Search(term, true, maxResults: skip + take); + ? searcher.Search(term) + : searcher.Search(term, maxResults: skip + take); totalRecords = results.TotalItemCount; return results.ToPublishedSearchResults(_contentCache); diff --git a/src/Umbraco.Web/Search/ExamineComponent.cs b/src/Umbraco.Web/Search/ExamineComponent.cs index 65264d0308..3061a668ca 100644 --- a/src/Umbraco.Web/Search/ExamineComponent.cs +++ b/src/Umbraco.Web/Search/ExamineComponent.cs @@ -149,7 +149,7 @@ namespace Umbraco.Web.Search profilingLogger.Logger.Debug("Examine shutdown registered with MainDom"); - var registeredIndexers = examineManager.Indexes.OfType().Count(x => x.EnableDefaultEventHandler); + var registeredIndexers = examineManager.Indexes.OfType().Count(x => x.EnableDefaultEventHandler); profilingLogger.Logger.Info("Adding examine event handlers for {RegisteredIndexers} index providers.", registeredIndexers); @@ -166,9 +166,19 @@ namespace Umbraco.Web.Search EnsureUnlocked(profilingLogger.Logger, examineManager); + //TODO: Instead of waiting 5000 ms, we could add an event handler on to fulfilling the first request, then start? RebuildIndexes(indexRebuilder, profilingLogger.Logger, true, 5000); } + private void TestExtending(IExamineManager examineManager) + { + if (examineManager.TryGetIndex("Test", out var index) && index is LuceneIndex umbIndex) + { + umbIndex.FieldValueTypeCollection.ValueTypeFactories.TryAdd() + umbIndex.FieldDefinitionCollection.TryAdd("productName_es-es", new FieldDefinition("productName_es-es", "")); + } + } + /// /// Called to rebuild empty indexes on startup @@ -496,7 +506,7 @@ namespace Umbraco.Web.Search //Delete all content of this content/media/member type that is in any content indexer by looking up matched examine docs foreach (var id in ci.Value.removedIds) { - foreach (var index in _examineManager.Indexes.OfType()) + foreach (var index in _examineManager.Indexes.OfType()) { var searcher = index.GetSearcher(); @@ -702,7 +712,7 @@ namespace Umbraco.Web.Search public static void Execute(ExamineComponent examineComponent, IContent content, bool isPublished) { - foreach (var index in examineComponent._examineManager.Indexes.OfType() + foreach (var index in examineComponent._examineManager.Indexes.OfType() //filter the indexers .Where(x => isPublished || !x.PublishedValuesOnly) .Where(x => x.EnableDefaultEventHandler)) @@ -739,7 +749,7 @@ namespace Umbraco.Web.Search { var valueSet = examineComponent._mediaValueSetBuilder.GetValueSets(media).ToList(); - foreach (var index in examineComponent._examineManager.Indexes.OfType() + foreach (var index in examineComponent._examineManager.Indexes.OfType() //filter the indexers .Where(x => isPublished || !x.PublishedValuesOnly) .Where(x => x.EnableDefaultEventHandler)) @@ -768,7 +778,7 @@ namespace Umbraco.Web.Search public static void Execute(ExamineComponent examineComponent, IMember member) { var valueSet = examineComponent._memberValueSetBuilder.GetValueSets(member).ToList(); - foreach (var index in examineComponent._examineManager.Indexes.OfType() + foreach (var index in examineComponent._examineManager.Indexes.OfType() //filter the indexers .Where(x => x.EnableDefaultEventHandler)) { @@ -798,7 +808,7 @@ namespace Umbraco.Web.Search public static void Execute(ExamineComponent examineComponent, int id, bool keepIfUnpublished) { var strId = id.ToString(CultureInfo.InvariantCulture); - foreach (var index in examineComponent._examineManager.Indexes.OfType() + foreach (var index in examineComponent._examineManager.Indexes.OfType() .Where(x => (keepIfUnpublished && !x.PublishedValuesOnly) || !keepIfUnpublished) .Where(x => x.EnableDefaultEventHandler)) diff --git a/src/Umbraco.Web/Search/GenericIndexDiagnostics.cs b/src/Umbraco.Web/Search/GenericIndexDiagnostics.cs index 32c8d43575..560fb19f09 100644 --- a/src/Umbraco.Web/Search/GenericIndexDiagnostics.cs +++ b/src/Umbraco.Web/Search/GenericIndexDiagnostics.cs @@ -33,7 +33,7 @@ namespace Umbraco.Web.Search try { var searcher = _index.GetSearcher(); - var result = searcher.Search("test", false); + var result = searcher.Search("test"); return Attempt.Succeed(); //if we can search we'll assume it's healthy } catch (Exception e) diff --git a/src/Umbraco.Web/Search/UmbracoIndexesCreator.cs b/src/Umbraco.Web/Search/UmbracoIndexesCreator.cs index 3723dedc48..4296176abf 100644 --- a/src/Umbraco.Web/Search/UmbracoIndexesCreator.cs +++ b/src/Umbraco.Web/Search/UmbracoIndexesCreator.cs @@ -59,8 +59,7 @@ namespace Umbraco.Web.Search { var index = new UmbracoContentIndex( Constants.UmbracoIndexes.InternalIndexName, - //fixme - how to deal with languages like in UmbracoContentIndexer.CreateFieldValueTypes - UmbracoExamineIndex.UmbracoIndexFieldDefinitions, + new UmbracoFieldDefinitionCollection(), CreateFileSystemLuceneDirectory(Constants.UmbracoIndexes.InternalIndexPath), new CultureInvariantWhitespaceAnalyzer(), ProfilingLogger, @@ -73,8 +72,7 @@ namespace Umbraco.Web.Search { var index = new UmbracoContentIndex( Constants.UmbracoIndexes.ExternalIndexName, - //fixme - how to deal with languages like in UmbracoContentIndexer.CreateFieldValueTypes - UmbracoExamineIndex.UmbracoIndexFieldDefinitions, + new UmbracoFieldDefinitionCollection(), CreateFileSystemLuceneDirectory(Constants.UmbracoIndexes.ExternalIndexPath), new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30), ProfilingLogger, @@ -87,8 +85,7 @@ namespace Umbraco.Web.Search { var index = new UmbracoMemberIndex( Constants.UmbracoIndexes.MembersIndexName, - //fixme - how to deal with languages like in UmbracoContentIndexer.CreateFieldValueTypes - UmbracoExamineIndex.UmbracoIndexFieldDefinitions, + new UmbracoFieldDefinitionCollection(), CreateFileSystemLuceneDirectory(Constants.UmbracoIndexes.MembersIndexPath), new CultureInvariantWhitespaceAnalyzer(), ProfilingLogger, diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index 4034dd2be6..e2df681122 100755 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -62,7 +62,7 @@ - + 2.6.2.25