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);
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-03 01:58:11