Adds a .Search(term) method to Model and Library, exposing the
default examine searcher / index
This commit is contained in:
@@ -30,6 +30,7 @@ namespace umbraco.MacroEngines
|
||||
public DynamicBackingItem(int Id)
|
||||
{
|
||||
NodeFactory.Node baseNode = new NodeFactory.Node(Id);
|
||||
|
||||
this.content = baseNode;
|
||||
this.Type = DynamicBackingItemType.Content;
|
||||
if (baseNode.Id == 0 && Id != 0)
|
||||
|
||||
@@ -18,6 +18,9 @@ using umbraco.cms.businesslogic.media;
|
||||
using umbraco.MacroEngines.Library;
|
||||
using umbraco.BusinessLogic.Utils;
|
||||
|
||||
using Examine;
|
||||
using Examine.SearchCriteria;
|
||||
using Examine.LuceneEngine.SearchCriteria;
|
||||
|
||||
namespace umbraco.MacroEngines
|
||||
{
|
||||
@@ -221,6 +224,33 @@ namespace umbraco.MacroEngines
|
||||
throw new NullReferenceException("DynamicNode wasn't initialized with an underlying NodeFactory.Node");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public DynamicNodeList Search(string term, bool useWildCards = true, string searchProvider = null)
|
||||
{
|
||||
var searcher = Examine.ExamineManager.Instance.DefaultSearchProvider;
|
||||
if(!string.IsNullOrEmpty(searchProvider))
|
||||
searcher = Examine.ExamineManager.Instance.SearchProviderCollection[searchProvider];
|
||||
|
||||
string luceneQuery = "+parentID:" + this.Id.ToString() + " +" + term.MultipleCharacterWildcard().Value;
|
||||
var crit = searcher.CreateSearchCriteria().RawQuery(luceneQuery);
|
||||
|
||||
return Search(crit, searcher);
|
||||
}
|
||||
|
||||
public DynamicNodeList Search(Examine.SearchCriteria.ISearchCriteria criteria, Examine.Providers.BaseSearchProvider searchProvider = null)
|
||||
{
|
||||
var s = Examine.ExamineManager.Instance.DefaultSearchProvider;
|
||||
if (searchProvider != null)
|
||||
s = searchProvider;
|
||||
|
||||
var results = s.Search(criteria);
|
||||
return ExamineSearchUtill.convertSearchResultToDynamicNode(results);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public bool HasProperty(string name)
|
||||
{
|
||||
if (n != null)
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Xml;
|
||||
|
||||
namespace umbraco.MacroEngines
|
||||
{
|
||||
class ExamineSearchUtill
|
||||
{
|
||||
|
||||
internal static DynamicNodeList convertSearchResultToDynamicNode(Examine.ISearchResults results)
|
||||
{
|
||||
DynamicNodeList list = new DynamicNodeList();
|
||||
XmlDocument xd = new XmlDocument();
|
||||
|
||||
foreach (var result in results.OrderByDescending(x => x.Score))
|
||||
{
|
||||
var item = new DynamicBackingItem(result.Id);
|
||||
if (item != null && item.Id != 0)
|
||||
{
|
||||
var node = (NodeFactory.Node)item.content;
|
||||
XmlNode examineResultXml = xmlHelper.addTextNode(xd, "examineScore", result.Score.ToString());
|
||||
node.Properties.Add(new NodeFactory.Property(examineResultXml));
|
||||
|
||||
list.Add(new DynamicNode(item));
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -128,6 +128,28 @@ namespace umbraco.MacroEngines.Library
|
||||
return MediaById(Ids.ToList());
|
||||
}
|
||||
|
||||
|
||||
public dynamic Search(string term, bool useWildCards = true, string searchProvider = null)
|
||||
{
|
||||
var searcher = Examine.ExamineManager.Instance.DefaultSearchProvider;
|
||||
if (!string.IsNullOrEmpty(searchProvider))
|
||||
searcher = Examine.ExamineManager.Instance.SearchProviderCollection[searchProvider];
|
||||
|
||||
var results = searcher.Search(term, useWildCards);
|
||||
return ExamineSearchUtill.convertSearchResultToDynamicNode(results);
|
||||
}
|
||||
|
||||
public dynamic Search(Examine.SearchCriteria.ISearchCriteria criteria, Examine.Providers.BaseSearchProvider searchProvider = null)
|
||||
{
|
||||
var s = Examine.ExamineManager.Instance.DefaultSearchProvider;
|
||||
if (searchProvider != null)
|
||||
s = searchProvider;
|
||||
|
||||
var results = s.Search(criteria);
|
||||
return ExamineSearchUtill.convertSearchResultToDynamicNode(results);
|
||||
}
|
||||
|
||||
|
||||
public T As<T>() where T : class
|
||||
{
|
||||
return (this as T);
|
||||
|
||||
@@ -91,6 +91,7 @@
|
||||
<Compile Include="RazorDynamicNode\DynamicNodeIdEqualityComparer.cs" />
|
||||
<Compile Include="RazorDynamicNode\DynamicNodeList.cs" />
|
||||
<Compile Include="RazorDynamicNode\ExamineBackedMedia.cs" />
|
||||
<Compile Include="RazorDynamicNode\ExamineSearchUtill.cs" />
|
||||
<Compile Include="RazorDynamicNode\Grouping.cs" />
|
||||
<Compile Include="RazorDynamicNode\HtmlTagWrapper.cs" />
|
||||
<Compile Include="RazorDynamicNode\HtmlTagWrapperBase.cs" />
|
||||
|
||||
Reference in New Issue
Block a user