生成Es索引

需求說明

elasticsearch索引問題,致使數(shù)據(jù)查詢時(shí)不能根據(jù)一個(gè)索引完成。Es索引本來式在配置文件中讀取,故障發(fā)生后,約定好:隨著時(shí)間的推移自動(dòng)生成Es索引。

以下為代碼實(shí)現(xiàn),分別生成:本月和上月的索引;截至到本月所有的索引。
/**
 * @author: localhost
 * @program: kq-itmb-relly
 * @description: 本月和上月的EsIndex
 * @create: 2019-01-24 16:20
 **/
public class LastEsIndex {
    public static  List<String> getLastEsIndex(Date date) throws ParseException {
        List<String> strList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String str = sdf.format(date);
        String newEsIndex;
        String[] split = str.split("-");
        String newCurMonth;
        String lastMonth;
        if (split[0].equals("2018")) {
            newCurMonth = "vehilcepasshk";
        } else {
            newCurMonth = "vehilcepasshk-" + split[0] + "." + split[1];
        }
        //上月
        Calendar c = Calendar.getInstance();
        c.setTime(sdf.parse(str));
        c.add(Calendar.MONTH, -1);
        String s = sdf.format(c.getTime());
        String[] split1 = s.split("-");
        if (split1[0].equals("2018")) {
            lastMonth = "vehilcepasshk";
        } else {
            lastMonth = "vehilcepasshk-" + split1[0] + "." + split1[1];
        }
        strList.add(lastMonth);
        strList.add(newCurMonth);
        return strList;
    }
}

  /**
   * @author: localhost
   * @program: kq-itmb-relly
   * @description: 截至到本月之前所有的es索引
   * @create: 2019-01-24 16:06
   **/
  public class AllEsIndex {
  
      public static String getAllEsindex(Date date) throws ParseException {
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
          String time = sdf.format(date);
          String newEsIndex;
          newEsIndex = "vehilcepasshk,";
          List<String> list = getMonthBetween("2019-01-01", time);
          for (String str : list) {
              String[] split = str.split("-");
              String newStr = "vehilcepasshk-" + split[0] + "." + split[1] + ",";
              newEsIndex += newStr;
          }
          String ss = newEsIndex.substring(0, newEsIndex.length() - 1);
          return ss;
      }
  
      //獲取指定日期之間的所有年月
      private static List<String> getMonthBetween(String minDate, String maxDate) throws ParseException {
          ArrayList<String> result = new ArrayList<String>();
          //格式化為年月
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
  
          Calendar min = Calendar.getInstance();
          Calendar max = Calendar.getInstance();
  
          min.setTime(sdf.parse(minDate));
          min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
  
          max.setTime(sdf.parse(maxDate));
          max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
  
          Calendar curr = min;
          while (curr.before(max)) {
              result.add(sdf.format(curr.getTime()));
              curr.add(Calendar.MONTH, 1);
          }
  
          return result;
      }
  }
測試類
/**
 * @author: localhost
 * @program: kq-itmb-relly
 * @description: 獲取這個(gè)月和上月生成的索引
 * @create: 2019-01-23 18:12
 **/
public class TimeTest {
    public static void main(String[] args) throws ParseException {
        List<String> lastEsIndex = LastEsIndex.getLastEsIndex(new Date());
        //******************本周以及上周***************
        System.out.println(lastEsIndex.get(0));
        System.out.println(lastEsIndex.get(1));
        //******************截至到目前***************
        String allEsindex = AllEsIndex.getAllEsindex(new Date());
        System.out.println(allEsindex);
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容