/// <summary> /// Removes a rule from this collection /// </summary> /// <param name="rule">The rule to remove.</param> public void RemoveRule(IndexWriterRule rule) { if (columns.Contains(rule)) { columns.Remove(rule); } }
/// <summary> /// Adds a rule to this collection /// </summary> /// <param name="rule">The rule to add.</param> public void AddRule(IndexWriterRule rule) { if (!columns.Contains(rule)) { columns.Add(rule); } }
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> /// Determines whether the specified <see cref="System.Object"/> is equal to this instance. /// </summary> /// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param> /// <returns> /// <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>. /// </returns> /// <exception cref="T:System.NullReferenceException"> /// The <paramref name="obj"/> parameter is null. /// </exception> public override bool Equals(object obj) { if (obj == null || obj.GetType() != typeof(IndexWriterRule)) { return(false); } IndexWriterRule tempRule = (IndexWriterRule)obj; return(tempRule.appliedType == this.appliedType); }
/// <summary> /// Gets a <see cref="IndexLibrary.IndexWriterRule"/> from the specified type /// </summary> /// <param name="ruleType">Type of the rule to grab.</param> /// <returns>If a rule with the specified type exists then that rule; otherwise a new rule for that type</returns> public IndexWriterRule GetRuleFromType(Type ruleType) { IndexWriterRule rule = columns.FirstOrDefault(x => x.AppliedType == ruleType); if (rule == null) { rule = new IndexWriterRule(ruleType); rule.DefaultValueIfNull = "false"; rule.MaxAllowedFieldLength = 1000; rule.OnlyIndexIfRuleContainsFieldName = false; rule.SkipColumnIfNull = true; this.columns.Add(rule); } return(rule); }
/// <summary> /// Determines if the specified fieldname should be skipped depending on the rule contained in this instance /// </summary> /// <param name="fieldType">Type of the field.</param> /// <param name="fieldName">Name of the field.</param> /// <returns></returns> /// <exception cref="System.ArgumentNullException"> /// The <paramref name="fieldType"/> parameter is null /// </exception> /// <exception cref="System.ArgumentNullException"> /// The <paramref name="fieldName"/> parameter is null or empty /// </exception> public bool SkipField(Type fieldType, string fieldName) { if (fieldType == null) { throw new ArgumentNullException("fieldType", "fieldType cannot be null"); } if (string.IsNullOrEmpty(fieldName)) { throw new ArgumentNullException("fieldName", "fieldName cannot be null or empty"); } IndexWriterRule rule = GetRuleFromType(fieldType); // no rule? include it! if (rule == null) { return(false); } return(rule.SkipField(fieldType, fieldName)); }
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); } }
/// <summary> /// Determines whether the specified rule is contained in this instance /// </summary> /// <param name="rule">The rule to search for.</param> /// <returns> /// <c>true</c> if the specified rule contains rule; otherwise, <c>false</c>. /// </returns> public bool ContainsRule(IndexWriterRule rule) { return(columns.Contains(rule)); }
/// <summary> /// Gets a <see cref="IndexLibrary.IndexWriterRule"/> from the specified type /// </summary> /// <param name="ruleType">Type of the rule to grab.</param> /// <returns>If a rule with the specified type exists then that rule; otherwise a new rule for that type</returns> public IndexWriterRule GetRuleFromType(Type ruleType) { IndexWriterRule rule = columns.FirstOrDefault(x => x.AppliedType == ruleType); if (rule == null) { rule = new IndexWriterRule(ruleType); rule.DefaultValueIfNull = "false"; rule.MaxAllowedFieldLength = 1000; rule.OnlyIndexIfRuleContainsFieldName = false; rule.SkipColumnIfNull = true; this.columns.Add(rule); } return rule; }
/// <summary> /// Determines whether the specified rule is contained in this instance /// </summary> /// <param name="rule">The rule to search for.</param> /// <returns> /// <c>true</c> if the specified rule contains rule; otherwise, <c>false</c>. /// </returns> public bool ContainsRule(IndexWriterRule rule) { return columns.Contains(rule); }
/// <summary> /// Adds a rule to this collection /// </summary> /// <param name="rule">The rule to add.</param> public void AddRule(IndexWriterRule rule) { if (!columns.Contains(rule)) columns.Add(rule); }
/// <summary> /// Removes a rule from this collection /// </summary> /// <param name="rule">The rule to remove.</param> public void RemoveRule(IndexWriterRule rule) { if (columns.Contains(rule)) columns.Remove(rule); }