2010-06-04 18:41:02 -07:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Orchard.Commands;
|
|
|
|
|
using Orchard.ContentManagement;
|
2011-01-05 18:13:56 -08:00
|
|
|
|
using Orchard.Indexing.Services;
|
2010-06-07 11:18:08 -07:00
|
|
|
|
using Orchard.Tasks.Indexing;
|
2013-03-05 11:13:33 -08:00
|
|
|
|
using Orchard.Utility.Extensions;
|
2010-06-04 18:41:02 -07:00
|
|
|
|
|
2010-06-17 16:21:29 -07:00
|
|
|
|
namespace Orchard.Indexing.Commands {
|
2010-06-04 18:41:02 -07:00
|
|
|
|
public class IndexingCommands : DefaultOrchardCommandHandler {
|
|
|
|
|
private readonly IIndexManager _indexManager;
|
2011-01-05 18:13:56 -08:00
|
|
|
|
private readonly IIndexingService _indexingService;
|
2010-06-07 11:18:08 -07:00
|
|
|
|
private readonly IIndexingTaskManager _indexingTaskManager;
|
|
|
|
|
private readonly IContentManager _contentManager;
|
2010-06-04 18:41:02 -07:00
|
|
|
|
|
|
|
|
|
public IndexingCommands(
|
2010-06-07 11:18:08 -07:00
|
|
|
|
IIndexManager indexManager,
|
2011-01-05 18:13:56 -08:00
|
|
|
|
IIndexingService indexingService,
|
2010-06-07 11:18:08 -07:00
|
|
|
|
IIndexingTaskManager indexingTaskManager,
|
|
|
|
|
IContentManager contentManager) {
|
|
|
|
|
_indexingTaskManager = indexingTaskManager;
|
|
|
|
|
_contentManager = contentManager;
|
2010-06-04 18:41:02 -07:00
|
|
|
|
_indexManager = indexManager;
|
2011-01-05 18:13:56 -08:00
|
|
|
|
_indexingService = indexingService;
|
2010-06-04 18:41:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[OrchardSwitch]
|
|
|
|
|
public string Query { get; set; }
|
|
|
|
|
|
2010-06-07 11:18:08 -07:00
|
|
|
|
[OrchardSwitch]
|
2011-01-02 20:39:04 -08:00
|
|
|
|
public string ContentItem { get; set; }
|
2010-06-07 11:18:08 -07:00
|
|
|
|
|
2013-03-05 11:13:33 -08:00
|
|
|
|
[CommandName("index create")]
|
|
|
|
|
[CommandHelp("index create <index>\r\n\t" + "Creates a new index with the specified name")]
|
|
|
|
|
public void Create(string index) {
|
|
|
|
|
if (!_indexManager.HasIndexProvider()) {
|
|
|
|
|
Context.Output.WriteLine(T("No index service available"));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(index)) {
|
|
|
|
|
Context.Output.WriteLine(T("Invalid index name."));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (index.ToSafeName() != index) {
|
|
|
|
|
Context.Output.WriteLine(T("Invalid index name."));
|
|
|
|
|
}
|
|
|
|
|
else {
|
2015-08-26 14:42:18 -07:00
|
|
|
|
var indexProvider = _indexManager.GetSearchIndexProvider();
|
|
|
|
|
if(indexProvider == null) {
|
|
|
|
|
Context.Output.WriteLine(T("New indexing service was found. Please enable a module like Lucene."));
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if (indexProvider.Exists(index)) {
|
|
|
|
|
Context.Output.WriteLine(T("The specified index already exists."));
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
_indexManager.GetSearchIndexProvider().CreateIndex(index);
|
|
|
|
|
Context.Output.WriteLine(T("New index has been created successfully."));
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-03-05 11:13:33 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-04 18:41:02 -07:00
|
|
|
|
[CommandName("index update")]
|
2013-03-05 11:13:33 -08:00
|
|
|
|
[CommandHelp("index update <index>\r\n\t" + "Updates the specified index")]
|
|
|
|
|
public void Update(string index) {
|
|
|
|
|
if (string.IsNullOrWhiteSpace(index)) {
|
|
|
|
|
Context.Output.WriteLine(T("Invalid index name."));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_indexingService.UpdateIndex(index);
|
2011-06-10 13:34:29 -07:00
|
|
|
|
Context.Output.WriteLine(T("Index is now being updated..."));
|
2010-06-04 18:41:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[CommandName("index rebuild")]
|
2013-03-05 11:13:33 -08:00
|
|
|
|
[CommandHelp("index rebuild <index> \r\n\t" + "Rebuilds the specified index")]
|
|
|
|
|
public void Rebuild(string index) {
|
|
|
|
|
if (string.IsNullOrWhiteSpace(index)) {
|
|
|
|
|
Context.Output.WriteLine(T("Invalid index name."));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_indexingService.RebuildIndex(index);
|
2011-06-10 13:34:29 -07:00
|
|
|
|
Context.Output.WriteLine(T("Index is now being rebuilt..."));
|
2010-06-04 18:41:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-05 11:13:33 -08:00
|
|
|
|
[CommandName("index query")]
|
|
|
|
|
[CommandHelp("index query <index> /Query:<query>\r\n\t" + "Searches the specified <query> terms in the specified index")]
|
2011-01-05 18:13:56 -08:00
|
|
|
|
[OrchardSwitches("Query")]
|
2013-03-05 11:13:33 -08:00
|
|
|
|
public void Search(string index) {
|
|
|
|
|
if (string.IsNullOrWhiteSpace(index)) {
|
|
|
|
|
Context.Output.WriteLine(T("Invalid index name."));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-04 18:41:02 -07:00
|
|
|
|
if ( !_indexManager.HasIndexProvider() ) {
|
2011-06-10 13:34:29 -07:00
|
|
|
|
Context.Output.WriteLine(T("No index available"));
|
|
|
|
|
return;
|
2010-06-04 18:41:02 -07:00
|
|
|
|
}
|
2013-03-05 11:13:33 -08:00
|
|
|
|
var searchBuilder = _indexManager.GetSearchIndexProvider().CreateSearchBuilder(index);
|
2011-01-05 18:13:56 -08:00
|
|
|
|
var results = searchBuilder.Parse( new [] {"body", "title"}, Query).Search();
|
2010-06-04 18:41:02 -07:00
|
|
|
|
|
|
|
|
|
Context.Output.WriteLine("{0} result{1}\r\n-----------------\r\n", results.Count(), results.Count() > 0 ? "s" : "");
|
|
|
|
|
|
|
|
|
|
Context.Output.WriteLine("┌──────────────────────────────────────────────────────────────┬────────┐");
|
|
|
|
|
Context.Output.WriteLine("│ {0} │ {1,6} │", "Title" + new string(' ', 60 - "Title".Length), "Score");
|
|
|
|
|
Context.Output.WriteLine("├──────────────────────────────────────────────────────────────┼────────┤");
|
|
|
|
|
foreach ( var searchHit in results ) {
|
2011-01-05 18:13:56 -08:00
|
|
|
|
var contentItem = _contentManager.Get(searchHit.ContentItemId);
|
2011-12-08 01:08:12 +00:00
|
|
|
|
var metadata = _contentManager.GetItemMetadata(contentItem);
|
|
|
|
|
var title = String.IsNullOrWhiteSpace(metadata.DisplayText) ? "- no title -" : metadata.DisplayText;
|
2010-07-07 15:02:45 -07:00
|
|
|
|
title = title.Substring(0, Math.Min(60, title.Length));
|
2011-01-05 18:13:56 -08:00
|
|
|
|
var score = Math.Round(searchHit.Score, 2).ToString();
|
2010-06-04 18:41:02 -07:00
|
|
|
|
Context.Output.WriteLine("│ {0} │ {1,6} │", title + new string(' ', 60 - title.Length), score);
|
|
|
|
|
}
|
|
|
|
|
Context.Output.WriteLine("└──────────────────────────────────────────────────────────────┴────────┘");
|
|
|
|
|
|
|
|
|
|
Context.Output.WriteLine();
|
2011-06-10 13:34:29 -07:00
|
|
|
|
Context.Output.WriteLine(T("End of search results"));
|
2010-06-04 18:41:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[CommandName("index stats")]
|
2013-03-05 11:13:33 -08:00
|
|
|
|
[CommandHelp("index stats <index>\r\n\t" + "Displays some statistics about the search index")]
|
2010-06-04 18:41:02 -07:00
|
|
|
|
[OrchardSwitches("IndexName")]
|
2013-03-05 11:13:33 -08:00
|
|
|
|
public void Stats(string index) {
|
|
|
|
|
if (string.IsNullOrWhiteSpace(index)) {
|
|
|
|
|
Context.Output.WriteLine(T("Invalid index name."));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-04 18:41:02 -07:00
|
|
|
|
if ( !_indexManager.HasIndexProvider() ) {
|
2011-06-10 13:34:29 -07:00
|
|
|
|
Context.Output.WriteLine(T("No index available"));
|
|
|
|
|
return;
|
2010-06-04 18:41:02 -07:00
|
|
|
|
}
|
2011-01-05 18:13:56 -08:00
|
|
|
|
|
2013-03-05 11:13:33 -08:00
|
|
|
|
Context.Output.WriteLine(T("Number of indexed documents: {0}", _indexManager.GetSearchIndexProvider().NumDocs(index)));
|
2010-06-04 18:41:02 -07:00
|
|
|
|
}
|
2010-06-07 11:18:08 -07:00
|
|
|
|
|
|
|
|
|
[CommandName("index refresh")]
|
2011-01-02 20:39:04 -08:00
|
|
|
|
[CommandHelp("index refresh /ContentItem:<content item id> \r\n\t" + "Refreshes the index for the specifed <content item id>")]
|
2010-06-07 11:18:08 -07:00
|
|
|
|
[OrchardSwitches("ContentItem")]
|
2011-06-10 13:34:29 -07:00
|
|
|
|
public void Refresh() {
|
2010-11-29 17:50:20 -08:00
|
|
|
|
int contentItemId;
|
2011-01-02 20:39:04 -08:00
|
|
|
|
if ( !int.TryParse(ContentItem, out contentItemId) ) {
|
2011-06-10 13:34:29 -07:00
|
|
|
|
Context.Output.WriteLine(T("Invalid content item id. Not an integer."));
|
|
|
|
|
return;
|
2010-06-07 11:18:08 -07:00
|
|
|
|
}
|
|
|
|
|
|
2010-11-29 17:50:20 -08:00
|
|
|
|
var contentItem = _contentManager.Get(contentItemId);
|
2010-06-07 11:18:08 -07:00
|
|
|
|
_indexingTaskManager.CreateUpdateIndexTask(contentItem);
|
|
|
|
|
|
2011-06-10 13:34:29 -07:00
|
|
|
|
Context.Output.WriteLine(T("Content Item marked for reindexing"));
|
2010-06-07 11:18:08 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[CommandName("index delete")]
|
2011-01-02 20:39:04 -08:00
|
|
|
|
[CommandHelp("index delete /ContentItem:<content item id>\r\n\t" + "Deletes the specifed <content item id> from the index")]
|
2010-06-07 11:18:08 -07:00
|
|
|
|
[OrchardSwitches("ContentItem")]
|
2011-06-10 13:34:29 -07:00
|
|
|
|
public void Delete() {
|
2011-01-02 20:39:04 -08:00
|
|
|
|
int contentItemId;
|
|
|
|
|
if(!int.TryParse(ContentItem, out contentItemId)) {
|
2011-06-10 13:34:29 -07:00
|
|
|
|
Context.Output.WriteLine(T("Invalid content item id. Not an integer."));
|
|
|
|
|
return;
|
2010-06-07 11:18:08 -07:00
|
|
|
|
}
|
|
|
|
|
|
2011-01-02 20:39:04 -08:00
|
|
|
|
var contentItem = _contentManager.Get(contentItemId);
|
2010-06-07 11:18:08 -07:00
|
|
|
|
_indexingTaskManager.CreateDeleteIndexTask(contentItem);
|
|
|
|
|
|
2011-06-10 13:34:29 -07:00
|
|
|
|
Context.Output.WriteLine(T("Content Item marked for deletion"));
|
2010-06-07 11:18:08 -07:00
|
|
|
|
}
|
|
|
|
|
|
2010-06-04 18:41:02 -07:00
|
|
|
|
}
|
|
|
|
|
}
|