Example #1
0
        public static IExpression ParseExactPhrase(string expression, ModificationFlags flags)
        {
            if (expression == null)
            {
                return(null);
            }

            expression = expression.Trim();

            if (expression.Trim().Length == 0)
            {
                return(null);
            }

            if (expression.IndexOfAny(TextUtil.WhitespaceChars) != -1)
            {
                // Add quotes to ensure exact phrase
                if (!expression.StartsWith(LiteralTerm.Quote.ToString()))
                {
                    expression = LiteralTerm.Quote + expression;
                }

                if (!expression.EndsWith(LiteralTerm.Quote.ToString()))
                {
                    expression = expression + LiteralTerm.Quote;
                }
            }

            IExpression parsed = new LiteralTerm(expression);

            return(flags == ModificationFlags.None ? parsed : new ModifierExpression(parsed, flags));
        }
Example #2
0
        public ModifierExpression(IExpression expression, ModificationFlags flags)
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }
            if (flags == ModificationFlags.None)
            {
                throw new ArgumentException("The flags value must not be 'None'.", "flags");
            }

            _expression = expression;
            _flags      = flags;
        }
Example #3
0
        /// <summary>
        /// Parses a string expression into an IExpression object using the specified operator as default
        /// (ie. where two terms appear next to each other without an operator in-between).
        /// </summary>
        public static IExpression Parse(string expression, BinaryOperator defaultOp, ModificationFlags flags)
        {
            if (expression == null)
            {
                return(null);
            }

            // Trim and replace MS Word quotes with regular quotes.
            expression = expression.Trim().Replace('\x201C', '\"').Replace('\x201D', '\"');

            if (expression.Trim().Length == 0)
            {
                return(null);
            }

            var matches = TokenizeRegex.Matches(expression);

            if (matches.Count == 0)
            {
                // How can this happen?
                Debug.Assert(expression.Trim().Trim(LiteralTerm.Quote).Length == 0,
                             "Expression '" + expression + "' contained no tokens - is this valid?");
                return(null);
            }

            var currentTokens = Tokenise(matches, defaultOp, flags);
            var parsed        = ParseExpression(currentTokens, defaultOp);

            if (IsNotExpression(parsed))
            {
                throw new NotQueryNotSupportedException();
            }

            // Don't apply flags to exact searches
            return(parsed);
            // return (flags == ModificationFlags.None || (parsed is LiteralTerm && ((LiteralTerm)parsed).IsExact) ? parsed : new ModifierExpression(parsed, flags));
        }
Example #4
0
 /// <summary>
 /// Parses a string expression into an IExpression object using "AND" as the default operator.
 /// </summary>
 public static IExpression Parse(string expression, ModificationFlags flags)
 {
     return(Parse(expression, DefaultBinaryOperator, flags));
 }
Example #5
0
        private static List <object> Tokenise(MatchCollection matches, BinaryOperator defaultOp, ModificationFlags flags)
        {
            var contexts       = new Stack <List <object> >();
            var currentTokens  = new List <object>();
            var allowShingling = flags.IsFlagSet(ModificationFlags.AllowShingling);

            for (var i = 0; i < matches.Count; i++)
            {
                var   match = matches[i];
                Token token = GetToken(match.Value);

                switch (token)
                {
                case Token.Literal:
                    var matchValue = match.Value;

                    if (allowShingling && i + 1 < matches.Count)
                    {
                        //get succesive literal tokens

                        while (i + 1 < matches.Count)
                        {
                            var nextToken = GetToken(matches[i + 1].Value);

                            if (nextToken != Token.Literal)
                            {
                                break;
                            }

                            matchValue = matchValue + " " + matches[i + 1].Value;
                            i++;
                        }
                        currentTokens.Add(new ModifierExpression(new LiteralTerm(matchValue), flags));
                    }
                    else
                    {
                        currentTokens.Add(new LiteralTerm(matchValue));
                    }
                    break;

                case Token.And:
                    currentTokens.Add(BinaryOperator.And);
                    break;

                case Token.Or:
                    currentTokens.Add(BinaryOperator.Or);
                    break;

                case Token.Not:
                    currentTokens.Add(UnaryOperator.Not);
                    break;

                case Token.OpenBracket:
                    // Start of a sub-expression.

                    contexts.Push(currentTokens);
                    currentTokens = new List <object>();
                    break;

                case Token.CloseBracket:
                    // End of sub-expression - parse it.

                    if (contexts.Count == 0)
                    {
                        throw new InvalidQueryException(string.Format("The closing bracket at position"
                                                                      + " {0} does not match any opening bracket.", match.Index));
                    }

                    IExpression subQuery = ParseExpression(currentTokens, defaultOp);
                    currentTokens = contexts.Pop();

                    if (subQuery == null)
                    {
                        throw new InvalidQueryException("The expression contains an empty sub-expression (brackets"
                                                        + " with nothing inside them).");
                    }

                    currentTokens.Add(subQuery);
                    break;

                default:
                    Debug.Fail("Unexpected token: " + token);
                    break;
                }
            }

            if (contexts.Count > 0)
            {
                throw new InvalidQueryException("An opening bracket does not have a matching"
                                                + " closing bracket.");
            }

            return(currentTokens);
        }
Example #6
0
        private org.apache.lucene.search.Query CreateQuery(string queryString, ModificationFlags flags)
        {
            var expression = Expression.Parse(queryString, flags);

            return(expression.GetLuceneQuery("content", _queryAnalyzer));
        }