Пример #1
0
        private Query CreateValueQuery(QueryFieldValue value)
        {
            var    currentField = _currentField.Peek();
            string numval;

            switch (value.Datatype)
            {
            case IndexableDataType.String:
                return(CreateStringValueQuery(value, currentField));

            case IndexableDataType.Int: numval = NumericUtils.IntToPrefixCoded(value.IntValue); break;

            case IndexableDataType.Long: numval = NumericUtils.LongToPrefixCoded(value.LongValue); break;

            case IndexableDataType.Float: numval = NumericUtils.FloatToPrefixCoded(value.SingleValue); break;

            case IndexableDataType.Double: numval = NumericUtils.DoubleToPrefixCoded(value.DoubleValue); break;

            default:
                throw new NotImplementedException("Unknown IndexableDataType enum value: " + value.Datatype);
            }
            var numterm = new Term(currentField.Name, numval);

            return(new TermQuery(numterm));
        }
Пример #2
0
        private Query CreateRangeQuery(string fieldName, QueryFieldValue minValue, QueryFieldValue maxValue, bool includeLower, bool includeUpper)
        {
            if (minValue != null && minValue.StringValue == ContentQuery.EmptyText && maxValue == null)
            {
                ParseEmptyQuery = true;
                return(new TermQuery(new Term(fieldName, minValue.StringValue)));
            }
            if (maxValue != null && maxValue.StringValue == ContentQuery.EmptyText && minValue == null)
            {
                ParseEmptyQuery = true;
                return(new TermQuery(new Term(fieldName, maxValue.StringValue)));
            }
            if (minValue != null && minValue.StringValue == ContentQuery.EmptyText)
            {
                minValue = null;
            }
            if (maxValue != null && maxValue.StringValue == ContentQuery.EmptyText)
            {
                maxValue = null;
            }

            switch (minValue != null ? minValue.Datatype : maxValue.Datatype)
            {
            case IndexableDataType.String:
                var lowerTerm = minValue == null ? null : minValue.StringValue.ToLower();
                var upperTerm = maxValue == null ? null : maxValue.StringValue.ToLower();
                return(new TermRangeQuery(fieldName, lowerTerm, upperTerm, includeLower, includeUpper));

            case IndexableDataType.Int:
                var lowerInt = minValue == null ? null : (Int32?)minValue.IntValue;
                var upperInt = maxValue == null ? null : (Int32?)maxValue.IntValue;
                return(NumericRangeQuery.NewIntRange(fieldName, lowerInt, upperInt, includeLower, includeUpper));

            case IndexableDataType.Long:
                var lowerLong = minValue == null ? null : (Int64?)minValue.LongValue;
                var upperLong = maxValue == null ? null : (Int64?)maxValue.LongValue;
                return(NumericRangeQuery.NewLongRange(fieldName, 8, lowerLong, upperLong, includeLower, includeUpper));

            case IndexableDataType.Float:
                var lowerFloat = minValue == null ? Single.MinValue : minValue.SingleValue;
                var upperFloat = maxValue == null ? Single.MaxValue : maxValue.SingleValue;
                return(NumericRangeQuery.NewFloatRange(fieldName, lowerFloat, upperFloat, includeLower, includeUpper));

            case IndexableDataType.Double:
                var lowerDouble = minValue == null ? Double.MinValue : minValue.DoubleValue;
                var upperDouble = maxValue == null ? Double.MaxValue : maxValue.DoubleValue;
                return(NumericRangeQuery.NewDoubleRange(fieldName, 8, lowerDouble, upperDouble, includeLower, includeUpper));

            default:
                throw new NotImplementedException("Unknown IndexableDataType: " + minValue.Datatype);
            }
        }
Пример #3
0
        private QueryFieldValue ParseExactValue(bool throwEx)
        {
            //ExactValue   ==>  STRING | NUMBER | EMPTY
            if (_lexer.StringValue == ContentQuery.EmptyText)
            {
                ParseEmptyQuery = true;
                var fieldVal = new QueryFieldValue(_lexer.StringValue, _lexer.CurrentToken, _lexer.IsPhrase);
                _lexer.NextToken();
                return(fieldVal);
            }
            if (_lexer.CurrentToken != SnLucLexer.Token.String && _lexer.CurrentToken != SnLucLexer.Token.Number)
            {
                if (throwEx)
                {
                    throw ParserError(String.Concat("Unexpected ", _lexer.CurrentToken, ". Expected: STRING | NUMBER"));
                }
                return(null);
            }

            var field     = _currentField.Peek();
            var fieldName = field.Name;
            var val       = new QueryFieldValue(_lexer.StringValue, _lexer.CurrentToken, _lexer.IsPhrase);

            if (fieldName != LucObject.FieldName.AllText && _lexer.StringValue != ContentQuery.EmptyInnerQueryText)
            {
                var info = SenseNet.ContentRepository.Schema.ContentTypeManager.GetPerFieldIndexingInfo(fieldName);
                if (info != null)
                {
                    var fieldHandler = info.IndexFieldHandler;
                    if (fieldHandler != null)
                    {
                        if (!fieldHandler.TryParseAndSet(val))
                        {
                            if (throwEx)
                            {
                                throw ParserError(String.Concat("Cannot parse the '", fieldName, "' field value: ", _lexer.StringValue));
                            }
                            return(null);
                        }
                    }
                }
            }
            _lexer.NextToken();
            return(val);
        }
Пример #4
0
        private QueryFieldValue ParseValue(bool throwEx)
        {
            //Value        ==>  ExactValue | WILDCARDSTRING
            var val = ParseExactValue(false);

            if (val != null)
            {
                return(val);
            }
            if (_lexer.CurrentToken == SnLucLexer.Token.WildcardString)
            {
                val = new QueryFieldValue(_lexer.StringValue.ToLower(), _lexer.CurrentToken, _lexer.IsPhrase);
                _lexer.NextToken();
                return(val);
            }
            if (!throwEx)
            {
                return(null);
            }
            throw ParserError(String.Concat("Unexpected ", _lexer.CurrentToken, ". Expected: STRING | NUMBER | WILDCARDSTRING"));
        }
Пример #5
0
        private Query CreateRangeQuery(string fieldName, QueryFieldValue minValue, QueryFieldValue maxValue, bool includeLower, bool includeUpper)
        {
            if (minValue != null && minValue.StringValue == ContentQuery.EmptyText && maxValue == null)
            {
                ParseEmptyQuery = true;
                return new TermQuery(new Term(fieldName, minValue.StringValue));
            }
            if (maxValue != null && maxValue.StringValue == ContentQuery.EmptyText && minValue == null)
            {
                ParseEmptyQuery = true;
                return new TermQuery(new Term(fieldName, maxValue.StringValue));
            }
            if (minValue != null && minValue.StringValue == ContentQuery.EmptyText)
                minValue = null;
            if (maxValue != null && maxValue.StringValue == ContentQuery.EmptyText)
                maxValue = null;

            switch (minValue != null ? minValue.Datatype : maxValue.Datatype)
            {
                case IndexableDataType.String:
                    var lowerTerm = minValue == null ? null : minValue.StringValue.ToLower();
                    var upperTerm = maxValue == null ? null : maxValue.StringValue.ToLower();
                    return new TermRangeQuery(fieldName, lowerTerm, upperTerm, includeLower, includeUpper);
                case IndexableDataType.Int:
                    var lowerInt = minValue == null ? null : (Int32?)minValue.IntValue;
                    var upperInt = maxValue == null ? null : (Int32?)maxValue.IntValue;
                    return NumericRangeQuery.NewIntRange(fieldName, lowerInt, upperInt, includeLower, includeUpper);
                case IndexableDataType.Long:
                    var lowerLong = minValue == null ? null : (Int64?)minValue.LongValue;
                    var upperLong = maxValue == null ? null : (Int64?)maxValue.LongValue;
                    return NumericRangeQuery.NewLongRange(fieldName, 8, lowerLong, upperLong, includeLower, includeUpper);
                case IndexableDataType.Float:
                    var lowerFloat = minValue == null ? Single.MinValue : minValue.SingleValue;
                    var upperFloat = maxValue == null ? Single.MaxValue : maxValue.SingleValue;
                    return NumericRangeQuery.NewFloatRange(fieldName, lowerFloat, upperFloat, includeLower, includeUpper);
                case IndexableDataType.Double:
                    var lowerDouble = minValue == null ? Double.MinValue : minValue.DoubleValue;
                    var upperDouble = maxValue == null ? Double.MaxValue : maxValue.DoubleValue;
                    return NumericRangeQuery.NewDoubleRange(fieldName, 8, lowerDouble, upperDouble, includeLower, includeUpper);
                default:
                    throw new NotImplementedException("Unknown IndexableDataType: " + minValue.Datatype);
            }
        }
Пример #6
0
        private Query CreateStringValueQuery(QueryFieldValue value, FieldInfo currentField)
        {
            switch (value.Token)
            {
                case SnLucLexer.Token.Number:
                case SnLucLexer.Token.String:
                    if(value.StringValue == ContentQuery.EmptyText)
                        return new TermQuery(new Term(currentField.Name, value.StringValue));
                    if (value.StringValue == ContentQuery.EmptyInnerQueryText)
                        return new TermQuery(new Term("Id", NumericUtils.IntToPrefixCoded(0)));

                    var words = GetAnalyzedText(currentField.Name, value.StringValue);

                    if (words.Length == 0)
                        words = new String[] { String.Empty }; //return null;
                    if (words.Length == 1)
                    {
                        var term = new Term(currentField.Name, words[0]);
                        if(value.FuzzyValue == null)
                            return new TermQuery(term);
                        return new FuzzyQuery(term, Convert.ToSingle(value.FuzzyValue));
                    }

                    var phraseQuery = new PhraseQuery();
                    foreach(var word in words)
                        phraseQuery.Add(new Term(currentField.Name, word));

                    if (value.FuzzyValue != null)
                    {
                        var slop = Convert.ToInt32(value.FuzzyValue.Value);
                        phraseQuery.SetSlop(slop);
                    }
                    return phraseQuery;
                case SnLucLexer.Token.WildcardString:
                    if (!value.StringValue.EndsWith("*"))
                        return new WildcardQuery(new Term(currentField.Name, value.StringValue));
                    var s = value.StringValue.TrimEnd('*');
                    if (s.Contains('?') || s.Contains('*'))
                        return new WildcardQuery(new Term(currentField.Name, value.StringValue));
                    return new PrefixQuery(new Term(currentField.Name, s));
                default:
                    throw new NotImplementedException("CreateValueQuery with Token: " + value.Token);
            }
        }
Пример #7
0
 private Query CreateValueQuery(QueryFieldValue value)
 {
     var currentField = _currentField.Peek();
     string numval;
     switch (value.Datatype)
     {
         case IndexableDataType.String:
             return CreateStringValueQuery(value, currentField);
         case IndexableDataType.Int: numval = NumericUtils.IntToPrefixCoded(value.IntValue); break;
         case IndexableDataType.Long: numval = NumericUtils.LongToPrefixCoded(value.LongValue); break;
         case IndexableDataType.Float: numval = NumericUtils.FloatToPrefixCoded(value.SingleValue); break;
         case IndexableDataType.Double: numval = NumericUtils.DoubleToPrefixCoded(value.DoubleValue); break;
         default:
             throw new NotImplementedException("Unknown IndexableDataType enum value: " + value.Datatype);
     }
     var numterm = new Term(currentField.Name, numval);
     return new TermQuery(numterm);
 }
Пример #8
0
        private QueryFieldValue ParseExactValue(bool throwEx)
        {
            //ExactValue   ==>  STRING | NUMBER | EMPTY
            if (_lexer.StringValue == ContentQuery.EmptyText)
            {
                ParseEmptyQuery = true;
                var fieldVal = new QueryFieldValue(_lexer.StringValue, _lexer.CurrentToken, _lexer.IsPhrase);
                _lexer.NextToken();
                return fieldVal;
            }
            if (_lexer.CurrentToken != SnLucLexer.Token.String && _lexer.CurrentToken != SnLucLexer.Token.Number)
            {
                if (throwEx)
                    throw ParserError(String.Concat("Unexpected ", _lexer.CurrentToken, ". Expected: STRING | NUMBER"));
                return null;
            }

            var fieldName = _currentField.Peek().Name;
            var val = new QueryFieldValue(_lexer.StringValue, _lexer.CurrentToken, _lexer.IsPhrase);
            if (fieldName != LucObject.FieldName.AllText && _lexer.StringValue != ContentQuery.EmptyInnerQueryText)
            {
                var fieldHandler = GetFieldParser(fieldName);
                if (fieldHandler != null)
                {
                    if (!fieldHandler.TryParseAndSet(val))
                    {
                        if (throwEx)
                            throw ParserError(String.Concat("Cannot parse the '", fieldName, "' field value: ", _lexer.StringValue));
                        return null;
                    }
                }
            }
            _lexer.NextToken();
            return val;
        }
Пример #9
0
 private QueryFieldValue ParseValue(bool throwEx)
 {
     //Value        ==>  ExactValue | WILDCARDSTRING
     var val = ParseExactValue(false);
     if (val != null)
         return val;
     if (_lexer.CurrentToken == SnLucLexer.Token.WildcardString)
     {
         val = new QueryFieldValue(_lexer.StringValue.ToLower(), _lexer.CurrentToken, _lexer.IsPhrase );
         _lexer.NextToken();
         return val;
     }
     if (!throwEx)
         return null;
     throw ParserError(String.Concat("Unexpected ", _lexer.CurrentToken, ". Expected: STRING | NUMBER | WILDCARDSTRING"));
 }
Пример #10
0
        private Query CreateStringValueQuery(QueryFieldValue value, FieldInfo currentField)
        {
            switch (value.Token)
            {
            case SnLucLexer.Token.Number:
            case SnLucLexer.Token.String:
                if (value.StringValue == ContentQuery.EmptyText)
                {
                    return(new TermQuery(new Term(currentField.Name, value.StringValue)));
                }
                if (value.StringValue == ContentQuery.EmptyInnerQueryText)
                {
                    return(new TermQuery(new Term("Id", NumericUtils.IntToPrefixCoded(0))));
                }

                var words = GetAnalyzedText(currentField.Name, value.StringValue);

                if (words.Length == 0)
                {
                    words = new String[] { String.Empty }
                }
                ;                                              //return null;
                if (words.Length == 1)
                {
                    var term = new Term(currentField.Name, words[0]);
                    if (value.FuzzyValue == null)
                    {
                        return(new TermQuery(term));
                    }
                    return(new FuzzyQuery(term, Convert.ToSingle(value.FuzzyValue)));
                }

                var phraseQuery = new PhraseQuery();
                foreach (var word in words)
                {
                    phraseQuery.Add(new Term(currentField.Name, word));
                }

                if (value.FuzzyValue != null)
                {
                    var slop = Convert.ToInt32(value.FuzzyValue.Value);
                    phraseQuery.SetSlop(slop);
                }
                return(phraseQuery);

            case SnLucLexer.Token.WildcardString:
                if (!value.StringValue.EndsWith("*"))
                {
                    return(new WildcardQuery(new Term(currentField.Name, value.StringValue)));
                }
                var s = value.StringValue.TrimEnd('*');
                if (s.Contains('?') || s.Contains('*'))
                {
                    return(new WildcardQuery(new Term(currentField.Name, value.StringValue)));
                }
                return(new PrefixQuery(new Term(currentField.Name, s)));

            default:
                throw new NotImplementedException("CreateValueQuery with Token: " + value.Token);
            }
        }