public SearchQuery BuildQuery(string queryString, string constraint, SearchQueryParserType parserType, bool haveApiKey) {
     return _instance.BuildQuery(queryString, constraint, parserType, haveApiKey);
 }
 private Yield Search(string queryString, SetDiscriminator discriminator, string constraint, bool useCache, SearchQueryParserType parser, Result<XDoc> result) {
     if(parser == SearchQueryParserType.Filename) {
         useCache = false;
     }
     switch(discriminator.SortField) {
     case "date":
         if(useCache) {
             discriminator.SortField = "modified";
         }
         break;
     case "score":
         if(useCache) {
             discriminator.SortField = "rank";
         }
         break;
     case "wordcount":
     case "size":
         useCache = false;
         break;
     }
     var search = Resolve<ISearchBL>(DreamContext.Current);
     var query = search.BuildQuery(queryString, constraint, parser, DekiContext.Current.IsValidApiKeyInRequest);
     Result<XDoc> docResult;
     if(useCache) {
         yield return docResult = Coroutine.Invoke(CachedSearch, search, query, discriminator, false, (TrackingInfo)null, new Result<XDoc>());
     } else {
         yield return docResult = Coroutine.Invoke(UnCachedSearch, query, discriminator, new Result<XDoc>());
     }
     result.Return(RewriteRedirectsAndUris(docResult.Value));
 }
Esempio n. 3
0
 public SearchQuery BuildQuery(string queryString, string constraint, SearchQueryParserType parserType, bool haveApiKey)
 {
     return(_instance.BuildQuery(queryString, constraint, parserType, haveApiKey));
 }
Esempio n. 4
0
        //--- Methods ---
        public SearchQuery BuildQuery(string queryString, string constraint, SearchQueryParserType parserType, bool haveApiKey)
        {
            var nullConstraint    = string.IsNullOrEmpty(constraint);
            var constraintBuilder = new LuceneClauseBuilder();

            // must have at a queryString or a constraint to proceed
            if (string.IsNullOrEmpty(queryString) && nullConstraint)
            {
                return(SearchQuery.CreateEmpty());
            }

            // If an apikey isn't provided filter out user results by requiring results to be one of the other types
            if (parserType != SearchQueryParserType.Filename && !haveApiKey)
            {
                constraintBuilder.And("type:(wiki document image comment binary)");
                constraintBuilder.And(constraint);
            }
            else
            {
                constraintBuilder.And(constraint);
            }
            SearchQuery query = null;

            switch (parserType)
            {
            case SearchQueryParserType.BestGuess:
            case SearchQueryParserType.Term:
                var terms = _parser.GetQueryTerms(queryString, parserType != SearchQueryParserType.Term);
                if (terms == null)
                {
                    query = BuildLuceneQuery(constraintBuilder, queryString);
                    break;
                }
                var unprefixed   = (from t in terms where !t.HasFieldPrefix select t.Escaped).ToArray();
                var prefixed     = (from t in terms where t.HasFieldPrefix select t.Escaped).ToArray();
                var queryBuilder = new StringBuilder();
                if (unprefixed.Any())
                {
                    var termString = "(" + string.Join(" ", unprefixed) + ")";
                    var termQuery  = _settings.GetValue("search/termquery", TERM_QUERY);
                    try {
                        queryBuilder.AppendFormat(termQuery, termString);
                    } catch (FormatException e) {
                        throw new FormatException("query format provided in 'search/termquery' is invalid", e);
                    }
                    queryBuilder.Append(" ");
                }
                if (prefixed.Any())
                {
                    foreach (var p in prefixed)
                    {
                        queryBuilder.Append(p);
                        queryBuilder.Append(" ");
                    }
                }
                if (!haveApiKey)
                {
                    constraintBuilder.And("-namespace:\"template_talk\" -namespace:\"help\" -namespace:\"help_talk\"");
                }
                query = new SearchQuery(queryString, queryBuilder.ToString(), constraintBuilder, terms);
                break;

            case SearchQueryParserType.Filename:
                if (string.IsNullOrEmpty(queryString))
                {
                    query = SearchQuery.CreateEmpty();
                    break;
                }
                var term      = _parser.CreateEscapedTerm(queryString);
                var fileQuery = string.Format("filename:{0}* extension:.{0} description:{0}", term);
                constraintBuilder = new LuceneClauseBuilder();
                constraintBuilder.And("type:(document image binary)");
                constraintBuilder.And(constraint);
                query = new SearchQuery(queryString, fileQuery, constraintBuilder, null);
                break;

            case SearchQueryParserType.Lucene:
                query = BuildLuceneQuery(constraintBuilder, queryString);
                break;
            }
            return(query);
        }