Esempio n. 1
0
        public override Query ToQuery(LuceneASTQueryConfiguration configuration)
        {
            configuration.FieldName = FieldName;
            var res = Node.ToGroupFieldQuery(configuration);

            return(res);
        }
Esempio n. 2
0
        public override Query ToGroupFieldQuery(LuceneASTQueryConfiguration configuration)
        {
            var query = Node.ToQuery(configuration);

            query.Boost = GetBoost();
            return(query);
        }
Esempio n. 3
0
 protected static Occur GetDefaultOccurFromConfiguration(LuceneASTQueryConfiguration configuration)
 {
     if (configuration.DefaultOperator == QueryOperator.And)
     {
         return(Occur.MUST);
     }
     return(Occur.SHOULD);
 }
Esempio n. 4
0
        public override Query ToQuery(LuceneASTQueryConfiguration configuration)
        {
            var query = new BooleanQuery();

            Node.AddQueryToBooleanQuery(query, configuration);
            query.Boost = Boost == null ? 1 : float.Parse(Boost);
            return(query);
        }
Esempio n. 5
0
        public virtual void AddQueryToBooleanQuery(BooleanQuery b, LuceneASTQueryConfiguration configuration, Occur o)
        {
            var query = ToQuery(configuration);

            if (query != null)
            {
                b.Add(query, o);
            }
        }
Esempio n. 6
0
        public override Query ToQuery(LuceneASTQueryConfiguration configuration)
        {
            var query = new BooleanQuery();

            //Here the Occur.MUST is ignored we will use the default occur in children nodes
            Node.AddQueryToBooleanQuery(query, configuration, Occur.MUST, true);
            query.Boost = GetBoost();
            return(query);
        }
Esempio n. 7
0
        public override Query ToQuery(LuceneASTQueryConfiguration configuration)
        {
            var query = new BooleanQuery();
            var occur = configuration.DefaultOperator == QueryOperator.And ? Occur.MUST : Occur.SHOULD;

            //if the node is boolean query than it is going to ignore this value.
            Node.AddQueryToBooleanQuery(query, configuration, occur);
            query.Boost = GetBoost();
            return(query);
        }
Esempio n. 8
0
        public override Query ToQuery(LuceneASTQueryConfiguration configuration)
        {
            configuration.FieldName = FieldName;
            var matchList = new List <string>();

            foreach (var match in Matches)
            {
                matchList.AddRange(match.GetAnalyzedTerm(configuration));
            }
            return(new TermsMatchQuery(FieldName, matchList));
        }
Esempio n. 9
0
        public override Query ToQuery(LuceneASTQueryConfiguration configuration)
        {
            var  query = new BooleanQuery();
            bool hasExplicitPrefix;

            switch (Op)
            {
            case Operator.AND:
                LeftNode.AddQueryToBooleanQuery(query, configuration, PrefixToOccurance(LeftNode, Occur.MUST, out hasExplicitPrefix));
                RightNode.AddQueryToBooleanQuery(query, configuration, PrefixToOccurance(RightNode, Occur.MUST, out hasExplicitPrefix));
                break;

            case Operator.OR:
                LeftNode.AddQueryToBooleanQuery(query, configuration, PrefixToOccurance(LeftNode, Occur.SHOULD, out hasExplicitPrefix));
                RightNode.AddQueryToBooleanQuery(query, configuration, PrefixToOccurance(RightNode, Occur.SHOULD, out hasExplicitPrefix), !hasExplicitPrefix);
                break;

            case Operator.NOT:
                query.Add(LeftNode.ToQuery(configuration), Occur.MUST_NOT);
                break;

            case Operator.Implicit:
                switch (configuration.DefaultOperator)
                {
                case QueryOperator.Or:
                    LeftNode.AddQueryToBooleanQuery(query, configuration, PrefixToOccurance(LeftNode, Occur.SHOULD, out hasExplicitPrefix));
                    RightNode.AddQueryToBooleanQuery(query, configuration, PrefixToOccurance(RightNode, Occur.SHOULD, out hasExplicitPrefix), !hasExplicitPrefix);
                    break;

                case QueryOperator.And:
                    LeftNode.AddQueryToBooleanQuery(query, configuration, PrefixToOccurance(LeftNode, Occur.MUST, out hasExplicitPrefix));
                    RightNode.AddQueryToBooleanQuery(query, configuration, PrefixToOccurance(RightNode, Occur.MUST, out hasExplicitPrefix));
                    break;

                default:
                    throw new ArgumentOutOfRangeException("defaultOperator");
                }
                break;

            case Operator.INTERSECT:
                LeftNode.AddQueryToBooleanQuery(query, configuration, PrefixToOccurance(LeftNode, Occur.MUST, out hasExplicitPrefix));
                RightNode.AddQueryToBooleanQuery(query, configuration, PrefixToOccurance(RightNode, Occur.MUST, out hasExplicitPrefix));
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
            return(query);
        }
Esempio n. 10
0
        public override Query ToQuery(LuceneASTQueryConfiguration configuration)
        {
            var query = new BooleanQuery();

            switch (Op)
            {
            case Operator.AND:
                LeftNode.AddQueryToBooleanQuery(query, configuration, LeftNode.Prefix == PrefixOperator.Minus ? Occur.MUST_NOT : Occur.MUST);
                RightNode.AddQueryToBooleanQuery(query, configuration, RightNode.Prefix == PrefixOperator.Minus ? Occur.MUST_NOT : Occur.MUST);
                break;

            case Operator.OR:
                LeftNode.AddQueryToBooleanQuery(query, configuration, LeftNode.Prefix == PrefixOperator.Plus ? Occur.MUST : Occur.SHOULD);
                RightNode.AddQueryToBooleanQuery(query, configuration, RightNode.Prefix == PrefixOperator.Plus ? Occur.MUST : Occur.SHOULD);
                break;

            case Operator.NOT:
                query.Add(LeftNode.ToQuery(configuration), Occur.MUST_NOT);
                break;

            case Operator.Implicit:
                switch (configuration.DefaultOperator)
                {
                case QueryOperator.Or:
                    LeftNode.AddQueryToBooleanQuery(query, configuration, LeftNode.Prefix == PrefixOperator.Plus ? Occur.MUST : Occur.SHOULD);
                    RightNode.AddQueryToBooleanQuery(query, configuration, RightNode.Prefix == PrefixOperator.Plus ? Occur.MUST : Occur.SHOULD);
                    break;

                case QueryOperator.And:
                    LeftNode.AddQueryToBooleanQuery(query, configuration, LeftNode.Prefix == PrefixOperator.Minus ? Occur.MUST_NOT : Occur.MUST);
                    RightNode.AddQueryToBooleanQuery(query, configuration, RightNode.Prefix == PrefixOperator.Minus ? Occur.MUST_NOT : Occur.MUST);
                    break;

                default:
                    throw new ArgumentOutOfRangeException("defaultOperator");
                }
                break;

            case Operator.INTERSECT:
                LeftNode.AddQueryToBooleanQuery(query, configuration, LeftNode.Prefix == PrefixOperator.Minus ? Occur.MUST_NOT : Occur.MUST);
                RightNode.AddQueryToBooleanQuery(query, configuration, RightNode.Prefix == PrefixOperator.Minus ? Occur.MUST_NOT : Occur.MUST);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
            return(query);
        }
Esempio n. 11
0
        public override void AddQueryToBooleanQuery(BooleanQuery query, LuceneASTQueryConfiguration configuration, Occur o, bool letChildrenDecide = false)
        {
            bool hasExplicitPrefix;

            switch (Op)
            {
            case Operator.AND:
                LeftNode.AddQueryToBooleanQuery(query, configuration, PrefixToOccurance(LeftNode, letChildrenDecide? Occur.MUST:o, out hasExplicitPrefix));
                RightNode.AddQueryToBooleanQuery(query, configuration, PrefixToOccurance(RightNode, Occur.MUST, out hasExplicitPrefix));
                break;

            case Operator.OR:
                LeftNode.AddQueryToBooleanQuery(query, configuration, PrefixToOccurance(LeftNode, letChildrenDecide ? Occur.SHOULD : o, out hasExplicitPrefix));
                RightNode.AddQueryToBooleanQuery(query, configuration, PrefixToOccurance(RightNode, Occur.SHOULD, out hasExplicitPrefix), !hasExplicitPrefix);
                break;

            case Operator.NOT:
                query.Add(LeftNode.ToQuery(configuration), Occur.MUST_NOT);
                break;

            case Operator.Implicit:
                switch (configuration.DefaultOperator)
                {
                case QueryOperator.Or:
                    LeftNode.AddQueryToBooleanQuery(query, configuration, PrefixToOccurance(LeftNode, letChildrenDecide ? Occur.SHOULD : o, out hasExplicitPrefix));
                    RightNode.AddQueryToBooleanQuery(query, configuration, PrefixToOccurance(RightNode, Occur.SHOULD, out hasExplicitPrefix), !hasExplicitPrefix);
                    break;

                case QueryOperator.And:
                    LeftNode.AddQueryToBooleanQuery(query, configuration, PrefixToOccurance(LeftNode, letChildrenDecide ? Occur.MUST : o, out hasExplicitPrefix));
                    RightNode.AddQueryToBooleanQuery(query, configuration, PrefixToOccurance(RightNode, Occur.MUST, out hasExplicitPrefix));
                    break;

                default:
                    throw new ArgumentOutOfRangeException("defaultOperator");
                }
                break;

            case Operator.INTERSECT:
                LeftNode.AddQueryToBooleanQuery(query, configuration, PrefixToOccurance(LeftNode, letChildrenDecide ? Occur.MUST : o, out hasExplicitPrefix));
                RightNode.AddQueryToBooleanQuery(query, configuration, PrefixToOccurance(RightNode, Occur.MUST, out hasExplicitPrefix));
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Esempio n. 12
0
        public IEnumerable <string> GetAnalyzedTerm(LuceneASTQueryConfiguration configuration)
        {
            switch (Type)
            {
            case TermType.Quoted:
            case TermType.UnQuoted:
                var tokenStream = configuration.Analayzer.ReusableTokenStream(configuration.FieldName, new StringReader(Term));
                while (tokenStream.IncrementToken())
                {
                    var attribute = (TermAttribute)tokenStream.GetAttribute <ITermAttribute>();
                    yield return(attribute.Term);
                }
                break;

            case TermType.QuotedWildcard:
            case TermType.WildCardTerm:
            case TermType.PrefixTerm:
                yield return(GetWildcardTerm(configuration).Text);

                break;

            case TermType.Float:
            case TermType.Double:
            case TermType.Hex:
            case TermType.DateTime:
            case TermType.Int:
            case TermType.Long:
            case TermType.Null:
                yield return(Term);

                break;

            case TermType.UnAnalyzed:
                yield return(Term.Substring(2, Term.Length - 4));

                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Esempio n. 13
0
        public override void AddQueryToBooleanQuery(BooleanQuery query, LuceneASTQueryConfiguration configuration, Occur o)
        {
            switch (Op)
            {
            case Operator.AND:
                LeftNode.AddQueryToBooleanQuery(query, configuration, PrefixToOccurance(LeftNode, Occur.MUST));
                RightNode.AddQueryToBooleanQuery(query, configuration, PrefixToOccurance(RightNode, Occur.MUST));
                break;

            case Operator.OR:
                LeftNode.AddQueryToBooleanQuery(query, configuration, PrefixToOccurance(LeftNode, Occur.SHOULD));
                RightNode.AddQueryToBooleanQuery(query, configuration, PrefixToOccurance(RightNode, Occur.SHOULD));
                break;

            case Operator.Implicit:
                switch (configuration.DefaultOperator)
                {
                case QueryOperator.Or:
                    LeftNode.AddQueryToBooleanQuery(query, configuration, PrefixToOccurance(LeftNode, Occur.SHOULD));
                    RightNode.AddQueryToBooleanQuery(query, configuration, PrefixToOccurance(RightNode, Occur.SHOULD));
                    break;

                case QueryOperator.And:
                    LeftNode.AddQueryToBooleanQuery(query, configuration, PrefixToOccurance(LeftNode, Occur.MUST));
                    RightNode.AddQueryToBooleanQuery(query, configuration, PrefixToOccurance(RightNode, Occur.MUST));
                    break;

                default:
                    throw new ArgumentOutOfRangeException("defaultOperator");
                }
                break;

            case Operator.INTERSECT:
                LeftNode.AddQueryToBooleanQuery(query, configuration, PrefixToOccurance(LeftNode, Occur.MUST));
                RightNode.AddQueryToBooleanQuery(query, configuration, PrefixToOccurance(RightNode, Occur.MUST));
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Esempio n. 14
0
 public override Query ToQuery(LuceneASTQueryConfiguration configuration)
 {
     // For numeric values { NUll TO <number> } should be [ <min value> TO <number>} but not for string values.
     OverideInclusive();
     if (RangeMin.Type == TermLuceneASTNode.TermType.Float || RangeMax.Type == TermLuceneASTNode.TermType.Float)
     {
         //Need to handle NULL values...
         var min = (RangeMin.Type == TermLuceneASTNode.TermType.Null || RangeMin.Term == "*") ? float.MinValue : float.Parse(RangeMin.Term.Substring(2));
         var max = (RangeMax.Type == TermLuceneASTNode.TermType.Null || RangeMax.Term == "*") ? float.MaxValue : float.Parse(RangeMax.Term.Substring(2));
         return(NumericRangeQuery.NewFloatRange(configuration.FieldName, 4, min, max, InclusiveMin, InclusiveMax));
     }
     if (RangeMin.Type == TermLuceneASTNode.TermType.Double || RangeMax.Type == TermLuceneASTNode.TermType.Double)
     {
         //numbers inside range without prefix are treated as strings.
         if (!RangeMin.Term.StartsWith("Dx") && !RangeMax.Term.StartsWith("Dx"))
         {
             return(new TermRangeQuery(configuration.FieldName,
                                       RangeMin.Type == TermLuceneASTNode.TermType.Null?null:RangeMin.Term,
                                       RangeMax.Type == TermLuceneASTNode.TermType.Null?null:RangeMax.Term,
                                       InclusiveMin, InclusiveMax));
         }
         var min = (RangeMin.Type == TermLuceneASTNode.TermType.Null || RangeMin.Term == "*") ? double.MinValue : double.Parse(RangeMin.Term.Substring(2));
         var max = (RangeMax.Type == TermLuceneASTNode.TermType.Null || RangeMax.Term == "*") ? double.MaxValue : double.Parse(RangeMax.Term.Substring(2));
         return(NumericRangeQuery.NewDoubleRange(configuration.FieldName, 4, min, max, InclusiveMin, InclusiveMax));
     }
     if (RangeMin.Type == TermLuceneASTNode.TermType.Int || RangeMax.Type == TermLuceneASTNode.TermType.Int)
     {
         //numbers inside range without prefix are treated as strings.
         if (!RangeMin.Term.StartsWith("Ix") && !RangeMax.Term.StartsWith("Ix"))
         {
             return(new TermRangeQuery(configuration.FieldName,
                                       RangeMin.Type == TermLuceneASTNode.TermType.Null ? null : RangeMin.Term,
                                       RangeMax.Type == TermLuceneASTNode.TermType.Null ? null : RangeMax.Term,
                                       InclusiveMin, InclusiveMax));
         }
         var intMin = (RangeMin.Type == TermLuceneASTNode.TermType.Null || RangeMin.Term == "*") ? int.MinValue : int.Parse(RangeMin.Term.Substring(2));
         var intMax = (RangeMax.Type == TermLuceneASTNode.TermType.Null || RangeMax.Term == "*") ? int.MaxValue : int.Parse(RangeMax.Term.Substring(2));
         return(NumericRangeQuery.NewIntRange(configuration.FieldName, 4, intMin, intMax, InclusiveMin, InclusiveMax));
     }
     if (RangeMin.Type == TermLuceneASTNode.TermType.Long || RangeMax.Type == TermLuceneASTNode.TermType.Long)
     {
         var longMin = (RangeMin.Type == TermLuceneASTNode.TermType.Null || RangeMin.Term == "*") ? long.MinValue : long.Parse(RangeMin.Term.Substring(2));
         var longMax = (RangeMax.Type == TermLuceneASTNode.TermType.Null || RangeMax.Term == "*") ? long.MaxValue : long.Parse(RangeMax.Term.Substring(2));
         return(NumericRangeQuery.NewLongRange(configuration.FieldName, 4, longMin, longMax, InclusiveMin, InclusiveMax));
     }
     if (RangeMin.Type == TermLuceneASTNode.TermType.Hex || RangeMax.Type == TermLuceneASTNode.TermType.Hex)
     {
         long longMin;
         long longMax;
         if (RangeMin.Type == TermLuceneASTNode.TermType.Hex)
         {
             if (RangeMin.Term.Length <= 10)
             {
                 var intMin = int.Parse(RangeMin.Term.Substring(2), NumberStyles.HexNumber);
                 var intMax = (RangeMax.Type == TermLuceneASTNode.TermType.Null || RangeMax.Term == "*") ? int.MaxValue : int.Parse(RangeMax.Term.Substring(2), NumberStyles.HexNumber);
                 return(NumericRangeQuery.NewIntRange(configuration.FieldName, 4, intMin, intMax, InclusiveMin, InclusiveMax));
             }
             longMin = long.Parse(RangeMin.Term.Substring(2), NumberStyles.HexNumber);
             longMax = (RangeMax.Type == TermLuceneASTNode.TermType.Null || RangeMax.Term == "*") ? long.MaxValue : long.Parse(RangeMax.Term.Substring(2), NumberStyles.HexNumber);
             return(NumericRangeQuery.NewLongRange(configuration.FieldName, 4, longMin, longMax, InclusiveMin, InclusiveMax));
         }
         if (RangeMax.Term.Length <= 10)
         {
             var intMin = (RangeMin.Type == TermLuceneASTNode.TermType.Null || RangeMin.Term == "*") ? int.MinValue : int.Parse(RangeMin.Term.Substring(2), NumberStyles.HexNumber);
             var intMax = int.Parse(RangeMax.Term.Substring(2), NumberStyles.HexNumber);
             return(NumericRangeQuery.NewIntRange(configuration.FieldName, 4, intMin, intMax, InclusiveMin, InclusiveMax));
         }
         longMin = (RangeMin.Type == TermLuceneASTNode.TermType.Null || RangeMin.Term == "*") ? long.MinValue : long.Parse(RangeMin.Term.Substring(2), NumberStyles.HexNumber);
         longMax = long.Parse(RangeMax.Term.Substring(2), NumberStyles.HexNumber);
         return(NumericRangeQuery.NewLongRange(configuration.FieldName, 4, longMin, longMax, InclusiveMin, InclusiveMax));
     }
     if (RangeMin.Type == TermLuceneASTNode.TermType.Null && RangeMax.Type == TermLuceneASTNode.TermType.Null)
     {
         return(new WildcardQuery(new Term(configuration.FieldName, "*")));
     }
     return(new TermRangeQuery(configuration.FieldName,
                               RangeMin.Type == TermLuceneASTNode.TermType.Null ? null : RangeMin.Term,
                               RangeMax.Type == TermLuceneASTNode.TermType.Null ? null : RangeMax.Term,
                               InclusiveMin, InclusiveMax));
 }
Esempio n. 15
0
 public virtual Query ToGroupFieldQuery(LuceneASTQueryConfiguration configuration)
 {
     return(ToQuery(configuration));
 }
Esempio n. 16
0
 public virtual void AddQueryToBooleanQuery(BooleanQuery b, LuceneASTQueryConfiguration configuration, Occur o = Occur.MUST)
 {
     b.Add(ToQuery(configuration), o);
 }
Esempio n. 17
0
 public abstract Query ToQuery(LuceneASTQueryConfiguration configuration);
Esempio n. 18
0
        public override Query ToQuery(LuceneASTQueryConfiguration configuration)
        {
            var boost = string.IsNullOrEmpty(Boost) ? 1 : float.Parse(Boost);

            //Look into changing the grammer to better handle qouted/unqouted unanlyzed terms
            if (Type == TermType.UnAnalyzed)
            {
                var originalLength = Term.Length;
                var qouted         = Term[2] == '\"' && Term[originalLength - 3] == '\"';
                var start          = qouted ? 3 : 2;
                var length         = qouted ? originalLength - 6 : originalLength - 4;
                return(new TermQuery(new Term(configuration.FieldName, Term.Substring(start, length)))
                {
                    Boost = boost
                });
            }
            switch (Type)
            {
            case TermType.Float:
            case TermType.Hex:
            case TermType.Double:
            case TermType.DateTime:
            case TermType.Int:
            case TermType.Long:
                return(new TermQuery(new Term(configuration.FieldName, Term))
                {
                    Boost = boost
                });
            }

            if (Type == TermType.QuotedWildcard)
            {
                var res = AnalyzedWildCardQueries(configuration);
                res.Boost = boost;
                return(res);
            }

            if (Type == TermType.WildCardTerm)
            {
                var res = AnalyzedWildCardQueries(configuration);
                res.Boost = boost;
                return(res);
            }

            var tokenStream = configuration.Analayzer.ReusableTokenStream(configuration.FieldName, new StringReader(Term));
            var terms       = new List <string>();

            while (tokenStream.IncrementToken())
            {
                var attribute = (TermAttribute)tokenStream.GetAttribute <ITermAttribute>();
                terms.Add(attribute.Term);
            }
            if (Type == TermType.PrefixTerm)
            {
                var actualTerm = string.Empty;
                if (terms.Count != 0)
                {
                    var first = terms.First();
                    actualTerm = first[first.Length - 1] == '*' ? first.Substring(0, first.Length - 1) : first;
                }
                return(new PrefixQuery(new Term(configuration.FieldName, actualTerm))
                {
                    Boost = boost
                });
            }
            if (terms.Count == 0)
            {
                return(new BooleanQuery());
            }

            if (Type == TermType.Quoted)
            {
                /*if (!string.IsNullOrEmpty(Proximity))
                 * {
                 *  var pq = new PhraseQuery() { Boost = boost };
                 *  pq.Add(new Term(configuration.FieldName, Term));
                 *  pq.Slop = int.Parse(Proximity);
                 *  return pq;
                 * }*/
                if (terms.Count == 1)
                {
                    return(new TermQuery(new Term(configuration.FieldName, terms.First()))
                    {
                        Boost = boost
                    });
                }
                var pq = new PhraseQuery()
                {
                    Boost = boost
                };
                foreach (var term in terms)
                {
                    pq.Add(new Term(configuration.FieldName, term));
                }
                return(pq);
                //return new TermQuery(new Term(configuration.FieldName, Term.Substring(1, Term.Length - 2))){Boost = boost};
            }
            //This is probably wrong, need to check what happens with analyzed unqouted terms.
            if (Type == TermType.UnQuoted && !string.IsNullOrEmpty(Similarity))
            {
                var similarity = float.Parse(Similarity);

                return(new FuzzyQuery(new Term(configuration.FieldName, terms.FirstOrDefault()), similarity, 0)
                {
                    Boost = boost
                });
            }
            if (terms.Count == 1)
            {
                return(new TermQuery(new Term(configuration.FieldName, terms.First()))
                {
                    Boost = boost
                });
            }
            var phrase = new PhraseQuery()
            {
                Boost = boost
            };

            foreach (var term in terms)
            {
                phrase.Add(new Term(configuration.FieldName, term));
            }
            return(phrase);
        }
Esempio n. 19
0
 private Query AnalyzedWildCardQueries(LuceneASTQueryConfiguration configuration)
 {
     return(new WildcardQuery(GetWildcardTerm(configuration)));
 }
Esempio n. 20
0
        private Term GetWildcardTerm(LuceneASTQueryConfiguration configuration)
        {
            var qouted      = Type == TermType.QuotedWildcard;
            var reader      = new StringReader(qouted ? Term.Substring(1, Term.Length - 2) : Term);
            var tokenStream = configuration.Analayzer.ReusableTokenStream(configuration.FieldName, reader);
            var terms       = new List <string>();

            while (tokenStream.IncrementToken())
            {
                var attribute = (TermAttribute)tokenStream.GetAttribute <ITermAttribute>();
                terms.Add(attribute.Term);
            }

            if (terms.Count == 0)
            {
                return(new Term(configuration.FieldName, Term));
            }

            var sb = new StringBuilder();
            int expectedLength;

            if (terms.Count == 1)
            {
                var firstTerm = terms.First();
                if (Term.StartsWith("*") && !firstTerm.StartsWith("*"))
                {
                    sb.Append('*');
                }
                sb.Append(firstTerm);
                if (Term.EndsWith("*") && !firstTerm.EndsWith("*"))
                {
                    sb.Append('*');
                }
                var res = sb.ToString();
                expectedLength = (qouted ? 2 : 0) + res.Length;
                Debug.Assert(expectedLength == Term.Length,
                             @"if analyzer changes length of term and removes wildcards after processing it, 
there is no way to know where to put the wildcard character back after the analysis. 
This edge-case has a very slim chance of happening, but still we should not ignore it completely.");
                return(new Term(configuration.FieldName, res));
            }

            foreach (var currentTerm in terms)
            {
                if (sb.Length < Term.Length)
                {
                    var c = Term[sb.Length];
                    if (c == '?' || c == '*')
                    {
                        sb.Append(c);
                    }
                }
                sb.Append(currentTerm);
            }

            var analyzedTermString = sb.ToString();

            expectedLength = analyzedTermString.Length + (qouted ? 2 : 0);
            Debug.Assert(expectedLength == Term.Length,
                         @"if analyzer changes length of term and removes wildcards after processing it, 
there is no way to know where to put the wildcard character back after the analysis. 
This edge-case has a very slim chance of happening, but still we should not ignore it completely.");

            return(new Term(configuration.FieldName, analyzedTermString));
        }
Esempio n. 21
0
 public override Query ToGroupFieldQuery(LuceneASTQueryConfiguration configuration)
 {
     return(Node.ToQuery(configuration));
 }
Esempio n. 22
0
        public virtual void AddQueryToBooleanQuery(BooleanQuery b, LuceneASTQueryConfiguration configuration, Occur o, bool letChildrenDecide = false)
        {
            var oc = letChildrenDecide ? GetDefaultOccurFromConfiguration(configuration) : o;

            b.Add(ToQuery(configuration), oc);
        }
Esempio n. 23
0
 public override Query ToQuery(LuceneASTQueryConfiguration configuration)
 {
     return(new MatchAllDocsQuery());
 }
Esempio n. 24
0
        public override Query ToQuery(LuceneASTQueryConfiguration configuration)
        {
            var boost = string.IsNullOrEmpty(Boost) ? 1 : float.Parse(Boost);

            //Look into changing the grammer to better handle qouted/unqouted unanlyzed terms
            if (Type == TermType.UnAnalyzed)
            {
                var originalLength = Term.Length;
                var qouted         = Term[2] == '\"' && Term[originalLength - 3] == '\"';
                var start          = qouted ? 3 : 2;
                var length         = qouted ? originalLength - 6 : originalLength - 4;
                return(new TermQuery(new Term(configuration.FieldName, Term.Substring(start, length)))
                {
                    Boost = boost
                });
            }
            switch (Type)
            {
            case TermType.Float:
            case TermType.Hex:
            case TermType.Double:
            case TermType.DateTime:
            case TermType.Int:
            case TermType.Long:
                return(new TermQuery(new Term(configuration.FieldName, Term))
                {
                    Boost = boost
                });
            }

            if (Type == TermType.QuotedWildcard)
            {
                var res = AnalyzedWildCardQueries(configuration);
                res.Boost = boost;
                return(res);
            }

            if (Type == TermType.WildCardTerm)
            {
                var res = AnalyzedWildCardQueries(configuration);
                res.Boost = boost;
                return(res);
            }

            var tokenStream = configuration.Analayzer.ReusableTokenStream(configuration.FieldName, new StringReader(Term));
            var terms       = new List <string>();

            while (tokenStream.IncrementToken())
            {
                var attribute = (TermAttribute)tokenStream.GetAttribute <ITermAttribute>();
                terms.Add(attribute.Term);
            }
            if (Type == TermType.PrefixTerm)
            {
                if (terms.Count != 0)
                {
                    var first      = terms.First();
                    var actualTerm = first[first.Length - 1] == '*' ? first.Substring(0, first.Length - 1) : first;
                    return(new PrefixQuery(new Term(configuration.FieldName, actualTerm))
                    {
                        Boost = boost
                    });
                }
                // if the term that we are trying to prefix has been removed entirely by the analyzer, then we are going
                // to cheat a bit, and check for both the term in as specified and the term in lower case format so we can
                // find it regardless of casing
                var removeStar   = Term.Substring(0, Term.Length - 1);
                var booleanQuery = new BooleanQuery
                {
                    Clauses =
                    {
                        new BooleanClause(new PrefixQuery(new Term(configuration.FieldName, removeStar)),                    Occur.SHOULD),
                        new BooleanClause(new PrefixQuery(new Term(configuration.FieldName, removeStar.ToLowerInvariant())), Occur.SHOULD),
                    },
                    Boost = boost
                };
                return(booleanQuery);
            }
            if (terms.Count == 0)
            {
                return(null);
            }

            if (Type == TermType.Quoted)
            {
                /*if (!string.IsNullOrEmpty(Proximity))
                 * {
                 *  var pq = new PhraseQuery() { Boost = boost };
                 *  pq.Add(new Term(configuration.FieldName, Term));
                 *  pq.Slop = int.Parse(Proximity);
                 *  return pq;
                 * }*/
                if (terms.Count == 1)
                {
                    return(new TermQuery(new Term(configuration.FieldName, terms.First()))
                    {
                        Boost = boost
                    });
                }
                var pq = new PhraseQuery()
                {
                    Boost = boost
                };
                foreach (var term in terms)
                {
                    pq.Add(new Term(configuration.FieldName, term));
                }
                return(pq);
                //return new TermQuery(new Term(configuration.FieldName, Term.Substring(1, Term.Length - 2))){Boost = boost};
            }
            //This is probably wrong, need to check what happens with analyzed unqouted terms.
            if (Type == TermType.UnQuoted && !string.IsNullOrEmpty(Similarity))
            {
                var similarity = float.Parse(Similarity);

                return(new FuzzyQuery(new Term(configuration.FieldName, terms.FirstOrDefault()), similarity, 0)
                {
                    Boost = boost
                });
            }
            if (terms.Count == 1)
            {
                return(new TermQuery(new Term(configuration.FieldName, terms.First()))
                {
                    Boost = boost
                });
            }
            var phrase = new PhraseQuery()
            {
                Boost = boost
            };

            foreach (var term in terms)
            {
                phrase.Add(new Term(configuration.FieldName, term));
            }
            return(phrase);
        }