diff --git a/src/Orchard.Web/Modules/Orchard.Search/Drivers/SearchSettingsPartDriver.cs b/src/Orchard.Web/Modules/Orchard.Search/Drivers/SearchSettingsPartDriver.cs index b2a1e04d3..bdd5aedfb 100644 --- a/src/Orchard.Web/Modules/Orchard.Search/Drivers/SearchSettingsPartDriver.cs +++ b/src/Orchard.Web/Modules/Orchard.Search/Drivers/SearchSettingsPartDriver.cs @@ -5,6 +5,7 @@ using Orchard.ContentManagement.Drivers; using Orchard.ContentManagement.Handlers; using Orchard.Indexing; using Orchard.Localization; +using Orchard.Search.Helpers; using Orchard.Search.Models; using Orchard.Search.ViewModels; @@ -35,7 +36,9 @@ namespace Orchard.Search.Drivers var model = new SearchSettingsIndexViewModel { SelectedIndex = part.SearchIndex, - AvailableIndexes = _indexManager.GetSearchIndexProvider().List().ToList() + AvailableIndexes = _indexManager.HasIndexProvider() + ? _indexManager.GetSearchIndexProvider().List().ToList() + : new List() }; if (updater != null) @@ -102,7 +105,9 @@ namespace Orchard.Search.Drivers context.ImportAttribute(part.PartDefinition.Name, "SearchFields", value => { - part.Store("SearchFields", value); + part.Store( + "SearchFields", + SearchSettingsHelper.MergeSearchFields(part.SearchFields, SearchSettingsHelper.DeserializeSearchFields(value))); }); } } diff --git a/src/Orchard.Web/Modules/Orchard.Search/Helpers/SearchSettingsHelper.cs b/src/Orchard.Web/Modules/Orchard.Search/Helpers/SearchSettingsHelper.cs index 0ec477295..21b1ca9e4 100644 --- a/src/Orchard.Web/Modules/Orchard.Search/Helpers/SearchSettingsHelper.cs +++ b/src/Orchard.Web/Modules/Orchard.Search/Helpers/SearchSettingsHelper.cs @@ -27,15 +27,43 @@ namespace Orchard.Search.Helpers return dictionary; } - public static string SerializeSearchFields(IDictionary value) - { - var data = string.Join("|", value.Select(x => string.Format("{0}:{1}", x.Key, string.Join(",", x.Value)))); - return data; - } + public static string SerializeSearchFields(IDictionary value) => + string.Join("|", value.Select(x => string.Format("{0}:{1}", x.Key, string.Join(",", x.Value)))); - public static string[] GetSearchFields(this SearchSettingsPart part, string index) + public static string[] GetSearchFields(this SearchSettingsPart part, string index) => + part.SearchFields.ContainsKey(index) ? part.SearchFields[index] : new string[0]; + + /// + /// Merge existing search index settings with those being imported, with the latter taking precedence. + /// + /// The existing search indexes with their search fields. The key is the + /// index name and the value is the array of field names. + /// The new search indexes with their search fields to import. The key is the + /// index name and the value is the array of field names. + /// The merged search indexes with their search fields. + public static string MergeSearchFields(IDictionary existingSearchFields, IDictionary searchFieldsToAdd) { - return part.SearchFields.ContainsKey(index) ? part.SearchFields[index] : new string[0]; + var mergedSearchFields = new Dictionary(); + + // Process new search fields to add first so they take precedence. + foreach (var newSearchFieldsToAdd in searchFieldsToAdd.Keys) + { + if (!mergedSearchFields.ContainsKey(newSearchFieldsToAdd)) + { + mergedSearchFields.Add(newSearchFieldsToAdd, searchFieldsToAdd[newSearchFieldsToAdd]); + } + } + + // Then add existing search fields that are not already present. + foreach (var existingSearchFieldsToAdd in existingSearchFields.Keys) + { + if (!mergedSearchFields.ContainsKey(existingSearchFieldsToAdd)) + { + mergedSearchFields.Add(existingSearchFieldsToAdd, existingSearchFields[existingSearchFieldsToAdd]); + } + } + + return SerializeSearchFields(mergedSearchFields); } } -} \ No newline at end of file +}