コード例 #1
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);
                }
            }
        }
コード例 #2
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);
            }
        }