mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
Added IContentManager.Search() method
Replaced usages with new declaration Created a NullSearchBuilder for default behavior when Indexing module is not activated --HG-- branch : dev
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using Lucene.Net.Documents;
|
||||
using Orchard.Utility.Extensions;
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ using Orchard.Indexing;
|
||||
|
||||
namespace Orchard.Search.Services {
|
||||
public interface ISearchService : IDependency {
|
||||
bool HasIndexToManage { get; }
|
||||
IPageOfItems<T> Query<T>(string query, int skip, int? take, bool filterCulture, string[] searchFields, Func<ISearchHit, T> shapeResult);
|
||||
}
|
||||
}
|
||||
@@ -1,27 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using Orchard.Collections;
|
||||
using Orchard.Indexing;
|
||||
using Orchard.Localization;
|
||||
using Orchard.Localization.Services;
|
||||
using Orchard.UI.Notify;
|
||||
using System.Web;
|
||||
using Orchard.ContentManagement;
|
||||
|
||||
namespace Orchard.Search.Services
|
||||
{
|
||||
public class SearchService : ISearchService
|
||||
{
|
||||
private const string SearchIndexName = "Search";
|
||||
private readonly IIndexManager _indexManager;
|
||||
private readonly IEnumerable<IIndexNotifierHandler> _indexNotifierHandlers;
|
||||
private readonly IContentManager _contentManager;
|
||||
private readonly ICultureManager _cultureManager;
|
||||
|
||||
public SearchService(IOrchardServices services, IIndexManager indexManager, IEnumerable<IIndexNotifierHandler> indexNotifierHandlers, ICultureManager cultureManager) {
|
||||
public SearchService(IOrchardServices services, IContentManager contentManager, ICultureManager cultureManager) {
|
||||
Services = services;
|
||||
_indexManager = indexManager;
|
||||
_indexNotifierHandlers = indexNotifierHandlers;
|
||||
_contentManager = contentManager;
|
||||
_cultureManager = cultureManager;
|
||||
T = NullLocalizer.Instance;
|
||||
}
|
||||
@@ -29,16 +24,12 @@ namespace Orchard.Search.Services
|
||||
public IOrchardServices Services { get; set; }
|
||||
public Localizer T { get; set; }
|
||||
|
||||
public bool HasIndexToManage {
|
||||
get { return _indexManager.HasIndexProvider(); }
|
||||
}
|
||||
|
||||
IPageOfItems<T> ISearchService.Query<T>(string query, int page, int? pageSize, bool filterCulture, string[] searchFields, Func<ISearchHit, T> shapeResult) {
|
||||
if (string.IsNullOrWhiteSpace(query) || !_indexManager.HasIndexProvider())
|
||||
|
||||
if (string.IsNullOrWhiteSpace(query))
|
||||
return null;
|
||||
|
||||
var searchBuilder = _indexManager.GetSearchIndexProvider().CreateSearchBuilder(SearchIndexName)
|
||||
.Parse(searchFields, query);
|
||||
var searchBuilder = _contentManager.Search().Parse(searchFields, query);
|
||||
|
||||
if ( filterCulture ) {
|
||||
var culture = _cultureManager.GetSiteCulture();
|
||||
|
||||
@@ -18,6 +18,7 @@ namespace Orchard.ContentManagement {
|
||||
private readonly IRepository<ContentItemVersionRecord> _contentItemVersionRepository;
|
||||
private readonly IContentDefinitionManager _contentDefinitionManager;
|
||||
private readonly Func<IContentManagerSession> _contentManagerSession;
|
||||
private readonly IIndexManager _indexManager;
|
||||
|
||||
public DefaultContentManager(
|
||||
IComponentContext context,
|
||||
@@ -25,13 +26,15 @@ namespace Orchard.ContentManagement {
|
||||
IRepository<ContentItemRecord> contentItemRepository,
|
||||
IRepository<ContentItemVersionRecord> contentItemVersionRepository,
|
||||
IContentDefinitionManager contentDefinitionManager,
|
||||
Func<IContentManagerSession> contentManagerSession) {
|
||||
Func<IContentManagerSession> contentManagerSession,
|
||||
IIndexManager indexManager) {
|
||||
_context = context;
|
||||
_contentTypeRepository = contentTypeRepository;
|
||||
_contentItemRepository = contentItemRepository;
|
||||
_contentItemVersionRepository = contentItemVersionRepository;
|
||||
_contentDefinitionManager = contentDefinitionManager;
|
||||
_contentManagerSession = contentManagerSession;
|
||||
_indexManager = indexManager;
|
||||
}
|
||||
|
||||
private IEnumerable<IContentHandler> _handlers;
|
||||
@@ -443,5 +446,11 @@ namespace Orchard.ContentManagement {
|
||||
handler.Indexed(indexContentContext);
|
||||
}
|
||||
}
|
||||
|
||||
public ISearchBuilder Search() {
|
||||
return _indexManager.HasIndexProvider()
|
||||
? _indexManager.GetSearchIndexProvider().CreateSearchBuilder("Search")
|
||||
: new NullSearchBuilder();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,8 @@ namespace Orchard.ContentManagement {
|
||||
void Remove(ContentItem contentItem);
|
||||
void Index(ContentItem contentItem, IDocumentIndex documentIndex);
|
||||
|
||||
ISearchBuilder Search();
|
||||
|
||||
void Flush();
|
||||
IContentQuery<ContentItem> Query();
|
||||
|
||||
|
||||
114
src/Orchard/Indexing/NullSearchBuilder.cs
Normal file
114
src/Orchard/Indexing/NullSearchBuilder.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Orchard.Indexing {
|
||||
public class NullSearchBuilder : ISearchBuilder {
|
||||
public ISearchBuilder Parse(string defaultField, string query) {
|
||||
return this;
|
||||
}
|
||||
|
||||
public ISearchBuilder Parse(string[] defaultFields, string query) {
|
||||
return this;
|
||||
}
|
||||
|
||||
public ISearchBuilder WithField(string field, bool value) {
|
||||
return this;
|
||||
}
|
||||
|
||||
public ISearchBuilder WithField(string field, DateTime value) {
|
||||
return this;
|
||||
}
|
||||
|
||||
public ISearchBuilder WithField(string field, string value) {
|
||||
return this;
|
||||
}
|
||||
|
||||
public ISearchBuilder WithField(string field, int value) {
|
||||
return this;
|
||||
}
|
||||
|
||||
public ISearchBuilder WithField(string field, float value) {
|
||||
return this;
|
||||
}
|
||||
|
||||
public ISearchBuilder WithinRange(string field, int min, int max) {
|
||||
return this;
|
||||
}
|
||||
|
||||
public ISearchBuilder WithinRange(string field, float min, float max) {
|
||||
return this;
|
||||
}
|
||||
|
||||
public ISearchBuilder WithinRange(string field, DateTime min, DateTime max) {
|
||||
return this;
|
||||
}
|
||||
|
||||
public ISearchBuilder WithinRange(string field, string min, string max) {
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Mark a clause as a mandatory match. By default all clauses are optional.
|
||||
/// </summary>
|
||||
public ISearchBuilder Mandatory() {
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Mark a clause as a forbidden match.
|
||||
/// </summary>
|
||||
public ISearchBuilder Forbidden() {
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Applied on string clauses, it removes the default Prefix mecanism. Like 'broadcast' won't
|
||||
/// return 'broadcasting'.
|
||||
/// </summary>
|
||||
public ISearchBuilder ExactMatch() {
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Apply a specific boost to a clause.
|
||||
/// </summary>
|
||||
/// <param name="weight">A value greater than zero, by which the score will be multiplied.
|
||||
/// If greater than 1, it will improve the weight of a clause</param>
|
||||
public ISearchBuilder Weighted(float weight) {
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Defines a clause as a filter, so that it only affect the results of the other clauses.
|
||||
/// For instance, if the other clauses returns nothing, even if this filter has matches the
|
||||
/// end result will be empty. It's like a two-pass query
|
||||
/// </summary>
|
||||
public ISearchBuilder AsFilter() {
|
||||
return this;
|
||||
}
|
||||
|
||||
public ISearchBuilder SortBy(string name) {
|
||||
return this;
|
||||
}
|
||||
|
||||
public ISearchBuilder Ascending() {
|
||||
return this;
|
||||
}
|
||||
|
||||
public ISearchBuilder Slice(int skip, int count) {
|
||||
return this;
|
||||
}
|
||||
|
||||
public IEnumerable<ISearchHit> Search() {
|
||||
return Enumerable.Empty<ISearchHit>();
|
||||
}
|
||||
public ISearchHit Get(int documentId) {
|
||||
return null;
|
||||
}
|
||||
public int Count() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -385,6 +385,7 @@
|
||||
<Compile Include="Environment\State\IShellStateManager.cs" />
|
||||
<Compile Include="Environment\State\ShellStateCoordinator.cs" />
|
||||
<Compile Include="IDependency.cs" />
|
||||
<Compile Include="Indexing\NullSearchBuilder.cs" />
|
||||
<Compile Include="Indexing\IIndexNotifierHandler.cs" />
|
||||
<Compile Include="Localization\Services\DefaultCultureManager.cs" />
|
||||
<Compile Include="Localization\Services\DefaultResourceManager.cs" />
|
||||
|
||||
Reference in New Issue
Block a user