Ejemplo n.º 1
0
        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());
            }
        }