public void Can_parse_simple_term_string()
 {
     Assert.AreEqual(new[] { "abc", "def" }, _parser.GetQueryTerms("abc def", false).ToEscaped());
 }
 public static string ToEscapedTerms(this string query)
 {
     return(string.Join(" ", _parser.GetQueryTerms(query, false).Select(x => x.Escaped).ToArray()));
 }
Esempio n. 3
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);
        }