/// <summary> /// Performs a lucene search using Examine. /// </summary> /// <param name="documentTypes">Array of document type aliases to search for.</param> /// <param name="searchGroups">A list of search groupings, if you have more than one group it will apply an and to the search criteria</param> /// <returns>Examine search results</returns> public Examine.ISearchResults SearchUsingExamine(string[] documentTypes, List <SearchGroup> searchGroups) { var searcher = ExamineManager.Instance.SearchProviderCollection["ExternalSearcher"]; ISearchCriteria searchCriteria = searcher.CreateSearchCriteria(UmbracoExamine.IndexTypes.Content); IBooleanOperation queryNodes = null; //only shows results for visible documents. queryNodes = searchCriteria.GroupedOr(new string[] { "umbracoNaviHide" }, "0", ""); if (documentTypes != null && documentTypes.Length > 0) { //only get results for documents of a certain type queryNodes = queryNodes.And().GroupedOr(new string[] { _docTypeAliasFieldName }, documentTypes); } if (searchGroups != null && searchGroups.Any()) { //in each search group it looks for a match where the specified fields contain any of the specified search terms //usually would only have 1 search group, unless you want to filter out further, i.e. using categories as well as search terms foreach (SearchGroup searchGroup in searchGroups) { queryNodes = queryNodes.And().GroupedOr(searchGroup.FieldsToSearchIn, searchGroup.SearchTerms); } } //return the results of the search return(ExamineManager.Instance.Search(queryNodes.Compile()));; }
internal ISearchResults TextSearchAllFields(string searchText, bool useWildcards, ISearchCriteria sc) { var splitSearch = searchText.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (useWildcards) { sc = sc.GroupedOr(GetSearchFields(), splitSearch.Select(x => new ExamineValue(Examineness.ComplexWildcard, x.MultipleCharacterWildcard().Value)).Cast <IExamineValue>().ToArray() ).Compile(); } else { sc = sc.GroupedOr(GetSearchFields(), splitSearch).Compile(); } return(Search(sc)); }
public static ISearchCriteria BuildQuery(string[] textFields, string searchString, ISearchCriteria criteria) { List<SearchTerm> Terms = new List<SearchTerm>(); if ((searchString.Contains(@"""")) && (searchString.Count(t => t == '"') % 2 == 0)) // even number of quotes, more than zero { Regex quoteRegex = new Regex(@""".+?"""); // look for any content between quotes foreach (Match item in quoteRegex.Matches(searchString)) { Terms.Add(new SearchTerm() { Term = item.Value.Replace('"', ' ').Trim(), TermType = SearchTermType.MultiWord }); searchString = Regex.Replace(searchString, item.Value, string.Empty); // remove them from search string for subsequent parsing } } List<string> singleTerms = new List<string>(); singleTerms = searchString.Split(' ').ToList(); singleTerms.ForEach(t => Terms.Add(new SearchTerm() { Term = t, TermType = SearchTermType.SingleWord })); foreach (SearchTerm t in Terms) { if (!string.IsNullOrEmpty(t.Term)) { switch (t.TermType) { case SearchTermType.SingleWord: criteria.GroupedOr(textFields, new IExamineValue[] { Examine.LuceneEngine.SearchCriteria.LuceneSearchExtensions.Fuzzy(t.Term, 0.4F) }); break; case SearchTermType.MultiWord: criteria.GroupedOr(textFields, new IExamineValue[] { Examine.LuceneEngine.SearchCriteria.LuceneSearchExtensions.Escape(t.Term) }); break; default: break; } } } return criteria; }
public static ISearchCriteria BuildQuery(string searchString, ISearchCriteria criteria, string[] textFields) { var terms = searchString.ToSearchTerms(); foreach (var t in terms.Where(t => !string.IsNullOrEmpty(t.Term))) { switch (t.SearchTermType) { case SearchTermType.SingleWord: criteria.GroupedOr( textFields, new[] { t.Term.Fuzzy() }); break; case SearchTermType.MultiWord: criteria.GroupedOr( textFields, new[] { t.Term.MultipleCharacterWildcard() }); break; } } return criteria; }
public static ISearchCriteria BuildQuery(string searchString, ISearchCriteria criteria, string[] textFields) { var terms = searchString.ToSearchTerms(); foreach (var t in terms.Where(t => !string.IsNullOrEmpty(t.Term))) { switch (t.SearchTermType) { case SearchTermType.SingleWord: criteria.GroupedOr( textFields, new[] { t.Term.Fuzzy() }); break; case SearchTermType.MultiWord: criteria.GroupedOr( textFields, new[] { t.Term.MultipleCharacterWildcard() }); break; } } return(criteria); }
private static IBooleanOperation GetFilter(string searchTerm, ISearchCriteria criteria) { IBooleanOperation result; // Handle searching for multiple terms var textFields = new[] { "header", "headerSmall", "manchet", "description", "bodyText1", "bodyText2" }; if (searchTerm.Contains(" ")) { string[] terms = searchTerm.Split(' '); result = criteria.GroupedOr(textFields, new[] { LuceneSearchExtensions.MultipleCharacterWildcard(terms[0]) }); for (int i = 1; i < terms.Length; i++) { result.Or().GroupedOr(textFields, new[] { LuceneSearchExtensions.MultipleCharacterWildcard(terms[i]) }); } } else { result = criteria.GroupedOr(new string[] { "header", "headerSmall", "manchet", "description", "bodyText1", "bodyText2" }, searchTerm.ToLower()).Or(). GroupedOr(new string[] { "header", "headerSmall", "manchet", "description", "bodyText1", "bodyText2" }, searchTerm.ToUpper()).Or(). GroupedOr(new string[] { "header", "headerSmall", "manchet", "description", "bodyText1", "bodyText2" }, searchTerm); } return(result); }
public static IEnumerable <SearchResult> PerformMemberSearch(string filter, IDictionary <string, string> filters, out int totalRecordCount, string memberType = "", int pageNumber = 0, int pageSize = 0, string orderBy = "", Direction orderDirection = Direction.Ascending) { var internalSearcher = ExamineManager.Instance.SearchProviderCollection[CoreConstants.Examine.InternalMemberSearcher]; ISearchCriteria criteria = internalSearcher.CreateSearchCriteria().RawQuery(" +__IndexType:member"); var basicFields = new List <string>() { "id", "_searchEmail", "email", "loginName" }; var filterParameters = filters.Where(q => !string.IsNullOrWhiteSpace(q.Value)); //build a lucene query if (string.IsNullOrWhiteSpace(filter) && !filterParameters.Any()) { // Generic get everything... criteria.RawQuery("a* b* c* d* e* f* g* h* i* j* k* l* m* n* o* p* q* r* s* t* u* v* w* x* y* z*"); } else { // the __nodeName will be boosted 10x without wildcards // then __nodeName will be matched normally with wildcards // the rest will be normal without wildcards if (!string.IsNullOrWhiteSpace(filter)) { var sb = new StringBuilder(); sb.Append("+("); //node name exactly boost x 10 sb.AppendFormat("__nodeName:{0}^10.0 ", filter.ToLower()); //node name normally with wildcards sb.AppendFormat(" __nodeName:{0}* ", filter.ToLower()); foreach (var field in basicFields) { //additional fields normally sb.AppendFormat("{0}:{1} ", field, filter); } sb.Append(")"); criteria.RawQuery(sb.ToString()); } // Now specific field searching. - these should be ANDed and grouped. foreach (var qs in filterParameters) { string alias = qs.Key; if (alias == Constants.Members.Groups) { // search on list with no commas. alias = $"_{alias}"; } else if (alias.StartsWith("f_")) { alias = qs.Key.Substring(2); } var values = filters[qs.Key].Split(','); if (values.Length > 0) { criteria.GroupedOr(new[] { alias }, values); } } } //must match index type if (!string.IsNullOrWhiteSpace(memberType)) { criteria.NodeTypeAlias(memberType); } //// Order the results //// Examine Sorting seems too unreliable, particularly on nodeName //if (orderDirection == Direction.Ascending) { // criteria.OrderBy(orderBy.ToLower() == "name" ? "nodeName" : "email"); //} else { // criteria.OrderByDescending(orderBy.ToLower() == "name" ? "nodeName" : "email"); //} var result = internalSearcher.Search(criteria); totalRecordCount = result.TotalItemCount; string orderFieldName; switch (orderBy.ToLower()) { case "isapproved": orderFieldName = CoreConstants.Conventions.Member.IsApproved; break; case "islockedout": orderFieldName = CoreConstants.Conventions.Member.IsLockedOut; break; case "name": orderFieldName = "nodeName"; break; default: orderFieldName = orderBy; break; } // Order the results var orderedResults = orderDirection == Direction.Ascending ? result.OrderBy(o => o.Fields[orderFieldName]) : result.OrderByDescending(o => o.Fields[orderFieldName]); if (pageSize > 0) { int skipCount = (pageNumber > 0 && pageSize > 0) ? Convert.ToInt32((pageNumber - 1) * pageSize) : 0; if (result.TotalItemCount < skipCount) { skipCount = result.TotalItemCount / pageSize; } return(orderedResults.Skip(skipCount).Take(pageSize)); } return(orderedResults); }
internal ISearchResults TextSearchAllFields(string searchText, bool useWildcards, ISearchCriteria sc) { var splitSearch = searchText.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (useWildcards) { sc = sc.GroupedOr(GetSearchFields(), splitSearch.Select(x => new ExamineValue(Examineness.ComplexWildcard, x.MultipleCharacterWildcard().Value)).Cast<IExamineValue>().ToArray() ).Compile(); } else { sc = sc.GroupedOr(GetSearchFields(), splitSearch).Compile(); } return Search(sc); }
public IBooleanOperation GroupedOr(ISearchCriteria criteria, IEnumerable <string> fields, params string[] query) { return(criteria.GroupedOr(fields, query)); }