public static NumericField SetIntValue(this NumericField f, int?value) { if (value.HasValue) { f.SetIntValue(value.Value); } else { f.SetIntValue(int.MinValue); } return(f); }
private void button1_Click(object sender, EventArgs e) { StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_30); Index = new RAMDirectory(); IndexWriter writer = new IndexWriter(Index, analyzer, new IndexWriter.MaxFieldLength(15)); foreach (var book in Books) { var doc = new Document(); var bookId = new NumericField("Id", Field.Store.YES, false); bookId.SetIntValue(book.Id); var bookTitle = new Field("Title", book.Title, Field.Store.YES, Field.Index.ANALYZED); doc.Add(bookId); doc.Add(bookTitle); foreach (var page in book.Pages) { var pageNo = new NumericField("no", Field.Store.YES, false); pageNo.SetIntValue(page.No); doc.Add(pageNo); doc.Add(new Field("Text", page.Text, Field.Store.YES, Field.Index.ANALYZED)); } writer.AddDocument(doc); } writer.Dispose(); }
private static AbstractField CreateField(IndexFieldInfo fieldInfo) { NumericField nf; switch (fieldInfo.Type) { case FieldInfoType.StringField: return(new Field(fieldInfo.Name, fieldInfo.Value, fieldInfo.Store, fieldInfo.Index, fieldInfo.TermVector)); case FieldInfoType.IntField: nf = new NumericField(fieldInfo.Name, fieldInfo.Store, fieldInfo.Index != Field.Index.NO); nf.SetIntValue(Int32.Parse(fieldInfo.Value, CultureInfo.InvariantCulture)); return(nf); case FieldInfoType.LongField: nf = new NumericField(fieldInfo.Name, 8, fieldInfo.Store, fieldInfo.Index != Field.Index.NO); nf.SetLongValue(Int64.Parse(fieldInfo.Value, CultureInfo.InvariantCulture)); return(nf); case FieldInfoType.SingleField: nf = new NumericField(fieldInfo.Name, fieldInfo.Store, fieldInfo.Index != Field.Index.NO); nf.SetFloatValue(Single.Parse(fieldInfo.Value, CultureInfo.InvariantCulture)); return(nf); case FieldInfoType.DoubleField: nf = new NumericField(fieldInfo.Name, 8, fieldInfo.Store, fieldInfo.Index != Field.Index.NO); nf.SetDoubleValue(Double.Parse(fieldInfo.Value, CultureInfo.InvariantCulture)); return(nf); default: throw new NotImplementedException("IndexFieldInfo." + fieldInfo.Type); } }
/// <summary> /// /// </summary> /// <param name="docId"></param> /// <param name="rng"></param> /// <param name="ctx"></param> /// <param name="percentageWithSpatialFields">ensures that some documents are missing spatial fields. This forces the cache to use FixedBitSet rather than MatchAllBits or MatchNoBits</param> /// <returns></returns> private Document CreateRandomDoc(int docId, Random rng, SpatialContext ctx, double percentageWithSpatialFields) { var doc = new Document(); var idField = new NumericField("locationId", Field.Store.YES, true); idField.SetIntValue(docId); doc.Add(idField); if (rng.NextDouble() > percentageWithSpatialFields) { return(doc); } Point shape = ctx.MakePoint(DistanceUtils.NormLonDEG(rng.NextDouble() * 360.0), DistanceUtils.NormLatDEG(rng.NextDouble() * 180.0)); foreach (AbstractField field in _spatialStrategy.CreateIndexableFields(shape)) { doc.Add(field); } doc.Add(_spatialStrategy.CreateStoredField(shape)); return(doc); }
internal static NumericField SetValue(this NumericField field, ValueType value) { if (value.GetType().IsEnum) { value = (ValueType)Convert.ChangeType(value, Enum.GetUnderlyingType(value.GetType())); } if (value is int) { return(field.SetIntValue((int)value)); } if (value is long) { return(field.SetLongValue((long)value)); } if (value is double) { return(field.SetDoubleValue((double)value)); } if (value is float) { return(field.SetFloatValue((float)value)); } throw new ArgumentException("Unable to store ValueType " + value.GetType() + " as NumericField.", "value"); }
public void FillDocument(Song song, Document doc) { if (!string.IsNullOrWhiteSpace(song.RelativePath)) { doc.Add(new Field(nameof(song.RelativePath), song.RelativePath, Field.Store.YES, Field.Index.ANALYZED)); } if (!string.IsNullOrWhiteSpace(song.Name)) { doc.Add(new Field(nameof(song.Name), song.Name, Field.Store.YES, Field.Index.ANALYZED)); } if (!string.IsNullOrWhiteSpace(song.Artist)) { doc.Add(new Field(nameof(song.Artist), song.Artist, Field.Store.YES, Field.Index.ANALYZED)); } var fieldFileLenght = new NumericField(nameof(song.FileLength), Field.Store.YES, false); fieldFileLenght.SetIntValue(song.FileLength); doc.Add(fieldFileLenght); var fieldLenght = new NumericField(nameof(song.Length), Field.Store.YES, false); fieldLenght.SetIntValue((int)song.Length.TotalMilliseconds); doc.Add(fieldLenght); doc.Add(new Field(nameof(song.LastModified), DateTools.DateToString(song.LastModified, DateTools.Resolution.SECOND), Field.Store.YES, Field.Index.NOT_ANALYZED)); }
private void AddFakeDocument(int fromNodeId) { //minimal fakeobject: NodeId, VersionId, Path, Version, NodeTimestamp, VersionTimestamp var node = Node.LoadNode(fromNodeId); var doc = IndexDocumentInfo.CreateDocument(node); doc.RemoveField(LucObject.FieldName.NodeId); doc.RemoveField(LucObject.FieldName.VersionId); doc.RemoveField(LucObject.FieldName.Name); doc.RemoveField(LucObject.FieldName.Path); var nf = new NumericField(LucObject.FieldName.NodeId, LucField.Store.YES, true); nf.SetIntValue(99999); doc.Add(nf); nf = new NumericField(LucObject.FieldName.VersionId, LucField.Store.YES, true); nf.SetIntValue(99999); doc.Add(nf); doc.Add(new LucField(LucObject.FieldName.Name, "fakedocument", LucField.Store.YES, LucField.Index.NOT_ANALYZED, LucField.TermVector.NO)); doc.Add(new LucField(LucObject.FieldName.Path, "/root/fakedocument", LucField.Store.YES, LucField.Index.NOT_ANALYZED, LucField.TermVector.NO)); LuceneManager.AddCompleteDocument(doc, 0, false); LuceneManager.UnregisterActivity(0, false); }
static void IndexItems(List <Product> list) { Lucene.Net.Store.Directory directory = FSDirectory.Open(new DirectoryInfo(@"D:\Index")); IndexWriter indexWriter = new IndexWriter( directory, new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30), true, IndexWriter.MaxFieldLength.LIMITED); foreach (Product product in list) { Document doc = new Document(); Field productName = new Field("ProductName", product.ProductName, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO); doc.Add(productName); NumericField price = new NumericField("Price", Field.Store.YES, true); price.SetIntValue(product.Price); doc.Add(price); indexWriter.AddDocument(doc); } indexWriter.Optimize(); indexWriter.Commit(); }
private static IFieldable GetCategoryField(CategoryProductDisplayOrder order) { var numericField = new NumericField(GetCategoryFieldName(order.Category.Id), Field.Store.YES, true); numericField.SetIntValue(order.DisplayOrder); return(numericField); }
/// <summary> /// Recursively tokenize the object into fields whose names are available via dot notation. /// </summary> /// <param name="obj"></param> /// <param name="prefix"></param> /// <param name="dictionary"></param> private static void TokenizeObject(JObject obj, string prefix, ref Dictionary <string, AbstractField> dictionary) { if (obj == null) { return; } //TODO: Add property-based ("$propertyName") conventions that allow the parameters to be customized. foreach (var property in obj.Properties().Where(p => p.Value != null)) { var fieldName = String.IsNullOrEmpty(prefix) ? property.Name : prefix + "." + property.Name; switch (property.Value.Type) { case JTokenType.Date: var dateString = DateTools.DateToString((DateTime)property.Value, DateTools.Resolution.MILLISECOND); var dateField = new Field(fieldName, dateString, Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.YES); dictionary.Add(fieldName, dateField); break; case JTokenType.TimeSpan: var timeSpanField = new NumericField(fieldName, Field.Store.YES, true); timeSpanField.SetLongValue(((TimeSpan)property.Value).Ticks); dictionary.Add(fieldName, timeSpanField); break; case JTokenType.Integer: var intField = new NumericField(fieldName, Field.Store.YES, true); intField.SetIntValue((int)property.Value); dictionary.Add(fieldName, intField); break; case JTokenType.Float: var floatField = new NumericField(fieldName, Field.Store.YES, true); floatField.SetFloatValue((float)property.Value); dictionary.Add(fieldName, floatField); break; case JTokenType.Guid: var guidField = new Field(fieldName, property.Value.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.NO); dictionary.Add(fieldName, guidField); break; case JTokenType.Object: TokenizeObject(property.Value as JObject, fieldName, ref dictionary); break; default: if (String.IsNullOrEmpty(property.Value.ToString()) == false) { var stringField = new Field(fieldName, property.Value.ToString(), Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS); dictionary.Add(fieldName, stringField); } break; } } }
public void BuildIndex() { var dirInfo = new System.IO.DirectoryInfo(indexFolder); using (var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30)) using (var indexDir = FSDirectory.Open(dirInfo)) { using (var indexWriter = new IndexWriter(indexDir, analyzer, IndexWriter.MaxFieldLength.UNLIMITED)) { foreach (var fileName in System.IO.Directory.GetFiles(baseFolder)) { var xmlDoc = XDocument.Load(fileName); string docid = xmlDoc.Root.Attribute("docid").Value; // remove older index entry var searchQuery = new TermQuery(new Term("exact", docid)); indexWriter.DeleteDocuments(searchQuery); // Create a Lucene document... Document document = new Document(); document.Add(new Field("exact", docid, Field.Store.YES, Field.Index.NOT_ANALYZED)); foreach (var element in xmlDoc.Root.Descendants()) { var name = element.Name.LocalName.ToLower(); var value = (element.Value ?? string.Empty).Trim().ToUpper(); switch (name) { case "rank": var numericField = new NumericField("rank", Field.Store.YES, true); numericField.SetIntValue(Convert.ToInt32(value)); document.Add(numericField); break; case "doj": var dateField = new Field(name, DateTools.DateToString(Convert.ToDateTime(value), DateTools.Resolution.MILLISECOND), Field.Store.YES, Field.Index.NOT_ANALYZED); document.Add(dateField); break; default: var field = new Field(name, value, Field.Store.YES, Field.Index.ANALYZED); document.Add(field); break; } } //add doc indexWriter.AddDocument(document); } } } }
protected static Document CloneDocument(Document luceneDoc) { var clonedDocument = new Document(); foreach (AbstractField field in luceneDoc.GetFields()) { var numericField = field as NumericField; if (numericField != null) { var clonedNumericField = new NumericField(numericField.Name, numericField.IsStored ? Field.Store.YES : Field.Store.NO, numericField.IsIndexed); var numericValue = numericField.NumericValue; if (numericValue is int) { clonedNumericField.SetIntValue((int)numericValue); } else if (numericValue is long) { clonedNumericField.SetLongValue((long)numericValue); } else if (numericValue is double) { clonedNumericField.SetDoubleValue((double)numericValue); } else if (numericValue is float) { clonedNumericField.SetFloatValue((float)numericValue); } clonedDocument.Add(clonedNumericField); } else { Field clonedField; if (field.IsBinary) { clonedField = new Field(field.Name, field.GetBinaryValue(), field.IsStored ? Field.Store.YES : Field.Store.NO); } else if (field.StringValue != null) { clonedField = new Field(field.Name, field.StringValue, field.IsStored ? Field.Store.YES : Field.Store.NO, field.IsIndexed ? Field.Index.ANALYZED_NO_NORMS : Field.Index.NOT_ANALYZED_NO_NORMS, field.IsTermVectorStored ? Field.TermVector.YES : Field.TermVector.NO); } else { //probably token stream, and we can't handle fields with token streams, so we skip this. continue; } clonedDocument.Add(clonedField); } } return(clonedDocument); }
private static Document CreateDocument(string content, DateTime filterDate) { var document = new Document(); document.Add(new Field(TextFieldName, content, Field.Store.YES, Field.Index.ANALYZED)); var numfield = new NumericField(DateFieldName, Field.Store.YES, true); numfield.SetIntValue(int.Parse(filterDate.ToString(NumericDateFormat))); document.Add(numfield); return(document); }
private static Document CloneDocument(Document luceneDoc) { var clonedDocument = new Document(); foreach (AbstractField field in luceneDoc.GetFields()) { var numericField = field as NumericField; if (numericField != null) { var clonedNumericField = new NumericField(numericField.Name(), numericField.IsStored() ? Field.Store.YES : Field.Store.NO, numericField.IsIndexed()); var numericValue = numericField.GetNumericValue(); if (numericValue is int) { clonedNumericField.SetIntValue((int)numericValue); } else if (numericValue is long) { clonedNumericField.SetLongValue((long)numericValue); } else if (numericValue is double) { clonedNumericField.SetDoubleValue((double)numericValue); } else if (numericValue is float) { clonedNumericField.SetFloatValue((float)numericValue); } clonedDocument.Add(clonedNumericField); } else { Field clonedField; if (field.IsBinary()) { clonedField = new Field(field.Name(), field.BinaryValue(), field.IsStored() ? Field.Store.YES : Field.Store.NO); } else { clonedField = new Field(field.Name(), field.StringValue(), field.IsStored() ? Field.Store.YES : Field.Store.NO, field.IsIndexed() ? Field.Index.ANALYZED_NO_NORMS : Field.Index.NOT_ANALYZED_NO_NORMS); } clonedDocument.Add(clonedField); } } return(clonedDocument); }
/// <summary> /// Indexes a given Rootobject into the index /// </summary> /// <param name="root">The Rootobject to index</param> public void IndexRootobject(Rootobject root) { foreach (Passage passage in root.passages) { Lucene.Net.Documents.Document doc = new Document(); Lucene.Net.Documents.NumericField isSelectedField = new NumericField(FIELDS_FN[0], Field.Store.YES, true); isSelectedField.SetIntValue(passage.is_selected); doc.Add(isSelectedField); if (!String.IsNullOrEmpty(passage.url)) { Lucene.Net.Documents.Field urlField = new Field(FIELDS_FN[1], passage.url, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES); doc.Add(urlField); } if (!String.IsNullOrEmpty(passage.passage_text)) { Lucene.Net.Documents.Field passageTextField = new Field(FIELDS_FN[2], passage.passage_text, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES); doc.Add(passageTextField); } Lucene.Net.Documents.NumericField passageIDField = new NumericField(FIELDS_FN[3], Field.Store.YES, true); passageIDField.SetIntValue(passage.passage_ID); doc.Add(passageIDField); writer.AddDocument(doc); //Commented out code for indexing query data in json file /*Lucene.Net.Documents.NumericField passageField = new NumericField("passage"+passage.passage_ID, Field.Store.YES, true); * passageField.SetIntValue(passage.passage_ID); * doc.Add(passageField);*/ } //Commented out code for indexing query data in json file /*Lucene.Net.Documents.NumericField queryIDField = new NumericField("query_id", Field.Store.YES, true); * queryIDField.SetIntValue(root.query_id); * doc.Add(queryIDField); * int i = 0; * foreach(string answer in root.answers) * { * Lucene.Net.Documents.Field answerField = new Field("answer"+i, answer, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES); * doc.Add(answerField); * i++; * } * Lucene.Net.Documents.Field QueryTypeField = new Field("query_type", root.query_type, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES); * Lucene.Net.Documents.Field QueryField = new Field("query", root.query, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES); * doc.Add(QueryTypeField); * doc.Add(QueryField); * writer.AddDocument(doc);*/ }
/// <summary> /// 将T转换成doc /// </summary> /// <param name="model"></param> /// <param name="fieldModelList"></param> /// <returns></returns> private Document ParseModeltoDoc(T model, IEnumerable <FieldDataModel> fieldModelList) { Document document = new Document(); Type type = model.GetType(); foreach (var item in fieldModelList) { PropertyInfo propertyInfo = type.GetProperty(item.PropertyName); var propertyValue = propertyInfo.GetValue(model); if (propertyValue != null) { string valueString = propertyValue.ToString(); IFieldable fieldable = null; if (item.FieldType == TypeCode.String) { fieldable = new Field(item.FieldName, valueString, item.Store, item.Index, item.TermVector); } else { NumericField numericField = new NumericField(item.FieldName, item.Store, item.Index == Field.Index.ANALYZED_NO_NORMS); switch (item.FieldType) { case TypeCode.Double: numericField.SetDoubleValue(Convert.ToDouble(valueString)); break; case TypeCode.Single: numericField.SetFloatValue(Convert.ToSingle(valueString)); break; case TypeCode.Int32: numericField.SetIntValue(Convert.ToInt32(valueString)); break; case TypeCode.Int64: numericField.SetLongValue(Convert.ToInt64(valueString)); break; default: break; } fieldable = numericField; } document.Add(fieldable); } } return(document); }
/// <summary> /// Converts the list item to document. /// </summary> /// <param name="listItem">The list item.</param> /// <returns>Document.</returns> public static Document ConvertListItemToDocument(SPListItem listItem) { var tokenDictionary = new Dictionary <string, AbstractField>(); foreach (var field in listItem.Fields.OfType <SPField>().Where(f => f.Hidden == false)) { switch (field.Type) { case SPFieldType.DateTime: var dateString = DateTools.DateToString((DateTime)listItem[field.Id], DateTools.Resolution.MILLISECOND); var dateField = new Field(field.Title, dateString, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES); tokenDictionary.Add(field.Title, dateField); break; case SPFieldType.Integer: var intField = new NumericField(field.Title, Field.Store.YES, true); intField.SetIntValue((int)listItem[field.Id]); tokenDictionary.Add(field.Title, intField); break; case SPFieldType.Number: var numberField = new NumericField(field.Title, Field.Store.YES, true); numberField.SetFloatValue(Convert.ToSingle(listItem[field.Id])); tokenDictionary.Add(field.Title, numberField); break; default: var textValue = field.GetFieldValueAsText(listItem[field.Id]); var stringField = new Field(field.Title, textValue, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS); tokenDictionary.Add(field.Title, stringField); break; } } var doc = new Document(); //Add individual fields. foreach (var kvp in tokenDictionary) { doc.Add(kvp.Value); } return(doc); }
/// <summary> /// 创建索引 /// </summary> /// <param name="Datas"></param> public void CreateIndexByData(string ID, int AccessLevel, Dictionary <string, string> Datas) { //索引文档保存位置 var indexPath = System.Configuration.ConfigurationManager.AppSettings["IndexArchivePath"]; using (FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NativeFSLockFactory())) { //IndexReader:对索引库进行读取的类 bool isExist = IndexReader.IndexExists(directory); //是否存在索引库文件夹以及索引库特征文件 if (isExist) { //如果索引目录被锁定(比如索引过程中程序异常退出或另一进程在操作索引库),则解锁 //Q:存在问题 如果一个用户正在对索引库写操作 此时是上锁的 而另一个用户过来操作时 将锁解开了 于是产生冲突 --解决方法后续 if (IndexWriter.IsLocked(directory)) { IndexWriter.Unlock(directory); } } //创建向索引库写操作对象 IndexWriter(索引目录,指定使用盘古分词进行切词,最大写入长度限制) //补充:使用IndexWriter打开directory时会自动对索引库文件上锁 using (IndexWriter writer = new IndexWriter(directory, new PanGuAnalyzer(), !isExist, IndexWriter.MaxFieldLength.UNLIMITED)) { //--------------------------------遍历数据源 将数据转换成为文档对象 存入索引库 Document document = new Document(); //new一篇文档对象 --一条记录对应索引库中的一个文档 //向文档中添加字段 Add(字段,值,是否保存字段原始值,是否针对该列创建索引) document.Add(new Field("id", ID, Field.Store.YES, Field.Index.NOT_ANALYZED));//--所有字段的值都将以字符串类型保存 因为索引库只存储字符串类型数据 // 访问级别字段 var numberField = new NumericField("AccessLevel", Field.Store.YES, true); numberField.SetIntValue(AccessLevel); document.Add(numberField); //Field.Store:表示是否保存字段原值。指定Field.Store.YES的字段在检索时才能用document.Get取出原值 //Field.Index.NOT_ANALYZED:指定不按照分词后的结果保存--是否按分词后结果保存取决于是否对该列内容进行模糊查询 //WITH_POSITIONS_OFFSETS:指示不仅保存分割后的词 还保存词之间的距离 foreach (KeyValuePair <string, string> item in Datas) { document.Add(new Field(item.Key, item.Value, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS)); } writer.AddDocument(document); //文档写入索引库 } } }
private IFieldable CreateLuceneField(LuceneField field) { var isStored = field.IsStored ? Field.Store.YES : Field.Store.NO; var isAnalyzed = field.IsAnalyzed ? Field.Index.ANALYZED_NO_NORMS : Field.Index.NOT_ANALYZED_NO_NORMS; var termVector = field.UseTermVector ? Field.TermVector.YES : Field.TermVector.NO; switch (field.DataType) { case LuceneFieldDataType.Long: var numericStorageValue = field.Value.ParseLongOrNull().ErrorIfNull(string.Format("Unable to convert string '{0}' to Numeric Field", field.Value)); var numericField = new NumericField(field.Name, 0, isStored, true); numericField.SetLongValue(numericStorageValue); return(numericField); case LuceneFieldDataType.Date: var dateStorageValue = field.Value.ParseIntOrNull().ErrorIfNull(string.Format("Unable to convert date string '{0}' to numeric", field.Value)); var luceneField = new NumericField(field.Name, isStored, true); luceneField.SetIntValue(dateStorageValue); return(luceneField); case LuceneFieldDataType.DateTime: var dateTimeStorageValue = field.Value.ParseLongOrNull().ErrorIfNull(string.Format("Unable to convert date string '{0}' to numeric", field.Value)); var dateTimeField = new NumericField(field.Name, isStored, true); dateTimeField.SetLongValue(dateTimeStorageValue); return(dateTimeField); case LuceneFieldDataType.String: default: return(new Field(field.Name, field.Value ?? String.Empty, isStored, isAnalyzed, termVector)); } }
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); }
internal static NumericField SetValue(this NumericField field, ValueType value) { if (value is int) { return(field.SetIntValue((int)value)); } if (value is long) { return(field.SetLongValue((long)value)); } if (value is double) { return(field.SetDoubleValue((double)value)); } if (value is float) { return(field.SetFloatValue((float)value)); } throw new ArgumentException("Unable to store ValueType " + value.GetType() + " as NumericField.", "value"); }
private static void SetKarmaAsNumericField(Examine.LuceneEngine.DocumentWritingEventArgs e) { //Get existing field karma field - some members may not have a value set - so check in case var existingField = e.Document.GetField("reputationCurrent"); if (existingField != null) { //Add new field that is numeric & prefix with the magic '__Sort_' string var karmaField = new NumericField(LuceneIndexer.SortedFieldNamePrefix + "karma", Field.Store.YES, true); //Get the existing value that is stored on the vanilla property that stores as strings var existingValue = existingField.StringValue(); //Convert to int var valAsInt = Convert.ToInt32(existingValue); //Set it on the karma field karmaField.SetIntValue(valAsInt); //Add the field to the document e.Document.Add(karmaField); } }
private static void CreateIndexContent() { using (FSDirectory directory = FSDirectory.Open(new DirectoryInfo(m_directoryPath), new NativeFSLockFactory())) //指定索引文件(打开索引目录) FS指的是就是FileSystem { bool isUpdate = IndexReader.IndexExists(directory); //IndexReader:对索引进行读取的类。该语句的作用:判断索引库文件夹是否存在以及索引特征文件是否存在。 if (isUpdate) { //同时只能有一段代码对索引库进行写操作。当使用IndexWriter打开directory时会自动对索引库文件上锁。 //如果索引目录被锁定(比如索引过程中程序异常退出),则首先解锁(提示一下:如果我现在正在写着已经加锁了,但是还没有写完,这时候又来一个请求,那么不就解锁了吗?这个问题后面会解决) if (IndexWriter.IsLocked(directory)) { IndexWriter.Unlock(directory); } } using (IndexWriter writer = new IndexWriter(directory, new PanGuAnalyzer(), !isUpdate, IndexWriter.MaxFieldLength.UNLIMITED)) //向索引库中写索引。这时在这里加锁。 { //如果队列中有数据,获取队列中的数据写到Lucene.Net中。 while (Queue.Count > 0) { KeyValuePair <T, LuceneTypeEnum> keyValuePair = Queue.Dequeue(); T model = keyValuePair.Key; Type type = model.GetType(); PropertyInfo propertyInfo = type.GetProperty(m_luceneDataModels[0].PropertyName); var propertyValue = propertyInfo.GetValue(model); string valueString = propertyValue != null?propertyValue.ToString() : null; writer.DeleteDocuments(new Term(m_luceneDataModels[0].FieldName, valueString)); //删除 if (keyValuePair.Value == LuceneTypeEnum.Delete) { continue; } //表示一篇文档。 Document document = new Document(); //Field.Store.YES:表示是否存储原值。只有当Field.Store.YES在后面才能用doc.Get("number")取出值来.Field.Index. NOT_ANALYZED:不进行分词保存 //Field.Index. ANALYZED:进行分词保存:也就是要进行全文的字段要设置分词 保存(因为要进行模糊查询 //Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS:不仅保存分词还保存分词的距离。 foreach (FieldDataModel item in m_luceneDataModels) { propertyInfo = type.GetProperty(item.PropertyName); propertyValue = propertyInfo.GetValue(model); if (propertyValue != null) { valueString = propertyValue.ToString(); IFieldable fieldable = null; if (item.FieldType == TypeCode.String) { fieldable = new Field(item.FieldName, valueString, item.Store, item.Index, item.TermVector); } else { NumericField numericField = new NumericField(item.FieldName, item.Store, item.Index == Field.Index.ANALYZED_NO_NORMS); switch (item.FieldType) { case TypeCode.Double: numericField.SetDoubleValue(Convert.ToDouble(valueString)); break; case TypeCode.Single: numericField.SetFloatValue(Convert.ToSingle(valueString)); break; case TypeCode.Int32: numericField.SetIntValue(Convert.ToInt32(valueString)); break; case TypeCode.Int64: numericField.SetLongValue(Convert.ToInt64(valueString)); break; default: break; } fieldable = numericField; } document.Add(fieldable); } } writer.AddDocument(document); } } //会自动解锁。 } }
/// <summary> /// This method generate the fields for indexing documents in lucene from the values. /// Given a name and a value, it has the following behavior: /// * If the value is enumerable, index all the items in the enumerable under the same field name /// * If the value is null, create a single field with the supplied name with the unanalyzed value 'NULL_VALUE' /// * If the value is string or was set to not analyzed, create a single field with the supplied name /// * If the value is date, create a single field with millisecond precision with the supplied name /// * If the value is numeric (int, long, double, decimal, or float) will create two fields: /// 1. with the supplied name, containing the numeric value as an unanalyzed string - useful for direct queries /// 2. with the name: name +'_Range', containing the numeric value in a form that allows range queries /// </summary> private IEnumerable <AbstractField> CreateFields(string name, JToken value, Field.Store defaultStorage, Field.TermVector defaultTermVector) { if (name.IsNullOrWhiteSpace()) { throw new ArgumentException(@"Field must be not null, not empty and cannot contain whitespace", "name"); } var fieldIndexingOptions = m_indexDefinition.GetIndex(name, null); var storage = m_indexDefinition.GetStorage(name, defaultStorage); var termVector = m_indexDefinition.GetTermVector(name, defaultTermVector); if (Equals(fieldIndexingOptions, Field.Index.NOT_ANALYZED) || Equals(fieldIndexingOptions, Field.Index.NOT_ANALYZED_NO_NORMS))// explicitly not analyzed { // date time, time span and date time offset have the same structure for analyzed and not analyzed. if (value.Type != JTokenType.Date && value.Type != JTokenType.TimeSpan) { yield return(new Field(name, value.ToString(), storage, m_indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS), termVector)); } } else { switch (value.Type) { case JTokenType.Array: { //Add each item in the array as a field with the same name. //Return an _IsArray field. if (Equals(storage, Field.Store.NO) == false) { yield return(new Field(name + "_IsArray", "true", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS, Field.TermVector.NO)); } var jArray = value as JArray; if (jArray == null) { throw new InvalidOperationException("Shouldn't Happen"); } foreach (var arrayValue in jArray) { if (CanCreateFieldsForNestedArray(arrayValue, fieldIndexingOptions) == false) { continue; } foreach (var field in CreateFields(name, arrayValue, storage, Field.TermVector.NO)) { yield return(field); } } break; } case JTokenType.Boolean: { yield return(new Field(name, (value.Value <bool>()) ? "true" : "false", storage, m_indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS), termVector)); } break; case JTokenType.Bytes: { var bytes = value.Value <byte[]>(); if (bytes != null) { yield return(CreateBinaryField(name, bytes, storage, fieldIndexingOptions, termVector)); } } break; case JTokenType.Date: { var val = value.Value <DateTime>(); var dateAsString = val.ToString(Default.DateTimeFormatsToWrite); if (val.Kind == DateTimeKind.Utc) { dateAsString += "Z"; } yield return(new Field(name, dateAsString, storage, m_indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS), termVector)); } break; case JTokenType.Guid: { yield return(new Field(name, value.Value <Guid>().ToString(), storage, m_indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS), termVector)); } break; case JTokenType.None: case JTokenType.Null: { yield return(new Field(name, Constants.NullValue, storage, Field.Index.NOT_ANALYZED_NO_NORMS, Field.TermVector.NO)); } break; case JTokenType.Object: { //Add an _IsObject field if (Equals(storage, Field.Store.NO) == false) { yield return(new Field(name + "_IsObject", "true", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS, Field.TermVector.NO)); } //Recursively add properties on the object. foreach (var objectValue in value.Children <JProperty>()) { if (CanCreateFieldsForNestedObject(objectValue, fieldIndexingOptions) == false) { continue; } foreach (var field in CreateFields(name + "." + objectValue.Name, objectValue.Value, storage, defaultTermVector)) { yield return(field); } } } break; case JTokenType.String: { if (Equals(value.Value <string>(), string.Empty)) { yield return(new Field(name, Constants.EmptyString, storage, Field.Index.NOT_ANALYZED_NO_NORMS, Field.TermVector.NO)); yield break; } var index = m_indexDefinition.GetIndex(name, Field.Index.ANALYZED); yield return(new Field(name, value.ToString(), storage, index, termVector)); } break; case JTokenType.Float: { var f = value.Value <float>(); var index = m_indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS); yield return(new Field(name, f.ToString(CultureInfo.InvariantCulture), storage, index, termVector)); var numericField = new NumericField(name + "_Range", storage, true); if (m_indexDefinition.GetSortOption(name) == SortOptions.Double) { yield return(numericField.SetDoubleValue(value.Value <double>())); } else { yield return(numericField.SetFloatValue(value.Value <float>())); } } break; case JTokenType.Integer: { var i = value.Value <int>(); var index = m_indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS); yield return(new Field(name, i.ToString(CultureInfo.InvariantCulture), storage, index, termVector)); var numericField = new NumericField(name + "_Range", storage, true); if (m_indexDefinition.GetSortOption(name) == SortOptions.Long) { yield return(numericField.SetLongValue(value.Value <long>())); } else { yield return(numericField.SetIntValue(value.Value <int>())); } } break; case JTokenType.TimeSpan: { var val = value.Value <TimeSpan>(); var index = m_indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS); yield return(new Field(name, val.ToString("c"), storage, index, termVector)); var numericField = new NumericField(name + "_Range", storage, true); yield return(numericField.SetLongValue(val.Ticks)); } break; case JTokenType.Uri: { yield return(new Field(name, value.Value <Uri>().ToString(), storage, m_indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS), termVector)); } break; case JTokenType.Undefined: case JTokenType.Raw: case JTokenType.Property: case JTokenType.Constructor: case JTokenType.Comment: //Do Nothing... break; default: throw new ArgumentOutOfRangeException("The specified JToken Type: " + value.Type + " is invalid or has not been implemented."); } } }
// Add to index internal static void _addToLuceneIndex(ClassifiedAd data, IndexWriter writer) { // remove older index entry var searchQuery = new TermQuery(new Term("Id", data.Id.ToString())); writer.DeleteDocuments(searchQuery); // add new index entry var doc = new Document(); // add lucene fields mapped to db fields doc.Add(new Field("Id", data.Id.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.Add(new Field("StringId", data.StringId, Field.Store.YES, Field.Index.NOT_ANALYZED)); // AdInfo foreach (var ai in data.AdInfo) { if (!String.IsNullOrEmpty(ai.Description)) { if (ai.Name.Equals("Mileage")) { var mil = new NumericField("Mileage", Field.Store.YES, true); mil.SetIntValue(ai.IntDescription); doc.Add(mil); } else if (ai.Name.Equals("Year")) { var yr = new NumericField("Year", Field.Store.YES, true); yr.SetIntValue(ai.IntDescription); doc.Add(yr); } else if (ai.Name.Equals("Size")) { var size = new NumericField("Size", Field.Store.YES, true); size.SetIntValue(ai.IntDescription); doc.Add(size); } else { doc.Add(new Field(ai.Name, ai.Description, Field.Store.YES, Field.Index.NOT_ANALYZED)); } } } // Photo if (data.AdPhotos != null) { if (data.AdPhotos.Count > 0) { doc.Add(new Field("AdPhoto", data.AdPhotos.SingleOrDefault(x => x.SetThumbnail == true).FileName, Field.Store.YES, Field.Index.NOT_ANALYZED)); } } // Default // Title doc.Add(new Field("Title", data.Title, Field.Store.YES, Field.Index.ANALYZED)); // Descripton var hfd = HttpUtility.HtmlDecode(data.HtmlFreeDescription); doc.Add(new Field("HtmlFreeDescription", hfd, Field.Store.YES, Field.Index.ANALYZED)); // Price var price = new NumericField("Price", Field.Store.YES, true); price.SetIntValue(data.Price); doc.Add(price); // Price Info doc.Add(new Field("PriceInfo", data.PriceInfo, Field.Store.YES, Field.Index.NOT_ANALYZED)); // Status doc.Add(new Field("Status", data.Status.ToString(), Field.Store.YES, Field.Index.ANALYZED)); // Ad Type doc.Add(new Field("AdType", data.AdType, Field.Store.YES, Field.Index.NOT_ANALYZED)); // Featured Status doc.Add(new Field("FeaturedAdStatus", data.FeaturedAdStatus.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); // Top Ad Status doc.Add(new Field("TopAdStatus", data.TopAdStatus.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); // Urgent Ad Status doc.Add(new Field("UrgentAdStatus", data.UrgentAdStatus.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); // Category doc.Add(new Field("CategoryId", data.Category.Id.ToString(), Field.Store.YES, Field.Index.ANALYZED)); doc.Add(new Field("CategoryName", data.Category.Name, Field.Store.YES, Field.Index.ANALYZED)); // SubCategory doc.Add(new Field("SubCategoryStringId", data.SubCategory.stringId, Field.Store.YES, Field.Index.ANALYZED)); doc.Add(new Field("SubCategoryName", data.SubCategory.Name, Field.Store.YES, Field.Index.ANALYZED)); // Time doc.Add(new Field("TimeStamp", data.TimeStamp.ToShortDateString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); // Time Tick var tick = new NumericField("EditTimeStampTicks", Field.Store.YES, true); tick.SetLongValue(data.EditTimeStamp.Ticks); doc.Add(tick); doc.Add(new Field("EditTimeStamp", data.EditTimeStamp.ToShortDateString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); // Location doc.Add(new Field("CountryId", data.Country.Id.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.Add(new Field("CountryName", data.Country.Name, Field.Store.YES, Field.Index.ANALYZED)); doc.Add(new Field("RegionId", data.Region.Id.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.Add(new Field("RegionName", data.Region.Name, Field.Store.YES, Field.Index.ANALYZED)); // add entry to index writer.AddDocument(doc); }
public void CreateIndexByFile(int FileID, int AccessLevel, int AttachID, Dictionary <string, string> Datas) { var attachInfo = _IObjectAttachService.Get(AttachID); // 2 : word, 8 : txt if (attachInfo.Type == 2 || attachInfo.Type == 8) { //索引文档保存位置 var indexPath = System.Configuration.ConfigurationManager.AppSettings["IndexFilePath"]; using (FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NativeFSLockFactory())) { //IndexReader:对索引库进行读取的类 bool isExist = IndexReader.IndexExists(directory); //是否存在索引库文件夹以及索引库特征文件 if (isExist) { //如果索引目录被锁定(比如索引过程中程序异常退出或另一进程在操作索引库),则解锁 //Q:存在问题 如果一个用户正在对索引库写操作 此时是上锁的 而另一个用户过来操作时 将锁解开了 于是产生冲突 --解决方法后续 if (IndexWriter.IsLocked(directory)) { IndexWriter.Unlock(directory); } } //创建向索引库写操作对象 IndexWriter(索引目录,指定使用盘古分词进行切词,最大写入长度限制) //补充:使用IndexWriter打开directory时会自动对索引库文件上锁 using (IndexWriter writer = new IndexWriter(directory, new PanGuAnalyzer(), !isExist, IndexWriter.MaxFieldLength.UNLIMITED)) { //--------------------------------遍历数据源 将数据转换成为文档对象 存入索引库 Document document = new Document(); //new一篇文档对象 --一条记录对应索引库中的一个文档 //向文档中添加字段 Add(字段,值,是否保存字段原始值,是否针对该列创建索引) document.Add(new Field("id", FileID.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));//--所有字段的值都将以字符串类型保存 因为索引库只存储字符串类型数据 // 访问级别字段 var numberField = new NumericField("AccessLevel", Field.Store.YES, true); numberField.SetIntValue(AccessLevel); document.Add(numberField); foreach (KeyValuePair <string, string> item in Datas) { document.Add(new Field(item.Key, item.Value, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS)); } //Field.Store:表示是否保存字段原值。指定Field.Store.YES的字段在检索时才能用document.Get取出原值 //Field.Index.NOT_ANALYZED:指定不按照分词后的结果保存--是否按分词后结果保存取决于是否对该列内容进行模糊查询 //WITH_POSITIONS_OFFSETS:指示不仅保存分割后的词 还保存词之间的距离 if (attachInfo.Type == 8) { // txt var reader = File.OpenText(attachInfo.Path); reader.ReadToEnd(); using (System.IO.StreamReader sr = new System.IO.StreamReader(attachInfo.Path, Encoding.Default)) { var txt = sr.ReadToEnd(); document.Add(new Field("content", txt, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS)); } } else { // word var wordFile = new Aspose.Words.Document(attachInfo.Path); var txt = wordFile.GetText(); document.Add(new Field("content", txt, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS)); } writer.AddDocument(document); //文档写入索引库 } } } }
/// <summary> /// This method generate the fields for indexing documents in lucene from the values. /// Given a name and a value, it has the following behavior: /// * If the value is enumerable, index all the items in the enumerable under the same field name /// * If the value is null, create a single field with the supplied name with the unanalyzed value 'NULL_VALUE' /// * If the value is string or was set to not analyzed, create a single field with the supplied name /// * If the value is date, create a single field with millisecond precision with the supplied name /// * If the value is numeric (int, long, double, decimal, or float) will create two fields: /// 1. with the supplied name, containing the numeric value as an unanalyzed string - useful for direct queries /// 2. with the name: name +'_Range', containing the numeric value in a form that allows range queries /// </summary> private static IEnumerable <AbstractField> CreateFields(string name, object value, IndexDefinition indexDefinition, Field.Store defaultStorage) { if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentException("Field must be not null, not empty and cannot contain whitespace", "name"); } if (char.IsLetter(name[0]) == false && name[0] != '_') { name = "_" + name; } if (value == null) { yield return(new Field(name, "NULL_VALUE", indexDefinition.GetStorage(name, defaultStorage), Field.Index.NOT_ANALYZED)); yield break; } if (value is DynamicNullObject) { if (((DynamicNullObject)value).IsExplicitNull) { yield return(new Field(name, "NULL_VALUE", indexDefinition.GetStorage(name, defaultStorage), Field.Index.NOT_ANALYZED)); } yield break; } if (value is AbstractField) { yield return((AbstractField)value); yield break; } var itemsToIndex = value as IEnumerable; if (itemsToIndex != null && ShouldTreatAsEnumerable(itemsToIndex)) { yield return(new Field(name + "_IsArray", "true", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS)); foreach (var itemToIndex in itemsToIndex) { foreach (var field in CreateFields(name, itemToIndex, indexDefinition, defaultStorage)) { yield return(field); } } yield break; } if (indexDefinition.GetIndex(name, null) == Field.Index.NOT_ANALYZED)// explicitly not analyzed { yield return(new Field(name, value.ToString(), indexDefinition.GetStorage(name, defaultStorage), indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED))); yield break; } if (value is string) { var index = indexDefinition.GetIndex(name, Field.Index.ANALYZED); yield return(new Field(name, value.ToString(), indexDefinition.GetStorage(name, defaultStorage), index)); yield break; } if (value is DateTime) { yield return(new Field(name, DateTools.DateToString((DateTime)value, DateTools.Resolution.MILLISECOND), indexDefinition.GetStorage(name, defaultStorage), indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED))); } else if (value is bool) { yield return(new Field(name, ((bool)value) ? "true" : "false", indexDefinition.GetStorage(name, defaultStorage), indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED))); } else if (value is IConvertible) // we need this to store numbers in invariant format, so JSON could read them { var convert = ((IConvertible)value); yield return(new Field(name, convert.ToString(CultureInfo.InvariantCulture), indexDefinition.GetStorage(name, defaultStorage), indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED))); } else if (value is DynamicJsonObject) { var inner = ((DynamicJsonObject)value).Inner; yield return(new Field(name + "_ConvertToJson", "true", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS)); yield return(new Field(name, inner.ToString(), indexDefinition.GetStorage(name, defaultStorage), indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED))); } else { yield return(new Field(name + "_ConvertToJson", "true", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS)); yield return(new Field(name, JToken.FromObject(value).ToString(), indexDefinition.GetStorage(name, defaultStorage), indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED))); } var numericField = new NumericField(name + "_Range", indexDefinition.GetStorage(name, defaultStorage), true); if (value is int) { if (indexDefinition.GetSortOption(name) == SortOptions.Long) { yield return(numericField.SetLongValue((int)value)); } else { yield return(numericField.SetIntValue((int)value)); } } if (value is long) { yield return(numericField .SetLongValue((long)value)); } if (value is decimal) { yield return(numericField .SetDoubleValue((double)(decimal)value)); } if (value is float) { if (indexDefinition.GetSortOption(name) == SortOptions.Double) { yield return(numericField.SetDoubleValue((float)value)); } else { yield return(numericField.SetFloatValue((float)value)); } } if (value is double) { yield return(numericField .SetDoubleValue((double)value)); } }
public static Document ToDocument(this SearchDocument content) { var doc = new Document(); doc.Add(new Field(SearchDocument.IdentifierKey, content.Identifier.ToString(), Field.Store.YES, Field.Index.NO)); doc.Add(new Field(SearchDocument.UserIdentifierKey, content.UserIdentifier.ToString(), Field.Store.YES, Field.Index.ANALYZED)); doc.Add(new Field(SearchDocument.MemberNameKey, content.MemberName, Field.Store.YES, Field.Index.ANALYZED)); var typeField = new NumericField(SearchDocument.TypeKey, Field.Store.YES, true); typeField.SetIntValue((int)content.Type); doc.Add(typeField); var latField = new NumericField(SearchDocument.LatitudeKey, Field.Store.YES, true); latField.SetDoubleValue(content.Latitude); doc.Add(latField); var longField = new NumericField(SearchDocument.LongitudeKey, Field.Store.YES, true); longField.SetDoubleValue(content.Longitude); doc.Add(longField); doc.Add(new Field(SearchDocument.CreatedOnKey, content.CreatedOn.ToStringExact(), Field.Store.YES, Field.Index.NO)); var permissions = content.Permissions(); if (!string.IsNullOrWhiteSpace(permissions)) { doc.Add(new Field(SearchDocument.PermissionsKey, permissions, Field.Store.YES, Field.Index.NO)); } if (!string.IsNullOrWhiteSpace(content.ImageData)) { doc.Add(new Field(SearchDocument.ImageDataKey, content.ImageData, Field.Store.YES, Field.Index.NO)); } if (!string.IsNullOrWhiteSpace(content.Title)) { doc.Add(new Field(SearchDocument.TitleKey, content.Title, Field.Store.YES, Field.Index.NO)); } if (!string.IsNullOrWhiteSpace(content.Description)) { doc.Add(new Field(SearchDocument.DescriptionKey, content.Description, Field.Store.YES, Field.Index.NO)); } if (!string.IsNullOrWhiteSpace(content.Key)) { doc.Add(new Field(SearchDocument.KeyKey, content.Key, Field.Store.YES, Field.Index.NOT_ANALYZED)); } if (!string.IsNullOrWhiteSpace(content.Content)) { doc.Add(new Field(SearchDocument.ContentKey, content.Content, Field.Store.YES, Field.Index.ANALYZED)); } if (!string.IsNullOrWhiteSpace(content.Location)) { doc.Add(new Field(SearchDocument.LocationKey, content.Location, Field.Store.YES, Field.Index.ANALYZED)); } return(doc); }
/// <summary> /// 将对象转成Lucene里面的Document类型 /// </summary> /// <param name="obj"></param> /// <param name="columnFields"></param> /// <returns></returns> public static Document Convert(object obj, ColumnField[] columnFields) { Document document = null; if (obj != null) { document = new Document(); Type type = obj.GetType(); foreach (ColumnField columnField in columnFields) { if (columnField.IsCustomScore) {//为文档增加自定义评分列 NumericField numericField = new NumericField(columnField.Column, columnField.Store, true); numericField.SetIntValue(1); document.Add(numericField); columnField.Type = FieldType.INT32; continue; } Lucene.Net.Documents.Field.Store store = columnField.Store; string fieldName = columnField.Column; PropertyInfo propertyInfo = type.GetProperty(fieldName); if (propertyInfo != null) { object value = propertyInfo.GetValue(obj, null); if (value != null) { if (columnField.Index == Field.Index.ANALYZED ||//分词建索引 columnField.Index == Field.Index.ANALYZED_NO_NORMS) //分词建索引,不支持权重 { Field field = new Field(fieldName, value.ToString(), store, columnField.Index); field.Boost = columnField.Boost; document.Add(field); columnField.Type = FieldType.STRING; } else { string typeName = value.GetType().Name; if (typeName == FieldType.SINGLE) { NumericField numericField = new NumericField(fieldName, store, true); numericField.SetFloatValue(System.Convert.ToSingle(value)); numericField.Boost = columnField.Boost; document.Add(numericField); columnField.Type = FieldType.SINGLE; } else if (typeName == FieldType.DOUBLE || typeName == FieldType.DECIMAL) { NumericField numericField = new NumericField(fieldName, store, true); numericField.SetDoubleValue(System.Convert.ToDouble(value)); numericField.Boost = columnField.Boost; document.Add(numericField); columnField.Type = FieldType.DOUBLE; } else if (typeName == FieldType.INT32) { NumericField numericField = new NumericField(fieldName, store, true); numericField.SetIntValue(System.Convert.ToInt32(value)); numericField.Boost = columnField.Boost; document.Add(numericField); columnField.Type = FieldType.INT32; } else if (typeName == FieldType.INT64) { NumericField numericField = new NumericField(fieldName, store, true); numericField.SetLongValue(System.Convert.ToInt64(value)); numericField.Boost = columnField.Boost; document.Add(numericField); columnField.Type = FieldType.INT64; } else if (typeName == FieldType.DATETIME) { NumericField numericField = new NumericField(fieldName, store, true); DateTime dateTime = System.Convert.ToDateTime(value); numericField.SetLongValue(dateTime.Ticks); numericField.Boost = columnField.Boost; document.Add(numericField); columnField.Type = FieldType.DATETIME; } else { Field field = new Field(fieldName, value.ToString(), store, columnField.Index); field.Boost = columnField.Boost; document.Add(field); columnField.Type = FieldType.STRING; } } } } } } return(document); }
internal static Document CreateDocument(IndexDocumentInfo info, IndexDocumentData docData) { if (info is NotIndexedIndexDocumentInfo) { return(null); } var doc = new Document(); foreach (var fieldInfo in info.fields) { if (fieldInfo.Name != "Password" && fieldInfo.Name != "PasswordHash") { doc.Add(CreateField(fieldInfo)); } } var path = docData.Path.ToLowerInvariant(); doc.Add(CreateStringField(LucObject.FieldName.Name, RepositoryPath.GetFileName(path), NameFieldIndexingInfo)); doc.Add(CreateStringField(LucObject.FieldName.Path, path, PathFieldIndexingInfo)); //LucObject.FieldName.Depth var nf = new NumericField(LucObject.FieldName.Depth, Field.Store.YES, true); nf.SetIntValue(DepthIndexHandler.GetDepth(docData.Path)); doc.Add(nf); //LucObject.FieldName.InTree //var fields = InTreeIndexHandlerInstance.GetIndexFields(LucObject.FieldName.InTree, docData.Path); var fields = CreateInTreeFields(LucObject.FieldName.InTree, docData.Path); foreach (var field in fields) { doc.Add(field); } //LucObject.FieldName.InFolder doc.Add(CreateInFolderField(LucObject.FieldName.InFolder, path)); //LucObject.FieldName.ParentId nf = new NumericField(LucObject.FieldName.ParentId, Field.Store.NO, true); nf.SetIntValue(docData.ParentId); doc.Add(nf); //LucObject.FieldName.IsSystem //doc.RemoveFields(LucObject.FieldName.IsSystem); doc.Add(new Field(LucObject.FieldName.IsSystem, docData.IsSystem ? BooleanIndexHandler.YES : BooleanIndexHandler.NO, Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.NO)); // flags doc.Add(new Field(LucObject.FieldName.IsLastPublic, docData.IsLastPublic ? BooleanIndexHandler.YES : BooleanIndexHandler.NO, Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.NO)); doc.Add(new Field(LucObject.FieldName.IsLastDraft, docData.IsLastDraft ? BooleanIndexHandler.YES : BooleanIndexHandler.NO, Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.NO)); // timestamps nf = new NumericField(LucObject.FieldName.NodeTimestamp, Field.Store.YES, true); nf.SetLongValue(docData.NodeTimestamp); doc.Add(nf); nf = new NumericField(LucObject.FieldName.VersionTimestamp, Field.Store.YES, true); nf.SetLongValue(docData.VersionTimestamp); doc.Add(nf); // custom fields if (info.HasCustomField) { var customFields = CustomIndexFieldManager.GetFields(info, docData); if (customFields != null) { foreach (var field in customFields) { doc.Add(field); } } } return(doc); }