/// <summary> /// Update the specified data in the index /// </summary> /// <param name='data'> /// If set to <c>true</c> data. /// </param> public bool Update(T data, LuceneUpdateOptions updateOptions) { if (_disposed) { throw new ObjectDisposedException("Resource was disposed."); } if (writer == null) { throw new ArgumentNullException("indexwriter not opened!"); } Document doc = createDocument(data, updateOptions); String id = String.Empty; String fieldname = String.Empty; if (getId(data, out id, out fieldname)) { doc = GetUpdateDocument(id, doc, updateOptions); if (!String.IsNullOrEmpty(id)) { log.Debug("update document with id: " + id + " idfield: " + fieldname); //Console.WriteLine("update document with id: " + id + " idfield: " + fieldname); writer.UpdateDocument(new Lucene.Net.Index.Term(fieldname, id), doc); return(true); } else { log.Warn("Update: could not find id"); } } return(false); }
private bool AddField(XmlNode node, LuceneUpdateOptions updateOptions) { if (updateOptions == LuceneUpdateOptions.Empty) { return(true); } if (updateOptions.UpdateMode == LuceneUpdateOptions.UpdateModes.Normal) { return(true); } if (updateOptions.UpdateMode == LuceneUpdateOptions.UpdateModes.SelectedFields) { if (updateOptions.Fields.Count == 0) { throw new Exception("no fields specified for mode 'UpdateModes.SelectedFields'"); } String fieldName = FieldDescription.GetFieldName(node); return(updateOptions.Fields.Any(f => f.Equals(fieldName, StringComparison.OrdinalIgnoreCase))); } return(false); }
void UpdateFields(Document sourceDocument, Document input, LuceneUpdateOptions options) { foreach (var field in sourceDocument.GetFields()) { if (!options.Fields.Any(f => f.Equals(field.Name, StringComparison.OrdinalIgnoreCase))) { input.Add(field); } } // foreach (var field in options.Fields) { // sourceDocument.RemoveField(field); // Field f = input.GetField(field); // sourceDocument.Add(f); // } }
private Document GetUpdateDocument(String id, Document input, LuceneUpdateOptions options) { if (options.UpdateMode == LuceneUpdateOptions.UpdateModes.Normal) { return(input); } if (options.UpdateMode == LuceneUpdateOptions.UpdateModes.SelectedFields) { Document sourceDocument = GetDocument(id); if (sourceDocument != null) { UpdateFields(sourceDocument, input, options); return(input); } } return(null); }
private Document createDocument(object data, LuceneUpdateOptions updateOptions) { try { Document ret = new Document(); FileInfo fi = Sharpend.Configuration.ConfigurationManager.getConfigFile("searchdescription.xml"); if (!fi.Exists) { throw new Exception("could not load searchdescription.xml"); } String className = data.GetType().ToString(); String assembly = data.GetType().Assembly.FullName; assembly = assembly.Split(',')[0]; log.Debug("create new document for class " + className + " assembly " + assembly); XmlNodeList lst = Sharpend.Configuration.ConfigurationManager.getValues("searchdescription.xml", "//field[(../../@class='" + className + "') and (../../@assembly='" + assembly + "')]"); if (lst.Count == 0) { log.Warn("there are no matching fields specified for this class: " + className + " assembly" + assembly); } foreach (XmlNode nd in lst) { if (AddField(nd, updateOptions)) { Field f = FieldDescription.CreateInstance(data, nd); if (VerboseMode) { log.Debug("add new field: Name: " + f.Name + " ToString: " + f.ToString()); } ret.Add(f); } } return(ret); } catch (Exception ex) { log.Error(ex); throw; } }