码迷,mamicode.com
首页 > Web开发 > 详细

Lucene 4.4 根据Int类型字段删除索引

时间:2014-10-21 19:43:33      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:Lucene   blog   io   os   ar   使用   java   文件   2014   

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类型字段删除索引

标签:Lucene   blog   io   os   ar   使用   java   文件   2014   

原文地址:http://blog.csdn.net/dongdongleng/article/details/40348591

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!