1. 程式人生 > >簡單的solr在某個時間範圍內批量刪除資料

簡單的solr在某個時間範圍內批量刪除資料

直接上程式碼

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;

import javax.management.Query;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *批量清理solr資料
 */
public class SolrDelete {
    private final static String url ="http://20.26.20.79:9001/solr/";
    private final static String core = "mycore";
    public static void main(String [] arg0) throws Exception{
        List<Object> list = new ArrayList<>();
        SolrServer solrServer = new HttpSolrServer(url+core);
        Date date = new Date();
        long time1 = date.getTime();
        long l = time1 - (24 * 60 * 60 * 1000 * 3);//3Tina前毫秒值
        Date date1 = new Date(l);//轉換為時間格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat dateFormat1 = new SimpleDateFormat("HH:mm:ss");
        String time = dateFormat.format(date1);
        String format = dateFormat1.format(date1);
        String aa = time + "T" + format + "Z";//拼接solr時間格式
        //create_time:[2018-09-25T12:01:30Z TO 2018-09-30T12:09:30Z]
        String queryfq = "create_time:[* TO " + aa +"]";//過濾條件
        solrServer.deleteByQuery(queryfq);
        solrServer.commit();
        solrServer.shutdown();
    }
}