Files
Umbraco-CMS/src/Umbraco.Web/Editors/ExamineManagementController.cs

276 lines
10 KiB
C#
Raw Normal View History

2018-05-31 22:03:12 +01:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Web.Http;
using Examine;
using Examine.LuceneEngine;
using Examine.LuceneEngine.Providers;
using Lucene.Net.Analysis;
using Lucene.Net.QueryParsers;
2018-05-31 22:03:12 +01:00
using Umbraco.Core;
using Umbraco.Core.Cache;
using Umbraco.Core.Composing;
using Umbraco.Core.Logging;
using Umbraco.Examine;
using Umbraco.Web.Models.ContentEditing;
2018-05-31 22:03:12 +01:00
using Umbraco.Web.Mvc;
using Umbraco.Web.Search;
using SearchResult = Umbraco.Web.Models.ContentEditing.SearchResult;
using Version = Lucene.Net.Util.Version;
2018-05-31 22:03:12 +01:00
namespace Umbraco.Web.Editors
{
[PluginController("UmbracoApi")]
public class ExamineManagementController : UmbracoAuthorizedJsonController
{
private readonly IExamineManager _examineManager;
private readonly ILogger _logger;
2019-01-17 11:01:23 +01:00
private readonly IAppPolicedCache _runtimeCacheProvider;
2018-12-03 23:15:18 +11:00
private readonly IndexRebuilder _indexRebuilder;
2018-05-31 22:03:12 +01:00
public ExamineManagementController(IExamineManager examineManager, ILogger logger,
2019-01-17 11:01:23 +01:00
IAppPolicedCache runtimeCacheProvider,
2018-12-03 23:15:18 +11:00
IndexRebuilder indexRebuilder)
2018-05-31 22:03:12 +01:00
{
_examineManager = examineManager;
_logger = logger;
_runtimeCacheProvider = runtimeCacheProvider;
2018-12-03 23:15:18 +11:00
_indexRebuilder = indexRebuilder;
2018-05-31 22:03:12 +01:00
}
/// <summary>
/// Get the details for indexers
/// </summary>
/// <returns></returns>
public IEnumerable<ExamineIndexModel> GetIndexerDetails()
2018-05-31 22:03:12 +01:00
{
return _examineManager.Indexes.Select(CreateModel).OrderBy(x => x.Name.TrimEnd("Indexer"));
2018-05-31 22:03:12 +01:00
}
/// <summary>
/// Get the details for searchers
/// </summary>
/// <returns></returns>
public IEnumerable<ExamineSearcherModel> GetSearcherDetails()
{
var model = new List<ExamineSearcherModel>(
_examineManager.RegisteredSearchers.Select(searcher => new ExamineSearcherModel { Name = searcher.Name })
.OrderBy(x => x.Name.TrimEnd("Searcher"))); //order by name , but strip the "Searcher" from the end if it exists
2018-05-31 22:03:12 +01:00
return model;
}
public SearchResults GetSearchResults(string searcherName, string query, int pageIndex = 0, int pageSize = 20)
2018-05-31 22:03:12 +01:00
{
if (query.IsNullOrWhiteSpace())
return SearchResults.Empty();
var msg = ValidateSearcher(searcherName, out var searcher);
if (!msg.IsSuccessStatusCode)
throw new HttpResponseException(msg);
var results = Examine.ExamineExtensions.TryParseLuceneQuery(query)
? searcher.CreateQuery().NativeQuery(query).Execute(maxResults: pageSize * (pageIndex + 1))
: searcher.Search(query, maxResults: pageSize * (pageIndex + 1));
var pagedResults = results.Skip(pageIndex * pageSize);
2018-05-31 22:03:12 +01:00
return new SearchResults
2018-05-31 22:03:12 +01:00
{
TotalRecords = results.TotalItemCount,
Results = pagedResults.Select(x => new SearchResult
2018-05-31 22:03:12 +01:00
{
Id = x.Id,
Score = x.Score,
//order the values by key
Values = new Dictionary<string, string>(x.Values.OrderBy(y => y.Key).ToDictionary(y => y.Key, y => y.Value))
})
};
}
2018-05-31 22:03:12 +01:00
2018-05-31 22:03:12 +01:00
/// <summary>
/// Check if the index has been rebuilt
/// </summary>
2018-11-30 12:22:23 +11:00
/// <param name="indexName"></param>
2018-05-31 22:03:12 +01:00
/// <returns></returns>
/// <remarks>
/// This is kind of rudimentary since there's no way we can know that the index has rebuilt, we
/// have a listener for the index op complete so we'll just check if that key is no longer there in the runtime cache
/// </remarks>
2018-11-30 12:22:23 +11:00
public ExamineIndexModel PostCheckRebuildIndex(string indexName)
2018-05-31 22:03:12 +01:00
{
2018-11-30 12:22:23 +11:00
var validate = ValidateIndex(indexName, out var index);
if (!validate.IsSuccessStatusCode)
throw new HttpResponseException(validate);
2018-05-31 22:03:12 +01:00
validate = ValidatePopulator(index);
2018-11-30 12:22:23 +11:00
if (!validate.IsSuccessStatusCode)
throw new HttpResponseException(validate);
var cacheKey = "temp_indexing_op_" + indexName;
2019-01-17 11:01:23 +01:00
var found = ApplicationCache.RuntimeCache.Get(cacheKey);
2018-11-30 12:22:23 +11:00
//if its still there then it's not done
return found != null
? null
: CreateModel(index);
2018-05-31 22:03:12 +01:00
}
/// <summary>
2018-11-30 12:22:23 +11:00
/// Rebuilds the index
2018-05-31 22:03:12 +01:00
/// </summary>
2018-11-30 12:22:23 +11:00
/// <param name="indexName"></param>
2018-05-31 22:03:12 +01:00
/// <returns></returns>
2018-11-30 12:22:23 +11:00
public HttpResponseMessage PostRebuildIndex(string indexName)
2018-05-31 22:03:12 +01:00
{
2018-11-30 12:22:23 +11:00
var validate = ValidateIndex(indexName, out var index);
if (!validate.IsSuccessStatusCode)
return validate;
2018-05-31 22:03:12 +01:00
validate = ValidatePopulator(index);
2018-11-30 12:22:23 +11:00
if (!validate.IsSuccessStatusCode)
return validate;
2018-05-31 22:03:12 +01:00
2018-11-30 12:22:23 +11:00
_logger.Info<ExamineManagementController>("Rebuilding index '{IndexName}'", indexName);
2018-05-31 22:03:12 +01:00
2018-11-30 12:22:23 +11:00
//remove it in case there's a handler there alraedy
index.IndexOperationComplete -= Indexer_IndexOperationComplete;
2018-05-31 22:03:12 +01:00
2018-11-30 12:22:23 +11:00
//now add a single handler
index.IndexOperationComplete += Indexer_IndexOperationComplete;
2018-05-31 22:03:12 +01:00
2018-11-30 12:22:23 +11:00
try
{
//clear and replace
index.CreateIndex();
var cacheKey = "temp_indexing_op_" + index.Name;
//put temp val in cache which is used as a rudimentary way to know when the indexing is done
2019-01-17 11:01:23 +01:00
ApplicationCache.RuntimeCache.Insert(cacheKey, () => "tempValue", TimeSpan.FromMinutes(5));
2018-12-03 23:15:18 +11:00
_indexRebuilder.RebuildIndex(indexName);
////populate it
//foreach (var populator in _populators.Where(x => x.IsRegistered(indexName)))
// populator.Populate(index);
2018-05-31 22:03:12 +01:00
2018-11-30 12:22:23 +11:00
return Request.CreateResponse(HttpStatusCode.OK);
}
catch (Exception ex)
{
//ensure it's not listening
index.IndexOperationComplete -= Indexer_IndexOperationComplete;
Logger.Error<ExamineManagementController>(ex, "An error occurred rebuilding index");
var response = Request.CreateResponse(HttpStatusCode.Conflict);
response.Content =
new
StringContent($"The index could not be rebuilt at this time, most likely there is another thread currently writing to the index. Error: {ex}");
response.ReasonPhrase = "Could Not Rebuild";
return response;
}
2018-05-31 22:03:12 +01:00
}
private ExamineIndexModel CreateModel(IIndex index)
2018-05-31 22:03:12 +01:00
{
var indexName = index.Name;
2018-05-31 22:03:12 +01:00
if (!(index is IIndexDiagnostics indexDiag))
indexDiag = new GenericIndexDiagnostics(index);
2018-05-31 22:03:12 +01:00
var isHealth = indexDiag.IsHealthy();
var properties = new Dictionary<string, object>
2018-05-31 22:03:12 +01:00
{
[nameof(IIndexDiagnostics.DocumentCount)] = indexDiag.DocumentCount,
[nameof(IIndexDiagnostics.FieldCount)] = indexDiag.FieldCount,
};
foreach (var p in indexDiag.Metadata)
properties[p.Key] = p.Value;
2018-05-31 22:03:12 +01:00
var indexerModel = new ExamineIndexModel
{
Name = indexName,
HealthStatus = isHealth.Success ? (isHealth.Result ?? "Healthy") : (isHealth.Result ?? "Unhealthy"),
2018-11-30 12:22:23 +11:00
ProviderProperties = properties,
CanRebuild = _indexRebuilder.CanRebuild(index)
};
2018-05-31 22:03:12 +01:00
return indexerModel;
}
private HttpResponseMessage ValidateSearcher(string searcherName, out ISearcher searcher)
2018-05-31 22:03:12 +01:00
{
//try to get the searcher from the indexes
if (_examineManager.TryGetIndex(searcherName, out var index))
2018-05-31 22:03:12 +01:00
{
searcher = index.GetSearcher();
return Request.CreateResponse(HttpStatusCode.OK);
2018-05-31 22:03:12 +01:00
}
//if we didn't find anything try to find it by an explicitly declared searcher
if (_examineManager.TryGetSearcher(searcherName, out searcher))
return Request.CreateResponse(HttpStatusCode.OK);
2018-05-31 22:03:12 +01:00
var response1 = Request.CreateResponse(HttpStatusCode.BadRequest);
response1.Content = new StringContent($"No searcher found with name = {searcherName}");
response1.ReasonPhrase = "Searcher Not Found";
return response1;
}
private HttpResponseMessage ValidatePopulator(IIndex index)
2018-11-30 12:22:23 +11:00
{
if (_indexRebuilder.CanRebuild(index))
2018-11-30 12:22:23 +11:00
return Request.CreateResponse(HttpStatusCode.OK);
var response = Request.CreateResponse(HttpStatusCode.BadRequest);
response.Content = new StringContent($"The index {index.Name} cannot be rebuilt because it does not have an associated {typeof(IIndexPopulator)}");
2018-11-30 12:22:23 +11:00
response.ReasonPhrase = "Index cannot be rebuilt";
return response;
}
private HttpResponseMessage ValidateIndex(string indexName, out IIndex index)
2018-05-31 22:03:12 +01:00
{
2018-11-30 12:22:23 +11:00
index = null;
2018-05-31 22:03:12 +01:00
if (_examineManager.TryGetIndex(indexName, out index))
2018-05-31 22:03:12 +01:00
{
//return Ok!
return Request.CreateResponse(HttpStatusCode.OK);
}
var response = Request.CreateResponse(HttpStatusCode.BadRequest);
2018-11-30 12:22:23 +11:00
response.Content = new StringContent($"No index found with name = {indexName}");
response.ReasonPhrase = "Index Not Found";
2018-05-31 22:03:12 +01:00
return response;
}
private void Indexer_IndexOperationComplete(object sender, EventArgs e)
{
var indexer = (LuceneIndex)sender;
2018-05-31 22:03:12 +01:00
_logger.Debug<ExamineManagementController>("Logging operation completed for index {IndexName}", indexer.Name);
2018-05-31 22:03:12 +01:00
//ensure it's not listening anymore
indexer.IndexOperationComplete -= Indexer_IndexOperationComplete;
_logger
.Info<ExamineManagementController
>($"Rebuilding index '{indexer.Name}' done, {indexer.CommitCount} items committed (can differ from the number of items in the index)");
var cacheKey = "temp_indexing_op_" + indexer.Name;
2019-01-17 11:01:23 +01:00
_runtimeCacheProvider.Clear(cacheKey);
2018-05-31 22:03:12 +01:00
}
}
}