Ejemplo n.º 1
0
        public static object ParseFieldValue(Type modelType, string field, string fieldValue)
        {
            if (String.IsNullOrWhiteSpace(fieldValue) || fieldValue == "*" || fieldValue == "NULL")
            {
                return(null);
            }

            var prop = modelType.GetProperty(field, BindingFlags.Public | BindingFlags.Instance);

            if (prop == null)
            {
                // if not corresponding property was found, it might be custom fields, they are all treated as string
                return(fieldValue);
            }

            if (TypeHelper.IsSimpleType(prop.PropertyType))
            {
                return(LuceneUtility.FromFieldStringValue(fieldValue, prop.PropertyType));
            }

            var propTypeInfo = ModelTypeInfo.GetTypeInfo(prop.PropertyType);

            if (propTypeInfo.IsDictionary)
            {
                return(LuceneUtility.FromFieldStringValue(fieldValue, propTypeInfo.DictionaryValueType));
            }
            else if (propTypeInfo.IsCollection)
            {
                return(LuceneUtility.FromFieldStringValue(fieldValue, propTypeInfo.ElementType));
            }

            return(null);
        }
Ejemplo n.º 2
0
        public IndexQuery WhereBetween(string field, object fromValue, object toValue, bool fromInclusive, bool toInclusive)
        {
            if (fromValue == null && toValue == null)
            {
                return(this);
            }

            var isNumericField = false;

            var property = ModelType.GetProperty(field, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);

            // property might be null because it might be custom fields
            if (property != null)
            {
                var fieldAttr = property.GetCustomAttribute <FieldAttribute>();
                isNumericField = fieldAttr != null && fieldAttr.Numeric;
            }

            Query query = null;

            if (isNumericField)
            {
                query = CreateNumericRangeQuery(field, property.PropertyType, fromValue, toValue, fromInclusive, toInclusive);
            }
            else
            {
                query = new TermRangeQuery(field, LuceneUtility.ToFieldStringValue(fromValue), LuceneUtility.ToFieldStringValue(toValue), fromInclusive, toInclusive);
            }

            return(And(query));
        }
Ejemplo n.º 3
0
        public IFieldable CreateLuceneField(string fieldName, object fieldValue)
        {
            if (!Numeric)
            {
                return(new Field(fieldName, LuceneUtility.ToFieldStringValue(fieldValue), Store, Index)
                {
                    Boost = Boost
                });
            }

            var field = new NumericField(fieldName, Store, Index != Field.Index.NO)
            {
                Boost = Boost
            };

            if (fieldValue is Int32)
            {
                field.SetIntValue((int)fieldValue);
            }
            else if (fieldValue is Int64)
            {
                field.SetLongValue((long)fieldValue);
            }
            else if (fieldValue is Single)
            {
                field.SetFloatValue((float)fieldValue);
            }
            else if (fieldValue is Double)
            {
                field.SetDoubleValue((double)fieldValue);
            }
            else if (fieldValue is Decimal)
            {
                field.SetDoubleValue((double)(decimal)fieldValue);
            }
            else
            {
                throw new NotSupportedException();
            }

            return(field);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Add sort field. Use + prefix for asc sorting, or use - prefix for desc sorting.
        /// </summary>
        public IndexQuery AddOrderBy(string field)
        {
            var reverse = false;

            if (field[0] == '+' || field[0] == '-')
            {
                reverse = field[0] == '-';
                field   = field.Substring(1);
            }

            var sortType = Lucene.Net.Search.SortField.STRING;

            var property = ModelType.GetProperty(field, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);

            if (property != null)
            {
                sortType = LuceneUtility.GetSortType(property.PropertyType);
            }

            SortFields.Add(new SortField(field, sortType, reverse));

            return(this);
        }
Ejemplo n.º 5
0
        public static object ToModel(Document document, Type modelType)
        {
            var model = Activator.CreateInstance(modelType);

            foreach (var prop in modelType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                if (TypeHelper.IsSimpleType(prop.PropertyType))
                {
                    var field = document.GetField(prop.Name);
                    if (field != null)
                    {
                        var propValue = LuceneUtility.FromFieldStringValue(field.StringValue, prop.PropertyType);
                        prop.SetValue(model, propValue, null);
                    }
                }
                else
                {
                    var propTypeInfo = ModelTypeInfo.GetTypeInfo(prop.PropertyType);
                    if (propTypeInfo.IsCollection)
                    {
                        if (propTypeInfo.IsDictionary)
                        {
                            var propValue = prop.GetValue(model, null);
                            if (propValue == null)
                            {
                                propValue = Activator.CreateInstance(typeof(Dictionary <,>).MakeGenericType(propTypeInfo.DictionaryKeyType, propTypeInfo.DictionaryValueType));
                                prop.SetValue(model, propValue, null);
                            }

                            var dic    = propValue as IDictionary;
                            var fields = document.GetFields().Where(f => f.Name.StartsWith(prop.Name));

                            // Property type is IDictionary<TKey, TValue>
                            if (TypeHelper.IsSimpleType(propTypeInfo.DictionaryValueType))
                            {
                                foreach (var field in fields)
                                {
                                    string propName;
                                    string dicKey;

                                    if (TryParseDictionaryFieldName(field.Name, out propName, out dicKey))
                                    {
                                        var fieldValue = LuceneUtility.FromFieldStringValue(field.StringValue, propTypeInfo.DictionaryValueType);
                                        dic.Add(dicKey, fieldValue);
                                    }
                                }
                            }
                            else // Property type is IDictionary<TKey, IList<TValue>> or IDictionary<TKey, ISet<TValue>>
                            {
                                var dicValueTypeInfo = ModelTypeInfo.GetTypeInfo(propTypeInfo.DictionaryValueType);
                                if (dicValueTypeInfo.IsCollection && TypeHelper.IsSimpleType(dicValueTypeInfo.ElementType))
                                {
                                    Type       newDicValueType  = null;
                                    MethodInfo hashsetAddMethod = null;
                                    if (dicValueTypeInfo.IsSet)
                                    {
                                        newDicValueType  = typeof(HashSet <>).MakeGenericType(dicValueTypeInfo.ElementType);
                                        hashsetAddMethod = GetAddMethod(newDicValueType, dicValueTypeInfo.ElementType);
                                    }
                                    else
                                    {
                                        newDicValueType = typeof(List <>).MakeGenericType(dicValueTypeInfo.ElementType);
                                    }

                                    foreach (var field in fields)
                                    {
                                        string propName;
                                        string dicKey;

                                        if (TryParseDictionaryFieldName(field.Name, out propName, out dicKey))
                                        {
                                            var fieldValue = LuceneUtility.FromFieldStringValue(field.StringValue, dicValueTypeInfo.ElementType);
                                            if (!dic.Contains(dicKey))
                                            {
                                                dic.Add(dicKey, Activator.CreateInstance(newDicValueType));
                                            }

                                            var list = dic[dicKey];

                                            if (dicValueTypeInfo.IsSet) // is HashSet<>
                                            {
                                                hashsetAddMethod.Invoke(list, new[] { fieldValue });
                                            }
                                            else // is IList<>
                                            {
                                                (list as IList).Add(fieldValue);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        else // Property is collection but not dictionary
                        {
                            var fields = document.GetFields(prop.Name);
                            if (fields.Length == 0)
                            {
                                continue;
                            }

                            var list = Activator.CreateInstance(typeof(List <>).MakeGenericType(propTypeInfo.ElementType)) as IList;

                            foreach (var field in fields)
                            {
                                var fieldValue = LuceneUtility.FromFieldStringValue(field.StringValue, propTypeInfo.ElementType);
                                list.Add(fieldValue);
                            }

                            if (prop.PropertyType.IsArray)
                            {
                                prop.SetValue(model, list.OfType <object>().ToArray(), null);
                            }
                            else
                            {
                                prop.SetValue(model, list, null);
                            }
                        }
                    }
                }
            }

            return(model);
        }
Ejemplo n.º 6
0
 private TermQuery CreateTermQuery(string field, object value)
 {
     return(new TermQuery(new Term(field, LuceneUtility.ToFieldStringValue(value))));
 }