首页 > 代码库 > Lucene 4.4 根据Int类型字段删除索引
Lucene 4.4 根据Int类型字段删除索引
1、方法一,通过Term删除 Term构造中没有,Int类型需要转换成Lucene自带的类BytesRef 。 /** * 根据商品ID删除索引文件 * @param id */ public void deleteDocument(int id) {; File file = new File("E://index"); IKAnalyzer analyzer = new IKAnalyzer(); IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_44, analyzer); IndexWriter indexWriter = null; Directory directory; try { directory = FSDirectory.open(file); // 创建 IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig); BytesRef bytes = new BytesRef(NumericUtils.BUF_SIZE_INT); NumericUtils.intToPrefixCoded(id, 0, bytes); Term term = new Term(field, bytes); indexWriter.deleteDocuments(term); indexWriter.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } 2.方法二,使用NumericRangeQuery /** * 根据商品ID删除索引文件 * @param id */ public void deleteDocument(int id) {; File file = new File("E://index"); IKAnalyzer analyzer = new IKAnalyzer(); IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_44, analyzer); IndexWriter indexWriter = null; Directory directory; try { directory = FSDirectory.open(file); // 创建 IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig); Query numbericRangeQuery = NumericRangeQuery.newIntRange("ID",id-1, id+1, false, false); indexWriter.deleteDocuments(numbericRangeQuery); indexWriter.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
Lucene 4.4 根据Int类型字段删除索引
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。