Lucene的UpdateDocument

来源:转载


Lucene从根本上来讲,并不支持对Document的更新操作,它提供的UpdateDocument方法其实是先删除后新增。在实际使用过程中,发现该方法有如下限制:

1、设置的Term对应的Field必须是字符串类型

也就是如下的组合能正常执行:

Document document = new Document();document.Add(new Field("serviceid", serviceQueryResult.ServiceId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));document.Add(new NumericField("shopid", Field.Store.YES, true).SetIntValue(serviceQueryResult.ShopId));document.Add(new Field("servicename", serviceQueryResult.ServiceName, Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));writer.AddDocument(document);writer.UpdateDocument(new Term("serviceid", serviceQueryResult.ServiceId.ToString()), document);
若serviceid是整型的,则不能直接用UpdateDocument方法来更新必须先删除后创建,如下:

Document document = new Document();document.Add(new NumericField("serviceid", Field.Store.YES, true).SetIntValue(serviceQueryResult.ServiceId));document.Add(new NumericField("shopid", Field.Store.YES, true).SetIntValue(serviceQueryResult.ShopId));document.Add(new Field("servicename", serviceQueryResult.ServiceName, Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));writer.AddDocument(document);writer.DeleteDocuments(NumericRangeQuery.NewIntRange("serviceid", serviceQueryResult.ServiceId, serviceQueryResult.ServiceId, true, true));writer.AddDocument(document);






分享给朋友:
您可能感兴趣的文章:
随机阅读: