转了个简单的代码

1.判断索引文件是否存在:
/**
     *
检查索引是否存在.
     * @param indexDir
     * @return
     */
    public static boolean indexExist(String indexDir)
    {
        return IndexReader.indexExists(indexDir);
    }
 private IndexWriter getWriter(String indexFilePath) throws Exception {
        boolean append=true;
        File file=new File(indexFilePath+File.separator+"segments");
        if(file.exists())
            append=false; 
        return new IndexWriter(indexFilePath,analyzer,append);
    }
 
2.删除索引
/**
     *
删除索引.
     * @param aTerm
索引删除条件
     * @param indexDir
索引目录
     */
    public static void deleteIndex(Term aTerm, String indexDir)
    {
        List aList = new ArrayList();
        aList.add(aTerm);
        deleteIndex(aList, indexDir);
    }
    /**
     *
删除索引.
     * @param aTerm
索引删除条件.
     * @param indexDir
索引目录     * 
     */
    public static void deleteIndex(List terms, String indexDir)
    {
        if (null == terms) {
            return;
        }
       
        if(!indexExist(indexDir)) { return; }
        IndexReader reader = null;
        try {
            reader = IndexReader.open(indexDir);
            for (int i = 0; i < terms.size(); i++){
                Term aTerm = (Term) terms.get(i);
                if (null != aTerm){
                    reader.delete(aTerm);
                }
            }
        } catch (IOException e){
            LogMan.warn("Error in Delete Index", e);
        } finally {
            try{
                if (null != reader){
                    reader.close();
                }
            }catch (IOException e){
                LogMan.warn("Close reader Error");
            }
        }
    } 
 删除索引需要一个条件,类似数据库中的字段条件,例如删除一条新闻的代码如下:
     public static void deleteNewsInfoIndex(int nid)
     {
         Term aTerm = new Term("nid", String.valueOf(nid));
         deleteIndex(aTerm,indexDir);
     }   
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值