예제 #1
0
        public void WriteDriveInfo(DriveInfo info, FieldStorage storage)
        {
            if (info == null)
            {
                throw new ArgumentNullException("info", "info cannot be null");
            }
            if (storage == null)
            {
                throw new ArgumentNullException("storage", "storage cannot be null");
            }
            IndexDocument document = new IndexDocument();

            document.Add(new FieldNormal("AvailableFreeSpace", info.AvailableFreeSpace.ToString(), storage.Store, storage.SearchRule, storage.VectorRule));
            document.Add(new FieldNormal("DriveFormat", info.DriveFormat, storage.Store, storage.SearchRule, storage.VectorRule));
            document.Add(new FieldNormal("DriveType", info.DriveType.ToString(), storage.Store, storage.SearchRule, storage.VectorRule));
            document.Add(new FieldNormal("IsReady", info.IsReady.ToString(), storage.Store, storage.SearchRule, storage.VectorRule));
            document.Add(new FieldNormal("Name", info.Name, storage.Store, storage.SearchRule, storage.VectorRule));
            if (info.RootDirectory != null && !string.IsNullOrEmpty(info.RootDirectory.FullName))
            {
                document.Add(new FieldNormal("", info.RootDirectory.FullName, storage.Store, storage.SearchRule, storage.VectorRule));
            }
            document.Add(new FieldNormal("TotalFreeSpace", info.TotalFreeSpace.ToString(), storage.Store, storage.SearchRule, storage.VectorRule));
            document.Add(new FieldNormal("TotalSize", info.TotalSize.ToString(), storage.Store, storage.SearchRule, storage.VectorRule));
            document.Add(new FieldNormal("VolumeLabel", info.VolumeLabel, storage.Store, storage.SearchRule, storage.VectorRule));
            this.Write(document);
        }
예제 #2
0
        public void Write <TKey, TValue>(IDictionary <TKey, TValue> values, FieldStorage storage)
        {
            if (!this.IsOpen)
            {
                throw new ObjectDisposedException("IndexWriter", "You cannot access this method from a disposed IndexWriter");
            }
            if (values == null)
            {
                throw new ArgumentNullException("values", "values cannot be null");
            }
            if (storage == null)
            {
                throw new ArgumentNullException("storage", "storage cannot be null");
            }

            IndexDocument document = new IndexDocument();

            foreach (KeyValuePair <TKey, TValue> pair in values)
            {
                if (pair.Key == null || pair.Value == null)
                {
                    continue;
                }
                string fieldName  = pair.Key.ToString();
                string fieldValue = pair.Value.ToString();
                if (string.IsNullOrEmpty(fieldName) || string.IsNullOrEmpty(fieldValue))
                {
                    continue;
                }
                document.Add(new FieldNormal(fieldName, fieldValue, storage.Store, storage.SearchRule, storage.VectorRule));
            }
            this.Write(document);
        }
예제 #3
0
        public void WriteDirectory(FileSystemInfo info, FieldStorage storage)
        {
            if (info == null)
            {
                throw new ArgumentNullException("info", "info cannot be null");
            }
            if (storage == null)
            {
                throw new ArgumentNullException("storage", "storage cannot be null");
            }
            IndexDocument document = new IndexDocument();

            if (!string.IsNullOrEmpty(info.Attributes.ToString()))
            {
                document.Add(new FieldNormal("Attributes", info.Attributes.ToString(), storage.Store, storage.SearchRule, storage.VectorRule));
            }
            document.Add(new FieldNormal("CreationTime", info.CreationTime.ToString("G"), storage.Store, storage.SearchRule, storage.VectorRule));
            document.Add(new FieldNormal("Exists", info.Exists.ToString(), storage.Store, storage.SearchRule, storage.VectorRule));
            document.Add(new FieldNormal("Extension", info.Extension, storage.Store, storage.SearchRule, storage.VectorRule));
            document.Add(new FieldNormal("FullName", info.FullName, storage.Store, storage.SearchRule, storage.VectorRule));
            document.Add(new FieldNormal("LastAccessTime", info.LastAccessTime.ToString("G"), storage.Store, storage.SearchRule, storage.VectorRule));
            document.Add(new FieldNormal("LastWriteTime", info.LastWriteTime.ToString("G"), storage.Store, storage.SearchRule, storage.VectorRule));
            document.Add(new FieldNormal("Name", info.Name, storage.Store, storage.SearchRule, storage.VectorRule));
            this.Write(document);

            // this.WriteInstance<DirectoryInfo>(info, storage); // could simpify the whole thing
        }
예제 #4
0
        public void Write <TKey>(string fieldName, IEnumerable <TKey> values, FieldStorage storage, bool makeSingleDocument)
        {
            if (!this.IsOpen)
            {
                throw new ObjectDisposedException("IndexWriter", "You cannot access this method from a disposed IndexWriter");
            }
            if (string.IsNullOrEmpty(fieldName))
            {
                throw new ArgumentNullException("fieldName", "fieldName cannot be null");
            }
            if (values == null)
            {
                throw new ArgumentNullException("values", "values cannot be null");
            }
            if (storage == null)
            {
                throw new ArgumentNullException("storage", "storage cannot be null");
            }

            if (makeSingleDocument)
            {
                IndexDocument document = new IndexDocument();
                foreach (TKey value in values)
                {
                    if (value == null)
                    {
                        continue;
                    }
                    string fieldValue = value.ToString();
                    if (string.IsNullOrEmpty(fieldValue))
                    {
                        continue;
                    }
                    document.Add(new FieldNormal(fieldName, fieldValue, storage.Store, storage.SearchRule, storage.VectorRule));
                }
                if (document.TotalFields > 0)
                {
                    this.Write(document);
                }
            }
            else
            {
                foreach (TKey value in values)
                {
                    if (value == null)
                    {
                        continue;
                    }
                    string fieldValue = value.ToString();
                    if (string.IsNullOrEmpty(fieldValue))
                    {
                        continue;
                    }
                    IndexDocument document = new IndexDocument();
                    document.Add(new FieldNormal(fieldName, fieldValue, storage.Store, storage.SearchRule, storage.VectorRule));
                    this.Write(document);
                }
            }
        }
예제 #5
0
        public void WriteDataRow(DataRow dataRow, IndexWriterRuleCollection ruleCollection, AnalyzerType analyzerType)
        {
            if (dataRow == null)
            {
                throw new ArgumentNullException("dataRow", "dataRow cannot be null");
            }
            if (ruleCollection == null)
            {
                throw new ArgumentNullException("ruleCollection", "ruleCollection cannot be null");
            }
            DataTable dataTable    = dataRow.Table;
            int       totalColumns = dataTable.Columns.Count;

            IndexDocument document = new IndexDocument();

            for (int j = 0; j < totalColumns; j++)
            {
                DataColumn column = dataTable.Columns[j];
                if (string.IsNullOrEmpty(column.ColumnName))
                {
                    continue;
                }
                IndexWriterRule rule = ruleCollection.GetRuleFromType(column.DataType);
                if (rule.SkipField(column.DataType, column.ColumnName))
                {
                    continue;
                }
                FieldStorage storageRule = rule.GetStorageRule(column.ColumnName);

                // that's all the field data, now lets get the value data
                object rowValue  = dataRow[j];
                bool   isRowNull = rowValue == null || rowValue == DBNull.Value;
                if (rule.SkipColumnIfNull && isRowNull)
                {
                    continue;
                }
                else if (!rule.SkipColumnIfNull && string.IsNullOrEmpty(rule.DefaultValueIfNull))
                {
                    continue;
                }

                string fieldValue = (isRowNull) ? rule.DefaultValueIfNull : rowValue.ToString();
                rowValue = null;
                document.Add(new FieldNormal(column.ColumnName, fieldValue, storageRule.Store, storageRule.SearchRule, storageRule.VectorRule));
            }

            if (document.TotalFields > 0)
            {
                if (analyzerType == AnalyzerType.None || analyzerType == AnalyzerType.Unknown)
                {
                    this.Write(document);
                }
                else
                {
                    this.Write(document, analyzerType);
                }
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="WriterEventArgs"/> class.
        /// </summary>
        /// <param name="document">The document to write.</param>
        /// <param name="analyzer">The analyzer being used to write.</param>
        /// <param name="indexName">Name of the index being written to.</param>
        public WriterEventArgs(IndexDocument document, AnalyzerType analyzer, string indexName)
        {
            if (document == null)
                throw new ArgumentNullException("document", "document cannot be null");
            if (string.IsNullOrEmpty(indexName))
                throw new ArgumentNullException("indexName", "indexName cannot be null or empty");

            this.document = document;
            this.analyzer = analyzer;
            this.index = indexName;
            this.cancel = false;
        }
예제 #7
0
        public static void WriteInstance <TKey>(this IndexWriter writer, TKey objectToWrite, IEnumerable <string> propertiesToIndex, FieldStorage storage)
            where TKey : class
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer", "writer cannot be null");
            }
            if (!writer.IsOpen)
            {
                throw new ObjectDisposedException("writer", "You cannot access this method from a disposed IndexWriter");
            }
            if (objectToWrite == null)
            {
                throw new ArgumentNullException("objectToWrite", "objectToWrite cannot be null");
            }
            if (storage == null)
            {
                throw new ArgumentNullException("storage", "storage cannot be null");
            }
            List <PropertyInfo> properties = new List <PropertyInfo>(objectToWrite.GetType().GetProperties());

            properties.RemoveAll(x => !x.CanRead || !propertiesToIndex.Contains(x.Name));
            if (properties.Count == 0)
            {
                throw new InvalidOperationException("You cannot index a class that doesn't have any publicly accessible (get) properties");
            }
            //for (int i = (totalProperties -1); i >= 0; i--) if (!propertiesToIndex.Contains(properties[i].Name)) properties.RemoveAt(i);

            // now all properties can be indexed
            int           totalProperties = properties.Count;
            IndexDocument document        = new IndexDocument();
            int           addedProperties = 0;

            for (int i = 0; i < totalProperties; i++)
            {
                PropertyInfo info          = properties[i];
                object       propertyValue = info.GetValue(objectToWrite, null);
                if (propertyValue == null)
                {
                    continue;
                }
                document.Add(new FieldNormal(info.Name, propertyValue.ToString(), storage.Store, storage.SearchRule, storage.VectorRule));
                ++addedProperties;
            }

            if (addedProperties > 0)
            {
                writer.Write(document);
            }
        }
예제 #8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="WriterEventArgs"/> class.
        /// </summary>
        /// <param name="document">The document to write.</param>
        /// <param name="analyzer">The analyzer being used to write.</param>
        /// <param name="indexName">Name of the index being written to.</param>
        public WriterEventArgs(IndexDocument document, AnalyzerType analyzer, string indexName)
        {
            if (document == null)
            {
                throw new ArgumentNullException("document", "document cannot be null");
            }
            if (string.IsNullOrEmpty(indexName))
            {
                throw new ArgumentNullException("indexName", "indexName cannot be null or empty");
            }

            this.document = document;
            this.analyzer = analyzer;
            this.index    = indexName;
            this.cancel   = false;
        }
예제 #9
0
        /// <summary>
        /// Writes the specified document.
        /// </summary>
        /// <param name="document">The document to out.</param>
        /// <param name="appliedAnalyzer">An analyzer to use against specifically this field.</param>
        /// <remarks>
        /// This is useful if you have a specific type of field or document that needs special analysis rules;
        /// not normally helpful if the analyzer is more complex, usually useful if the analyzer is less complex.
        /// </remarks>
        public void Write(IndexDocument document, AnalyzerType appliedAnalyzer)
        {
            if (this.isDisposed)
            {
                throw new ObjectDisposedException("IndexWriter", "You cannot access this method from a disposed IndexWriter");
            }
            if (document == null)
            {
                throw new ArgumentNullException("document", "document cannot be null");
            }

            OnWriting(new WriterEventArgs(document, (appliedAnalyzer == IndexLibrary.AnalyzerType.None || appliedAnalyzer == IndexLibrary.AnalyzerType.Unknown) ? this.AnalyzerType : appliedAnalyzer, this.index.IndexDirectory.Name));
            if (appliedAnalyzer != IndexLibrary.AnalyzerType.None && appliedAnalyzer != IndexLibrary.AnalyzerType.Unknown)
            {
                this.writer.AddDocument(document.GetLuceneDocument, TypeConverter.GetAnalyzer(appliedAnalyzer));
            }
            else
            {
                this.writer.AddDocument(document.GetLuceneDocument);
            }
            this.totalWrites++;
        }
예제 #10
0
 /// <summary>
 /// Writes the specified document out to the index.
 /// </summary>
 /// <param name="document">The document to output.</param>
 public void Write(IndexDocument document)
 {
     this.Write(document, IndexLibrary.AnalyzerType.None);
 }
예제 #11
0
        public static void WriteDefinition(this IndexWriter writer, Type typeToOutput, ReflectionTypes dataToExport)
        {
            if (typeToOutput == null)
            {
                throw new ArgumentNullException("typeToOutput", "typeToOutput cannot be null");
            }
            if (writer == null)
            {
                throw new ArgumentNullException("writer", "writer cannot be null");
            }
            if (!writer.IsOpen)
            {
                throw new ObjectDisposedException("writer", "You cannot access this method from a disposed IndexWriter");
            }

            string assemblyName                  = typeToOutput.Assembly.FullName;
            string codeTypeFieldName             = "CodeType";
            string assemblyFieldName             = "AssemblyName";
            string declaringTypeFieldName        = "DeclaringType";
            string declarationModifiersFieldName = "DeclarationModifiers";
            string declarationFieldName          = "Declaration";
            string parametersFieldName           = "Parameters";

            bool exportAll = ((dataToExport & ReflectionTypes.All) == ReflectionTypes.All);
            int  i         = 0;

            if (exportAll || (dataToExport & ReflectionTypes.Constructors) == ReflectionTypes.Constructors)
            {
                ConstructorInfo[] constructors = typeToOutput.GetConstructors();
                if (constructors != null)
                {
                    int totalConstructors = constructors.Length;
                    for (i = 0; i < totalConstructors; i++)
                    {
                        IndexDocument constructorDocument = new IndexDocument();
                        constructorDocument.Add(new FieldNormal(codeTypeFieldName, "Constructor", true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        constructorDocument.Add(new FieldNormal(assemblyFieldName, assemblyName, true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        constructorDocument.Add(new FieldNormal(declaringTypeFieldName, constructors[i].DeclaringType == null ? NO_VALUE : constructors[i].DeclaringType.Name, true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        constructorDocument.Add(new FieldNormal(declarationModifiersFieldName, GetAccessParameters(constructors[i]), true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        constructorDocument.Add(new FieldNormal(declarationFieldName, constructors[i].Name, true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        constructorDocument.Add(new FieldNormal(parametersFieldName, GetParameterString(constructors[i]), true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        writer.Write(constructorDocument);
                    }
                }
            }
            if (exportAll || (dataToExport & ReflectionTypes.Properties) == ReflectionTypes.Properties)
            {
                PropertyInfo[] properties = typeToOutput.GetProperties();
                if (properties != null)
                {
                    int totalProperties = properties.Length;
                    for (i = 0; i < totalProperties; i++)
                    {
                        IndexDocument propertyDocument = new IndexDocument();
                        propertyDocument.Add(new FieldNormal(codeTypeFieldName, "Property", true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        propertyDocument.Add(new FieldNormal(assemblyFieldName, assemblyName, true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        propertyDocument.Add(new FieldNormal(declaringTypeFieldName, properties[i].DeclaringType == null ? NO_VALUE : properties[i].DeclaringType.Name, true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        propertyDocument.Add(new FieldNormal(declarationModifiersFieldName, GetAccessParameters(properties[i]), true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        propertyDocument.Add(new FieldNormal(declarationFieldName, properties[i].Name, true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        propertyDocument.Add(new FieldNormal(parametersFieldName, NO_VALUE, true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        writer.Write(propertyDocument);
                    }
                }
            }
            if (exportAll || (dataToExport & ReflectionTypes.NestedTypes) == ReflectionTypes.NestedTypes)
            {
                Type[] nestedTypes = typeToOutput.GetNestedTypes();
                if (nestedTypes != null)
                {
                    int totalNestedTypes = nestedTypes.Length;
                    for (i = 0; i < totalNestedTypes; i++)
                    {
                        IndexDocument nestedDocument = new IndexDocument();
                        nestedDocument.Add(new FieldNormal(codeTypeFieldName, "NestedType", true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        nestedDocument.Add(new FieldNormal(assemblyFieldName, assemblyName, true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        nestedDocument.Add(new FieldNormal(declaringTypeFieldName, nestedTypes[i].DeclaringType == null ? NO_VALUE : nestedTypes[i].DeclaringType.Name, true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        nestedDocument.Add(new FieldNormal(declarationModifiersFieldName, GetAccessParameters(nestedTypes[i]), true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        nestedDocument.Add(new FieldNormal(declarationFieldName, nestedTypes[i].Name, true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        nestedDocument.Add(new FieldNormal(parametersFieldName, NO_VALUE, true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        writer.Write(nestedDocument);
                    }
                }
            }
            if (exportAll || (dataToExport & ReflectionTypes.Methods) == ReflectionTypes.Methods)
            {
                MethodInfo[] methods = typeToOutput.GetMethods();
                if (methods != null)
                {
                    int totalMethods = methods.Length;
                    for (i = 0; i < totalMethods; i++)
                    {
                        IndexDocument methodDocument = new IndexDocument();
                        methodDocument.Add(new FieldNormal(codeTypeFieldName, "Method", true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        methodDocument.Add(new FieldNormal(assemblyFieldName, assemblyName, true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        methodDocument.Add(new FieldNormal(declaringTypeFieldName, methods[i].DeclaringType == null ? NO_VALUE : methods[i].DeclaringType.Name, true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        methodDocument.Add(new FieldNormal(declarationModifiersFieldName, GetAccessParameters(methods[i]), true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        methodDocument.Add(new FieldNormal(declarationFieldName, methods[i].Name, true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        methodDocument.Add(new FieldNormal(parametersFieldName, GetParameterString(methods[i]), true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        writer.Write(methodDocument);
                    }
                }
            }
            if (exportAll || (dataToExport & ReflectionTypes.Members) == ReflectionTypes.Members)
            {
                MemberInfo[] members = typeToOutput.GetMembers();
                if (members != null)
                {
                    int totalMembers = members.Length;
                    for (i = 0; i < totalMembers; i++)
                    {
                        IndexDocument memberDocument = new IndexDocument();
                        memberDocument.Add(new FieldNormal(codeTypeFieldName, "Member", true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        memberDocument.Add(new FieldNormal(assemblyFieldName, assemblyName, true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        memberDocument.Add(new FieldNormal(declaringTypeFieldName, members[i].DeclaringType == null ? NO_VALUE : members[i].DeclaringType.Name, true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        memberDocument.Add(new FieldNormal(declarationModifiersFieldName, NO_VALUE, true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        memberDocument.Add(new FieldNormal(declarationFieldName, members[i].Name, true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        memberDocument.Add(new FieldNormal(parametersFieldName, NO_VALUE, true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        writer.Write(memberDocument);
                    }
                }
            }
            if (exportAll || (dataToExport & ReflectionTypes.Interfaces) == ReflectionTypes.Interfaces)
            {
                Type[] interfaces = typeToOutput.GetInterfaces();
                if (interfaces != null)
                {
                    int totalInterfaces = interfaces.Length;
                    for (i = 0; i < totalInterfaces; i++)
                    {
                        IndexDocument interfaceDocument = new IndexDocument();
                        interfaceDocument.Add(new FieldNormal(codeTypeFieldName, "Interface", true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        interfaceDocument.Add(new FieldNormal(assemblyFieldName, assemblyName, true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        interfaceDocument.Add(new FieldNormal(declaringTypeFieldName, interfaces[i].DeclaringType == null ? NO_VALUE : interfaces[i].DeclaringType.Name, true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        interfaceDocument.Add(new FieldNormal(declarationModifiersFieldName, GetAccessParameters(interfaces[i]), true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        interfaceDocument.Add(new FieldNormal(declarationFieldName, interfaces[i].Name, true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        interfaceDocument.Add(new FieldNormal(parametersFieldName, NO_VALUE, true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        writer.Write(interfaceDocument);
                    }
                }
            }
            if (exportAll || (dataToExport & ReflectionTypes.Fields) == ReflectionTypes.Fields)
            {
                FieldInfo[] fields = typeToOutput.GetFields();
                if (fields != null)
                {
                    int totalFields = fields.Length;
                    for (i = 0; i < totalFields; i++)
                    {
                        IndexDocument fieldDocument = new IndexDocument();
                        fieldDocument.Add(new FieldNormal(codeTypeFieldName, "Field", true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        fieldDocument.Add(new FieldNormal(assemblyFieldName, assemblyName, true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        fieldDocument.Add(new FieldNormal(declaringTypeFieldName, fields[i].DeclaringType == null ? NO_VALUE : fields[i].DeclaringType.Name, true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        fieldDocument.Add(new FieldNormal(declarationModifiersFieldName, NO_VALUE, true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        fieldDocument.Add(new FieldNormal(declarationFieldName, fields[i].Name, true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        fieldDocument.Add(new FieldNormal(parametersFieldName, GetAccessParameters(fields[i]), true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        writer.Write(fieldDocument);
                    }
                }
            }
            if (exportAll || (dataToExport & ReflectionTypes.Events) == ReflectionTypes.Events)
            {
                EventInfo[] events = typeToOutput.GetEvents();
                if (events != null)
                {
                    int totalEvents = events.Length;
                    for (i = 0; i < totalEvents; i++)
                    {
                        IndexDocument eventDocument = new IndexDocument();
                        eventDocument.Add(new FieldNormal(codeTypeFieldName, "Event", true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        eventDocument.Add(new FieldNormal(assemblyFieldName, assemblyName, true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        eventDocument.Add(new FieldNormal(declaringTypeFieldName, events[i].DeclaringType == null ? NO_VALUE : events[i].DeclaringType.Name, true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        eventDocument.Add(new FieldNormal(declarationModifiersFieldName, NO_VALUE, true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        eventDocument.Add(new FieldNormal(declarationFieldName, events[i].Name, true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        eventDocument.Add(new FieldNormal(parametersFieldName, GetAccessParameters(events[i].GetRaiseMethod()), true, FieldSearchableRule.Analyzed, FieldVectorRule.No));
                        writer.Write(eventDocument);
                    }
                }
            }
        }
예제 #12
0
        public static void WriteInstance <TKey>(this IndexWriter writer, TKey objectToWrite, IndexWriterRuleCollection ruleCollection)
            where TKey : class
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer", "writer cannot be null");
            }
            if (!writer.IsOpen)
            {
                throw new ObjectDisposedException("writer", "You cannot access this method from a disposed IndexWriter");
            }
            if (objectToWrite == null)
            {
                throw new ArgumentNullException("objectToWrite", "objectToWrite cannot be null");
            }
            if (ruleCollection == null)
            {
                throw new ArgumentNullException("ruleCollection", "storage cannot be null");
            }
            List <PropertyInfo> properties = new List <PropertyInfo>(objectToWrite.GetType().GetProperties());

            properties.RemoveAll(x => !x.CanRead);
            if (properties.Count == 0)
            {
                throw new InvalidOperationException("You cannot index a class that doesn't have any publicly accessible (get) properties");
            }
            int           totalProperties = properties.Count;
            IndexDocument document        = new IndexDocument();
            int           addedProperties = 0;

            for (int i = 0; i < totalProperties; i++)
            {
                PropertyInfo    info          = properties[i];
                IndexWriterRule rule          = ruleCollection.GetRuleFromType(info.PropertyType);
                object          propertyValue = info.GetValue(objectToWrite, null);
                if (rule.SkipColumnIfNull && propertyValue == null)
                {
                    continue;
                }
                else if (!rule.SkipColumnIfNull && string.IsNullOrEmpty(rule.DefaultValueIfNull) && propertyValue == null)
                {
                    continue;
                }

                string stringValue = string.Empty;
                if (propertyValue == null)
                {
                    stringValue = rule.DefaultValueIfNull;
                }
                else
                {
                    stringValue = propertyValue.ToString();
                }
                propertyValue = null;

                FieldStorage storage = (rule.AppliedColumns.ContainsKey(info.Name)) ? rule.AppliedColumns[info.Name] : rule.DefaultStorageRule;
                document.Add(new FieldNormal(info.Name, stringValue, storage.Store, storage.SearchRule, storage.VectorRule));
                ++addedProperties;
            }

            if (addedProperties > 0)
            {
                writer.Write(document);
            }
        }