コード例 #1
0
        public ActionResult AddNewIndex()
        {
            if (Core.Server.Info.Version.Major < 5)
            {
                throw new Exception("Elasticsearch version 5 or higher required");
            }

            ElasticSearchSection config = ElasticSearchSection.GetConfiguration();

            IEnumerable <string> languages = _languageBranchRepository
                                             .ListEnabled()
                                             .Select(lang => lang.LanguageID);

            foreach (string lang in languages)
            {
                foreach (IndexConfiguration indexConfig in config.IndicesParsed)
                {
                    var  indexName = _settings.GetCustomIndexName(indexConfig.Name, lang);
                    Type indexType = GetIndexType(indexConfig, config);

                    var index = new Index(_settings, indexName);

                    if (!index.Exists)
                    {
                        index.Initialize(indexType);
                        index.WaitForStatus();
                        index.DisableDynamicMapping(indexType);
                        index.WaitForStatus();
                    }

                    if (IsCustomType(indexType))
                    {
                        _coreIndexer.UpdateMapping(indexType, indexType, indexName, lang, true);
                        index.WaitForStatus();
                    }
                    else if (_settings.CommerceEnabled)
                    {
                        indexName = _settings.GetCustomIndexName($"{indexConfig.Name}-{Constants.CommerceProviderName}", lang);
                        index     = new Index(_settings, indexName);
                        if (!index.Exists)
                        {
                            index.Initialize(indexType);
                            index.WaitForStatus();
                            index.DisableDynamicMapping(indexType);
                            index.WaitForStatus();
                        }
                    }
                }
            }

            return(RedirectToAction("Index"));
        }
コード例 #2
0
        public JsonResult UpdateItem(string id, bool recursive = false)
        {
            try
            {
                if (_contentLoader.TryGet(ContentReference.Parse(id), out IContent content))
                {
                    string indexName = null;

                    // Point catalog content to correct index
                    if (Constants.CommerceProviderName.Equals(content.ContentLink.ProviderName))
                    {
                        string lang = _indexer.GetLanguage(content);
                        indexName = _settings.GetCustomIndexName($"{_settings.Index}-{Constants.CommerceProviderName}", lang);
                    }

                    IndexingStatus status = recursive
                        ? _indexer.UpdateStructure(content, indexName)
                        : _indexer.Update(content, indexName);

                    return(Json(new { status = status.ToString() }));
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Error updating item with id '" + id + "'", ex);
                return(Json(new { status = nameof(IndexingStatus.Error), error = ex.Message }));
            }

            return(Json(new { status = nameof(IndexingStatus.Error) }));
        }
コード例 #3
0
 /// <summary>
 /// Refresh the index
 /// </summary>
 /// <param name="language"></param>
 /// <remarks>https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-refresh.html</remarks>
 public void Refresh(string language)
 {
     foreach (string indexName in _settings.Indices.Select(i => _settings.GetCustomIndexName(i, language)))
     {
         RefreshIndex(indexName);
     }
 }
コード例 #4
0
        private string GetIndexName(string language)
        {
            if (!String.IsNullOrEmpty(CustomIndexName))
            {
                return(_settings.GetCustomIndexName(CustomIndexName, language));
            }

            return(_settings.GetDefaultIndexName(language));
        }
コード例 #5
0
        public ActionResult AddNewIndex()
        {
            if (Core.Server.Info.Version.Major < 5)
            {
                throw new InvalidOperationException("Elasticsearch version 5 or higher required");
            }

            var config = ElasticSearchSection.GetConfiguration();

            foreach (var lang in Languages)
            {
                foreach (IndexConfiguration indexConfig in config.IndicesParsed)
                {
                    var  indexName = _settings.GetCustomIndexName(indexConfig.Name, lang.Key);
                    Type indexType = GetIndexType(indexConfig, config);

                    var index = new Index(_settings, _httpClientHelper, indexName);

                    if (!index.Exists)
                    {
                        index.Initialize(indexType);
                    }

                    if (IsCustomType(indexType))
                    {
                        _coreIndexer.UpdateMapping(indexType, indexType, indexName, lang.Key, false);
                        index.WaitForStatus();
                    }
                    else if (_settings.CommerceEnabled)
                    {
                        CreateCommerceIndex(lang.Key, indexConfig, indexType);
                    }
                    else
                    {
                        index.WaitForStatus();
                        index.DisableDynamicMapping(indexType);
                        index.WaitForStatus();
                    }
                }
            }

            return(RedirectToAction("Index"));
        }
コード例 #6
0
        public virtual ActionResult AddNewIndex()
        {
            if (_serverInfoService.GetInfo().Version < Constants.MinimumSupportedVersion)
            {
                throw new InvalidOperationException("Elasticsearch version 5 or higher required");
            }

            ElasticSearchSection config = ElasticSearchSection.GetConfiguration();

            foreach (KeyValuePair <string, string> lang in Languages)
            {
                foreach (IndexConfiguration indexConfig in config.IndicesParsed)
                {
                    Type indexType = GetIndexType(indexConfig, config);
                    var  indexName = _settings.GetCustomIndexName(indexConfig.Name, lang.Key);

                    CreateIndex(indexType, indexName);
                }
            }

            return(RedirectToAction("Index"));
        }
コード例 #7
0
        public BulkBatchResult BulkUpdate(IEnumerable <IContent> contents, Action <string> logger, string indexName = null)
        {
            List <IContent> contentList = contents.ToList();

            logger = logger ?? delegate { };
            var before = contentList.Count;

            logger("Filtering away content of excluded types and content with property HideFromSearch enabled...");

            contentList.RemoveAll(ShouldHideFromSearch);
            contentList.RemoveAll(IsExludedType);

            logger($"Filtered away content of excluded types and content with property HideFromSearch enabled... {before - contentList.Count} of {before} items removed. Next will IContent be converted to indexable items and added to indexed. Depending on number of IContent items this is a time-consuming task.");

            var operations =
                contentList.Select(
                    content =>
            {
                var language = GetLanguage(content);
                var index    = String.IsNullOrWhiteSpace(indexName)
                                    ? _elasticSearchSettings.GetDefaultIndexName(language)
                                    : _elasticSearchSettings.GetCustomIndexName(indexName, language);

                return(new BulkOperation(
                           content.AsIndexItem(),
                           Operation.Index,
                           GetLanguage(content),
                           typeof(IndexItem),
                           content.ContentLink.ToReferenceWithoutVersion().ToString(),
                           index));
            }
                    )
                .Where(b => b.Data != null)
                .ToList();

            logger($"Initializing bulk operation... Bulk indexing {operations.Count} items");

            return(_coreIndexer.Bulk(operations, logger));
        }
コード例 #8
0
        private void UpdateMappings(IList <LanguageBranch> languages, string indexName = null)
        {
            // Update mappings
            foreach (var language in languages.Select(l => l.LanguageID))
            {
                try
                {
                    if (String.IsNullOrWhiteSpace(indexName))
                    {
                        indexName = _settings.GetDefaultIndexName(language);
                    }
                    else
                    {
                        indexName = _settings.GetCustomIndexName(indexName, language);
                    }

                    _logger.Debug("Index: " + indexName);

                    var indexing = new Indexing(_settings);

                    if (!indexing.IndexExists(indexName))
                    {
                        throw new Exception("Index does not exist");
                    }

                    OnStatusChanged("Updating mapping for index " + indexName);

                    _coreIndexer.UpdateMapping(typeof(IndexItem), typeof(IndexItem), indexName);

                    ContentExtensions.CreateAnalyzedMappingsIfNeeded(typeof(IndexItem), language, indexName);
                    ContentExtensions.CreateDidYouMeanMappingsIfNeeded(typeof(IndexItem), language, indexName);
                }
                catch (Exception ex)
                {
                    _logger.Warning("Uh oh...", ex);
                }
            }
        }
コード例 #9
0
 private string GetIndexName(string languageId, string selectedIndex = null)
 {
     return(selectedIndex != null
         ? _elasticSearchSettings.GetCustomIndexName(selectedIndex, languageId)
         : _elasticSearchSettings.GetDefaultIndexName(languageId));
 }
コード例 #10
0
 private string GetIndexName(string language) => !String.IsNullOrEmpty(CustomIndexName) ? _settings.GetCustomIndexName(CustomIndexName, language) : _settings.GetDefaultIndexName(language);