using System; using System.Collections.Generic; using System.Linq; using System.Security; using System.Xml.Linq; using System.Xml.XPath; using Examine; using Examine.LuceneEngine.Providers; using Examine.LuceneEngine.SearchCriteria; using Examine.SearchCriteria; using Examine.Providers; using Umbraco.Core.Macros; using UmbracoExamine.DataServices; namespace UmbracoExamine { /// /// Methods to support Umbraco XSLT extensions. /// /// /// XSLT extensions will ONLY work for provider that have a base class of BaseUmbracoIndexer /// [XsltExtension("Examine")] public class XsltExtensions { /// /// Uses the provider specified to search, returning an XPathNodeIterator /// /// /// /// /// /// internal static XPathNodeIterator Search(string searchText, bool useWildcards, BaseSearchProvider provider, string indexType) { if (provider == null) throw new ArgumentNullException("provider"); var results = provider.Search(searchText, useWildcards, indexType); return GetResultsAsXml(results); } /// /// Uses the provider specified to search, returning an XPathNodeIterator /// /// The search text. /// if set to true [use wildcards]. /// Name of the provider. /// Type of the index. /// public static XPathNodeIterator Search(string searchText, bool useWildcards, string providerName, string indexType) { var provider = ExamineManager.Instance.SearchProviderCollection[providerName]; return Search(searchText, useWildcards, provider, indexType); } /// /// Uses the provider specified to search, returning an XPathNodeIterator /// /// /// /// /// public static XPathNodeIterator Search(string searchText, bool useWildcards, string providerName) { return Search(searchText, useWildcards, providerName, string.Empty); } /// /// Uses the default provider specified to search, returning an XPathNodeIterator /// /// The search query /// Enable a wildcard search query /// A node-set of the search results public static XPathNodeIterator Search(string searchText, bool useWildcards) { return Search(searchText, useWildcards, ExamineManager.Instance.DefaultSearchProvider.Name); } /// /// Uses the default provider specified to search, returning an XPathNodeIterator /// /// The search query /// A node-set of the search results public static XPathNodeIterator Search(string searchText) { return Search(searchText, true); } /// /// Will perform a search against the media index type only /// /// /// /// /// public static XPathNodeIterator SearchMediaOnly(string searchText, bool useWildcards, string providerName) { return Search(searchText, useWildcards, providerName, IndexTypes.Media); } /// /// Will perform a search against the media index type only /// /// /// /// public static XPathNodeIterator SearchMediaOnly(string searchText, bool useWildcards) { return SearchMediaOnly(searchText, useWildcards, ExamineManager.Instance.DefaultSearchProvider.Name); } /// /// Will perform a search against the media index type only /// /// /// public static XPathNodeIterator SearchMediaOnly(string searchText) { return SearchMediaOnly(searchText, true); } /// /// Searches the member only. /// /// The search text. /// if set to true [use wildcards]. /// Name of the provider. /// public static XPathNodeIterator SearchMemberOnly(string searchText, bool useWildcards, string providerName) { return Search(searchText, useWildcards, providerName, IndexTypes.Member); } /// /// Searches the member only. /// /// The search text. /// if set to true [use wildcards]. /// public static XPathNodeIterator SearchMemberOnly(string searchText, bool useWildcards) { return SearchMemberOnly(searchText, useWildcards, ExamineManager.Instance.DefaultSearchProvider.Name); } /// /// Searches the member only. /// /// The search text. /// public static XPathNodeIterator SearchMemberOnly(string searchText) { return SearchMemberOnly(searchText, true); } /// /// Will perform a search against the content index type only /// /// /// /// /// public static XPathNodeIterator SearchContentOnly(string searchText, bool useWildcards, string providerName) { return Search(searchText, useWildcards, providerName, IndexTypes.Content); } /// /// Will perform a search against the content index type only /// /// /// /// public static XPathNodeIterator SearchContentOnly(string searchText, bool useWildcards) { return SearchContentOnly(searchText, useWildcards, ExamineManager.Instance.DefaultSearchProvider.Name); } /// /// Will perform a search against the content index type only /// /// /// public static XPathNodeIterator SearchContentOnly(string searchText) { return SearchContentOnly(searchText, true); } /// /// Gets the results as XML. /// /// The results. /// private static XPathNodeIterator GetResultsAsXml(ISearchResults results) { // create the XDocument XDocument doc = new XDocument(); // check there are any search results if (results.TotalItemCount > 0) { // create the root element XElement root = new XElement("nodes"); // iterate through the search results foreach (SearchResult result in results) { // create a new element XElement node = new XElement("node"); // create the @id attribute XAttribute nodeId = new XAttribute("id", result.Id); // create the @score attribute XAttribute nodeScore = new XAttribute("score", result.Score); // add the content node.Add(nodeId, nodeScore); foreach (KeyValuePair field in result.Fields) { // create a new element XElement data = new XElement("data"); // create the @alias attribute XAttribute alias = new XAttribute("alias", field.Key); // assign the value to a CDATA section XCData value = new XCData(field.Value); // append the content data.Add(alias, value); // append the element node.Add(data); } // add the node root.Add(node); } // add the root node doc.Add(root); } else { doc.Add(new XElement("error", "There were no search results.")); } return doc.CreateNavigator().Select("/"); } } }