protected Query CreateQueryFromTerm(string term) { Query query = null; if (!string.IsNullOrEmpty(term)) { // Single and ranged numeric value search on Id field if (term.Trim().StartsWith("Id")) { QueryParser parser = new NumericQueryParser(Lucene.Net.Util.Version.LUCENE_30, "Id", new PersonAnalyzer()); query = parser.Parse(term); } else if (term.Trim().StartsWith("IsRestrictedProfile")) { QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "IsRestrictedProfile", new PersonAnalyzer()); query = parser.Parse(term); } else { BooleanQuery topQuery = new BooleanQuery(); // manually construct a wildcard query for each search term for more results foreach (string termToken in term.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)) { BooleanQuery termTokenQuery = new BooleanQuery(); foreach (string field in new string[] { "Name", "MilitaryIDNumber", "Rank", "Function" }) { string txt = termToken.ToLower(); // perform filtering of non-alphanumeric chars for MilitaryIDNumber field (this would also // be done via PersonAnalyzer, if we were using a QueryParser to construct the Query). if (string.Equals("MilitaryIDNumber", field)) { txt = new string(txt.ToCharArray() .Where(x => char.IsLetterOrDigit(x)) .ToArray()); } // default similarity of 0.5 = max edit distance of 2 if (!string.Equals("MilitaryIDNumber", field)) { termTokenQuery.Add(new FuzzyQuery(new Term(field, txt)), Occur.SHOULD); } txt = "*" + txt + "*"; termTokenQuery.Add(new WildcardQuery(new Term(field, txt)), Occur.SHOULD); } topQuery.Add(termTokenQuery, Occur.SHOULD); } query = topQuery; } log.Debug("Search query: " + query.ToString()); } return(query); }
// See UnitIndexer.cs for details on how fields were originally indexed. public IList <LuceneSearchResult> Search(string term, int numResults) { using (SearcherManager manager = new SearcherManager(UnitIndexWriterSingleton.Instance)) { this.searcher = manager.Acquire().Searcher; QueryParser parser; if (!string.IsNullOrEmpty(term) && term.Trim().StartsWith("Id")) { // Single and ranged numeric value search on Id field parser = new NumericQueryParser(Lucene.Net.Util.Version.LUCENE_30, "Id", new PersonAnalyzer()); } else { // General search across text fields parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30, new string[] { "Name", "ParentNameChange", "ChildNameChange", "BackgroundInformation", "Organization" }, new PersonAnalyzer()); // We maintain OR as default for maximum results parser.DefaultOperator = QueryParser.Operator.OR; if (!string.IsNullOrEmpty(term)) { if (!term.Contains(':')) { // Edit user's search string and add wildcards. term = string.Join(" ", term.Split(new string[] { " " }, System.StringSplitOptions.RemoveEmptyEntries) .Select(x => "*" + x + "*") .ToArray() ); } } } parser.AllowLeadingWildcard = true; try { Query query = parser.Parse(term); log.Debug("Search query: " + query.ToString()); this.topDocs = this.searcher.Search(query, numResults); return(TransformTopDocs()); } catch (ParseException e) { log.Error("Encountered problem parsing the search term: " + term, e); return(new List <LuceneSearchResult>()); } } }
public IList <LuceneSearchResult> Search(string term, int numResults, AdminUser user, string sortField, bool descending) { using (SearcherManager manager = new SearcherManager(RequestIndexWriterSingleton.Instance)) { this.searcher = manager.Acquire().Searcher; Query query = null; if (string.IsNullOrEmpty(term)) { query = new MatchAllDocsQuery(); } else { QueryParser parser; if (term.Trim().StartsWith("Id")) { // Single and ranged numeric value search on Id field parser = new NumericQueryParser(Lucene.Net.Util.Version.LUCENE_30, "Id", new KeywordAnalyzer()); } else { // General search across text fields parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30, new string[] { "ReferenceNumber", "RequestName", "RequestEntity", "RequestType", "CurrentStatus" }, new LowerCaseAnalyzer()); parser.DefaultOperator = QueryParser.Operator.AND; if (!string.IsNullOrEmpty(term)) { if (!term.Contains(':')) { // Edit user's search string and add wildcards. term = string.Join(" ", term.Split(new string[] { " " }, System.StringSplitOptions.RemoveEmptyEntries) .Select(x => "*" + x + "*") .ToArray() ); } } } parser.AllowLeadingWildcard = true; try { query = parser.Parse(term); } catch (ParseException e) { log.Error("Encountered problem parsing the search term: " + term, e); return(new List <LuceneSearchResult>()); } } // when given a user, assumes results must be filtered if (user != null && user.GetRequestEntity() != null) { BooleanQuery bq = new BooleanQuery(); bq.Add(query, Occur.MUST); BooleanQuery bq2 = new BooleanQuery(); bq2.Add(new TermQuery(new Term("RequestEntity", user.GetRequestEntity().RequestEntityName)), Occur.SHOULD); bq2.Add(new TermQuery(new Term("CreatorRequestEntity", user.GetRequestEntity().RequestEntityName)), Occur.SHOULD); bq2.Add(new TermQuery(new Term("Creator", user.UserID)), Occur.SHOULD); bq.Add(bq2, Occur.MUST); query = bq; } log.Debug("Search query: " + query.ToString()); this.PerformSearch(query, numResults, sortField, descending); return(TransformTopDocs()); } }
public IList <LuceneSearchResult> Search(string term, DateTime?start, DateTime?end, int numResults, string sortField, bool descending) { using (SearcherManager manager = new SearcherManager(EventIndexWriterSingleton.Instance)) { this.searcher = manager.Acquire().Searcher; QueryParser parser; if (!string.IsNullOrEmpty(term) && term.Trim().StartsWith("Id")) { // Single and ranged numeric value search on Id field parser = new NumericQueryParser(Lucene.Net.Util.Version.LUCENE_30, "Id", new KeywordAnalyzer()); } else { // General search across text fields parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30, new string[] { "Name", "JhroCaseNumber", "Violation", "NarrativeEn", "NarrativeFr", "Location", "Notes", "StartDateDisplay", "EndDateDisplay" }, new LowerCaseAnalyzer()); parser.DefaultOperator = QueryParser.Operator.AND; if (!string.IsNullOrEmpty(term)) { if (!term.Contains(':')) { // Edit user's search string and add wildcards. term = string.Join(" ", term.Split(new string[] { " " }, System.StringSplitOptions.RemoveEmptyEntries) .Select(x => "*" + x + "*") .ToArray() ); } } } parser.AllowLeadingWildcard = true; try { Query query = null; if (!string.IsNullOrEmpty(term)) { query = parser.Parse(term); } if (start.HasValue || end.HasValue) { long?min = null; if (start.HasValue) { min = start.Value.Ticks; } long?max = null; if (end.HasValue) { max = end.Value.Ticks; } BooleanQuery bq = new BooleanQuery(); if (query != null) { bq.Add(query, Occur.MUST); } BooleanQuery bq2 = new BooleanQuery(); bq2.Add(NumericRangeQuery.NewLongRange("StartDateSearch", min, max, true, true), Occur.SHOULD); bq2.Add(NumericRangeQuery.NewLongRange("EndDateSearch", min, max, true, true), Occur.SHOULD); bq.Add(bq2, Occur.MUST); query = bq; } log.Debug("Search query: " + query.ToString()); this.PerformSearch(query, numResults, sortField, descending); return(TransformTopDocs()); } catch (ParseException e) { log.Error("Encountered problem parsing the search term: " + term, e); return(new List <LuceneSearchResult>()); } } }