mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-07 16:13:58 +08:00
Added index commands (update, rebuild, search, stats)
--HG-- branch : dev
This commit is contained in:
101
src/Orchard.Web/Core/Indexing/Commands/IndexingCommands.cs
Normal file
101
src/Orchard.Web/Core/Indexing/Commands/IndexingCommands.cs
Normal file
@@ -0,0 +1,101 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Orchard.Commands;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.Indexing;
|
||||
using Orchard.Security;
|
||||
|
||||
namespace Orchard.Core.Indexing.Commands {
|
||||
public class IndexingCommands : DefaultOrchardCommandHandler {
|
||||
private readonly IEnumerable<IIndexNotifierHandler> _indexNotifierHandlers;
|
||||
private readonly IIndexManager _indexManager;
|
||||
private const string SearchIndexName = "Search";
|
||||
|
||||
public IndexingCommands(
|
||||
IEnumerable<IIndexNotifierHandler> indexNotifierHandlers,
|
||||
IIndexManager indexManager) {
|
||||
_indexNotifierHandlers = indexNotifierHandlers;
|
||||
_indexManager = indexManager;
|
||||
}
|
||||
|
||||
[OrchardSwitch]
|
||||
public string IndexName { get; set; }
|
||||
|
||||
[OrchardSwitch]
|
||||
public string Query { get; set; }
|
||||
|
||||
[CommandName("index update")]
|
||||
[CommandHelp("index update [/IndexName:<index name>]\r\n\t" + "Updates the index with the specified <index name>, or the search index if not specified")]
|
||||
[OrchardSwitches("IndexName")]
|
||||
public string Update() {
|
||||
if ( !_indexManager.HasIndexProvider() ) {
|
||||
return "No index available";
|
||||
}
|
||||
|
||||
var indexName = String.IsNullOrWhiteSpace(IndexName) ? SearchIndexName : IndexName;
|
||||
foreach ( var handler in _indexNotifierHandlers ) {
|
||||
handler.UpdateIndex(indexName);
|
||||
}
|
||||
|
||||
return "Index is now being updated...";
|
||||
}
|
||||
|
||||
[CommandName("index rebuild")]
|
||||
[CommandHelp("index rebuild [/IndexName:<index name>]\r\n\t" + "Rebuilds the index with the specified <index name>, or the search index if not specified")]
|
||||
[OrchardSwitches("IndexName")]
|
||||
public string Rebuild() {
|
||||
if ( !_indexManager.HasIndexProvider() ) {
|
||||
return "No index available";
|
||||
}
|
||||
|
||||
var indexName = String.IsNullOrWhiteSpace(IndexName) ? SearchIndexName : IndexName;
|
||||
var searchProvider = _indexManager.GetSearchIndexProvider();
|
||||
if ( searchProvider.Exists(indexName) )
|
||||
searchProvider.DeleteIndex(indexName);
|
||||
|
||||
searchProvider.CreateIndex(indexName);
|
||||
return "Index is now being rebuilt...";
|
||||
}
|
||||
|
||||
[CommandName("index search")]
|
||||
[CommandHelp("index search /Query:<query> [/IndexName:<index name>]\r\n\t" + "Searches the specified <query> terms in the index with the specified <index name>, or in the search index if not specified")]
|
||||
[OrchardSwitches("Query,IndexName")]
|
||||
public string Search() {
|
||||
if ( !_indexManager.HasIndexProvider() ) {
|
||||
return "No index available";
|
||||
}
|
||||
var indexName = String.IsNullOrWhiteSpace(IndexName) ? SearchIndexName : IndexName;
|
||||
var searchBuilder = _indexManager.GetSearchIndexProvider().CreateSearchBuilder(indexName);
|
||||
var results = searchBuilder.WithField("body", Query).WithField("title", Query).Search();
|
||||
|
||||
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 ) {
|
||||
var title = searchHit.GetString("title");
|
||||
title = title.Substring(0, Math.Min(60, title.Length)) ?? "- no title -";
|
||||
var score = searchHit.Score;
|
||||
Context.Output.WriteLine("│ {0} │ {1,6} │", title + new string(' ', 60 - title.Length), score);
|
||||
}
|
||||
Context.Output.WriteLine("└──────────────────────────────────────────────────────────────┴────────┘");
|
||||
|
||||
Context.Output.WriteLine();
|
||||
return "End of search results";
|
||||
}
|
||||
|
||||
[CommandName("index stats")]
|
||||
[CommandHelp("index stats [/IndexName:<index name>]\r\n\t" + "Displays some statistics about the index with the specified <index name>, or in the search index if not specified")]
|
||||
[OrchardSwitches("IndexName")]
|
||||
public string Stats() {
|
||||
if ( !_indexManager.HasIndexProvider() ) {
|
||||
return "No index available";
|
||||
}
|
||||
var indexName = String.IsNullOrWhiteSpace(IndexName) ? SearchIndexName : IndexName;
|
||||
Context.Output.WriteLine("Number of indexed documents: {0}", _indexManager.GetSearchIndexProvider().NumDocs(indexName));
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
@@ -71,7 +71,7 @@ namespace Orchard.Core.Indexing.Lucene {
|
||||
}
|
||||
|
||||
public bool IsEmpty(string indexName) {
|
||||
if(!Exists(indexName)) {
|
||||
if ( !Exists(indexName) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -85,6 +85,21 @@ namespace Orchard.Core.Indexing.Lucene {
|
||||
}
|
||||
}
|
||||
|
||||
public int NumDocs(string indexName) {
|
||||
if ( !Exists(indexName) ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
var reader = IndexReader.Open(GetDirectory(indexName), true);
|
||||
|
||||
try {
|
||||
return reader.NumDocs();
|
||||
}
|
||||
finally {
|
||||
reader.Close();
|
||||
}
|
||||
}
|
||||
|
||||
public void CreateIndex(string indexName) {
|
||||
var writer = new IndexWriter(GetDirectory(indexName), _analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED);
|
||||
writer.Close();
|
||||
|
@@ -109,6 +109,7 @@
|
||||
<Compile Include="Feeds\Rss\RssResult.cs" />
|
||||
<Compile Include="HomePage\Controllers\HomeController.cs" />
|
||||
<Compile Include="HomePage\Routes.cs" />
|
||||
<Compile Include="Indexing\Commands\IndexingCommands.cs" />
|
||||
<Compile Include="Indexing\Lucene\DefaultIndexDocument.cs" />
|
||||
<Compile Include="Indexing\Lucene\DefaultIndexProvider.cs" />
|
||||
<Compile Include="Indexing\Lucene\DefaultSearchBuilder.cs" />
|
||||
|
@@ -23,6 +23,11 @@ namespace Orchard.Indexing {
|
||||
/// </summary>
|
||||
bool IsEmpty(string indexName);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of indexed documents
|
||||
/// </summary>
|
||||
int NumDocs(string indexName);
|
||||
|
||||
/// <summary>
|
||||
/// Creates an empty document
|
||||
/// </summary>
|
||||
|
Reference in New Issue
Block a user