From 23c7e596d4644062c0f4dbfbc36325cd30689ace Mon Sep 17 00:00:00 2001 From: "DK\\psk" Date: Tue, 30 Oct 2018 12:22:51 +0200 Subject: [PATCH] Fix the issue for 9.0.1, using roles an considering all search providers --- .../Include/zzz/Sitecore.Support.96931.config | 14 ++- .../Azure/CloudSearchDocumentBuilder.cs | 94 +++++++++++++++++++ .../LuceneProvider/LuceneDocumentBuilder.cs | 85 +++++++++-------- .../SolrProvider/SolrDocumentBuilder.cs | 94 +++++++++++++++++++ .../Sitecore.Support.96931.csproj | 11 +++ src/Sitecore.Support.96931/packages.config | 6 +- 6 files changed, 259 insertions(+), 45 deletions(-) create mode 100644 src/Sitecore.Support.96931/ContentSearch/Azure/CloudSearchDocumentBuilder.cs create mode 100644 src/Sitecore.Support.96931/ContentSearch/SolrProvider/SolrDocumentBuilder.cs diff --git a/src/Sitecore.Support.96931/App_Config/Include/zzz/Sitecore.Support.96931.config b/src/Sitecore.Support.96931/App_Config/Include/zzz/Sitecore.Support.96931.config index 2c531cb..7302d1a 100644 --- a/src/Sitecore.Support.96931/App_Config/Include/zzz/Sitecore.Support.96931.config +++ b/src/Sitecore.Support.96931/App_Config/Include/zzz/Sitecore.Support.96931.config @@ -1,12 +1,22 @@ - + - + Sitecore.Support.ContentSearch.LuceneProvider.LuceneDocumentBuilder, Sitecore.Support.96931 + + + Sitecore.Support.ContentSearch.SolrProvider.SolrDocumentBuilder, Sitecore.Support.96931 + + + + + Sitecore.Support.ContentSearch.Azure.CloudSearchDocumentBuilder, Sitecore.Support.96931 + + diff --git a/src/Sitecore.Support.96931/ContentSearch/Azure/CloudSearchDocumentBuilder.cs b/src/Sitecore.Support.96931/ContentSearch/Azure/CloudSearchDocumentBuilder.cs new file mode 100644 index 0000000..939b40e --- /dev/null +++ b/src/Sitecore.Support.96931/ContentSearch/Azure/CloudSearchDocumentBuilder.cs @@ -0,0 +1,94 @@ +namespace Sitecore.Support.ContentSearch.Azure +{ + using System; + using System.Collections.Concurrent; + using Sitecore.ContentSearch; + using Sitecore.ContentSearch.Diagnostics; + using Sitecore.Data.LanguageFallback; + + public class CloudSearchDocumentBuilder : Sitecore.ContentSearch.Azure.CloudSearchDocumentBuilder + { + public CloudSearchDocumentBuilder(IIndexable indexable, IProviderUpdateContext context) : base(indexable, context) + { + } + + protected override void AddComputedIndexFieldsInParallel() + { + ConcurrentQueue exceptions = new ConcurrentQueue(); + + //ensure that we preserve current item-level language fallback setting when entering new threads + var needEnterLanguageFallbackItemSwitcher = LanguageFallbackItemSwitcher.CurrentValue; + + this.ParallelForeachProxy.ForEach( + this.Options.ComputedIndexFields, + this.ParallelOptions, + (computedIndexField, parallelLoopState) => + { + object fieldValue; + + try + { + using (new LanguageFallbackItemSwitcher(needEnterLanguageFallbackItemSwitcher)) + { + //take field-level language fallback setting into accout (fix for issue #96931) + using (new LanguageFallbackFieldSwitcher(this.Index.EnableFieldLanguageFallback)) + { + fieldValue = computedIndexField.ComputeFieldValue(this.Indexable); + } + } + } + catch (Exception ex) + { + CrawlingLog.Log.Warn( + string.Format("Could not compute value for ComputedIndexField: {0} for indexable: {1}", + computedIndexField.FieldName, this.Indexable.UniqueId), ex); + if (this.Settings.StopOnCrawlFieldError()) + { + exceptions.Enqueue(ex); + parallelLoopState.Stop(); + } + + System.Diagnostics.Debug.WriteLine(ex); + return; + } + + this.AddComputedIndexField(computedIndexField, fieldValue); + }); + + if (!exceptions.IsEmpty) + { + throw new AggregateException(exceptions); + } + } + + protected override void AddComputedIndexFieldsInSequence() + { + foreach (var computedIndexField in this.Options.ComputedIndexFields) + { + object fieldValue; + + try + { + //take field-level language fallback setting into accout (fix for issue #96931) + using (new LanguageFallbackFieldSwitcher(this.Index.EnableFieldLanguageFallback)) + { + fieldValue = computedIndexField.ComputeFieldValue(this.Indexable); + } + } + catch (Exception ex) + { + CrawlingLog.Log.Warn(string.Format("Could not compute value for ComputedIndexField: {0} for indexable: {1}", computedIndexField.FieldName, this.Indexable.UniqueId), ex); + if (this.Settings.StopOnCrawlFieldError()) + { + throw; + } + + System.Diagnostics.Debug.WriteLine(ex); + continue; + } + + this.AddComputedIndexField(computedIndexField, fieldValue); + } + } + } +} \ No newline at end of file diff --git a/src/Sitecore.Support.96931/ContentSearch/LuceneProvider/LuceneDocumentBuilder.cs b/src/Sitecore.Support.96931/ContentSearch/LuceneProvider/LuceneDocumentBuilder.cs index a10ef10..2521ffe 100644 --- a/src/Sitecore.Support.96931/ContentSearch/LuceneProvider/LuceneDocumentBuilder.cs +++ b/src/Sitecore.Support.96931/ContentSearch/LuceneProvider/LuceneDocumentBuilder.cs @@ -2,20 +2,12 @@ { using System; using System.Collections.Concurrent; - using System.Reflection; - using System.Threading.Tasks; using Sitecore.ContentSearch; - using Sitecore.ContentSearch.ComputedFields; using Sitecore.ContentSearch.Diagnostics; using Sitecore.Data.LanguageFallback; public class LuceneDocumentBuilder : Sitecore.ContentSearch.LuceneProvider.LuceneDocumentBuilder { - private static readonly MethodInfo AddComputedIndexFieldMethodInfo; - static LuceneDocumentBuilder() - { - AddComputedIndexFieldMethodInfo = typeof(Sitecore.ContentSearch.LuceneProvider.LuceneDocumentBuilder).GetMethod("AddComputedIndexField", BindingFlags.Instance | BindingFlags.NonPublic); - } public LuceneDocumentBuilder(IIndexable indexable, IProviderUpdateContext context) : base(indexable, context) { } @@ -23,68 +15,79 @@ public LuceneDocumentBuilder(IIndexable indexable, IProviderUpdateContext contex protected override void AddComputedIndexFieldsInParallel() { ConcurrentQueue exceptions = new ConcurrentQueue(); + + //ensure that we preserve current item-level language fallback setting when entering new threads var needEnterLanguageFallbackItemSwitcher = LanguageFallbackItemSwitcher.CurrentValue; - Parallel.ForEach(base.Options.ComputedIndexFields, base.ParallelOptions, delegate (IComputedIndexField computedIndexField, ParallelLoopState parallelLoopState) - { - object fieldValue; - try + + this.ParallelForeachProxy.ForEach( + this.Options.ComputedIndexFields, + this.ParallelOptions, + (computedIndexField, parallelLoopState) => { - using (new LanguageFallbackItemSwitcher(needEnterLanguageFallbackItemSwitcher)) + object fieldValue; + + try { - using (new LanguageFallbackFieldSwitcher(this.Index.EnableFieldLanguageFallback)) + using (new LanguageFallbackItemSwitcher(needEnterLanguageFallbackItemSwitcher)) { - fieldValue = computedIndexField.ComputeFieldValue(this.Indexable); + //take field-level language fallback setting into accout (fix for issue #96931) + using (new LanguageFallbackFieldSwitcher(this.Index.EnableFieldLanguageFallback)) + { + fieldValue = computedIndexField.ComputeFieldValue(this.Indexable); + } } } - } - catch (Exception ex) - { - CrawlingLog.Log.Warn(string.Format("Could not compute value for ComputedIndexField: {0} for indexable: {1}", computedIndexField.FieldName, this.Indexable.UniqueId), ex); - if (this.Settings.StopOnCrawlFieldError()) + catch (Exception ex) { - exceptions.Enqueue(ex); - parallelLoopState.Stop(); + CrawlingLog.Log.Warn( + string.Format("Could not compute value for ComputedIndexField: {0} for indexable: {1}", + computedIndexField.FieldName, this.Indexable.UniqueId), ex); + if (this.Settings.StopOnCrawlFieldError()) + { + exceptions.Enqueue(ex); + parallelLoopState.Stop(); + } + + System.Diagnostics.Debug.WriteLine(ex); + return; } - return; - } - this.AddComputedIndexField(computedIndexField, fieldValue); - }); - if (exceptions.Count > 0) + + this.AddComputedIndexField(computedIndexField, fieldValue); + }); + + if (!exceptions.IsEmpty) { throw new AggregateException(exceptions); } } - private void AddComputedIndexField(IComputedIndexField computedIndexField, object fieldValue) - { - AddComputedIndexFieldMethodInfo.Invoke(this, new object[] - { - computedIndexField, fieldValue - }); - } - protected override void AddComputedIndexFieldsInSequence() { - foreach (IComputedIndexField current in base.Options.ComputedIndexFields) + foreach (var computedIndexField in this.Options.ComputedIndexFields) { object fieldValue; + try { + //take field-level language fallback setting into accout (fix for issue #96931) using (new LanguageFallbackFieldSwitcher(this.Index.EnableFieldLanguageFallback)) { - fieldValue = current.ComputeFieldValue(base.Indexable); + fieldValue = computedIndexField.ComputeFieldValue(this.Indexable); } } - catch (Exception exception) + catch (Exception ex) { - CrawlingLog.Log.Warn(string.Format("Could not compute value for ComputedIndexField: {0} for indexable: {1}", current.FieldName, base.Indexable.UniqueId), exception); - if (base.Settings.StopOnCrawlFieldError()) + CrawlingLog.Log.Warn(string.Format("Could not compute value for ComputedIndexField: {0} for indexable: {1}", computedIndexField.FieldName, this.Indexable.UniqueId), ex); + if (this.Settings.StopOnCrawlFieldError()) { throw; } + + System.Diagnostics.Debug.WriteLine(ex); continue; } - this.AddComputedIndexField(current, fieldValue); + + this.AddComputedIndexField(computedIndexField, fieldValue); } } } diff --git a/src/Sitecore.Support.96931/ContentSearch/SolrProvider/SolrDocumentBuilder.cs b/src/Sitecore.Support.96931/ContentSearch/SolrProvider/SolrDocumentBuilder.cs new file mode 100644 index 0000000..5ef20ec --- /dev/null +++ b/src/Sitecore.Support.96931/ContentSearch/SolrProvider/SolrDocumentBuilder.cs @@ -0,0 +1,94 @@ +namespace Sitecore.Support.ContentSearch.SolrProvider +{ + using System; + using System.Collections.Concurrent; + using Sitecore.ContentSearch; + using Sitecore.ContentSearch.Diagnostics; + using Sitecore.Data.LanguageFallback; + + public class SolrDocumentBuilder : Sitecore.ContentSearch.SolrProvider.SolrDocumentBuilder + { + public SolrDocumentBuilder(IIndexable indexable, IProviderUpdateContext context) : base(indexable, context) + { + } + + protected override void AddComputedIndexFieldsInParallel() + { + ConcurrentQueue exceptions = new ConcurrentQueue(); + + //ensure that we preserve current item-level language fallback setting when entering new threads + var needEnterLanguageFallbackItemSwitcher = LanguageFallbackItemSwitcher.CurrentValue; + + this.ParallelForeachProxy.ForEach( + this.Options.ComputedIndexFields, + this.ParallelOptions, + (computedIndexField, parallelLoopState) => + { + object fieldValue; + + try + { + using (new LanguageFallbackItemSwitcher(needEnterLanguageFallbackItemSwitcher)) + { + //take field-level language fallback setting into accout (fix for issue #96931) + using (new LanguageFallbackFieldSwitcher(this.Index.EnableFieldLanguageFallback)) + { + fieldValue = computedIndexField.ComputeFieldValue(this.Indexable); + } + } + } + catch (Exception ex) + { + CrawlingLog.Log.Warn( + string.Format("Could not compute value for ComputedIndexField: {0} for indexable: {1}", + computedIndexField.FieldName, this.Indexable.UniqueId), ex); + if (this.Settings.StopOnCrawlFieldError()) + { + exceptions.Enqueue(ex); + parallelLoopState.Stop(); + } + + System.Diagnostics.Debug.WriteLine(ex); + return; + } + + this.AddComputedIndexField(computedIndexField, fieldValue); + }); + + if (!exceptions.IsEmpty) + { + throw new AggregateException(exceptions); + } + } + + protected override void AddComputedIndexFieldsInSequence() + { + foreach (var computedIndexField in this.Options.ComputedIndexFields) + { + object fieldValue; + + try + { + //take field-level language fallback setting into accout (fix for issue #96931) + using (new LanguageFallbackFieldSwitcher(this.Index.EnableFieldLanguageFallback)) + { + fieldValue = computedIndexField.ComputeFieldValue(this.Indexable); + } + } + catch (Exception ex) + { + CrawlingLog.Log.Warn(string.Format("Could not compute value for ComputedIndexField: {0} for indexable: {1}", computedIndexField.FieldName, this.Indexable.UniqueId), ex); + if (this.Settings.StopOnCrawlFieldError()) + { + throw; + } + + System.Diagnostics.Debug.WriteLine(ex); + continue; + } + + this.AddComputedIndexField(computedIndexField, fieldValue); + } + } + } +} \ No newline at end of file diff --git a/src/Sitecore.Support.96931/Sitecore.Support.96931.csproj b/src/Sitecore.Support.96931/Sitecore.Support.96931.csproj index 59df0c1..c7d40ef 100644 --- a/src/Sitecore.Support.96931/Sitecore.Support.96931.csproj +++ b/src/Sitecore.Support.96931/Sitecore.Support.96931.csproj @@ -21,6 +21,7 @@ + true @@ -48,10 +49,18 @@ ..\packages\SC.Sitecore.ContentSearch.9.0.1\lib\Sitecore.ContentSearch.dll False + + ..\packages\SC.Sitecore.ContentSearch.Azure.9.0.1\lib\Sitecore.ContentSearch.Azure.dll + False + ..\packages\SC.Sitecore.ContentSearch.LuceneProvider.9.0.1\lib\Sitecore.ContentSearch.LuceneProvider.dll False + + ..\packages\SC.Sitecore.ContentSearch.SolrProvider.9.0.1\lib\Sitecore.ContentSearch.SolrProvider.dll + False + ..\packages\SC.Sitecore.Kernel.9.0.1\lib\Sitecore.Kernel.dll False @@ -63,8 +72,10 @@ + + diff --git a/src/Sitecore.Support.96931/packages.config b/src/Sitecore.Support.96931/packages.config index 70fbab1..7b07151 100644 --- a/src/Sitecore.Support.96931/packages.config +++ b/src/Sitecore.Support.96931/packages.config @@ -1,7 +1,9 @@ - + + + - + \ No newline at end of file