elasticsearch 常用統計查詢


 

1-# 各機構上報所有病例

GET rdr_inpat_front_medical_records/_search
{
"size": 0,
"aggs": {
"org_Code": {
"terms": {
"field": "DE08_10_052_00.keyword"

}
}
}

}

2-#嵌套類型利用腳本衍生新字段

GET rdr_inpat_nested_patient/_search
{
  "size":10,
   "script_fields": {
      "change_age_field": {	 
            "script": {
                "lang":   "painless",
                "source": "params['_source']['inpat_front_medical_records'].length"
            }
        },
        "test":{
            "script": {
              "lang":"painless",
              "source":"doc['SEX.keyword']+'aa'"
              
              
            } 
          
        }
   },
   "_source":{
            "includes":["fixedNo","SEX","birthday","dqmc_empi_visit.age"]
        }
}

 

3-# 統計每份病例下的診斷條數

GET /rdr_inpat_nested/_search
{
"size":0,
"query":{
"bool":{
"filter":[]
}
},
"aggregations":{
"DE01_00_014_00.keyword":{
"terms":{
"field":"DE01_00_014_00.keyword",
"size":1000,
"missing":"#NULL"
},
"aggregations":{
"diagnosis_aggs": {
"nested": {
"path": "inpat_diagnosis"
},
"aggregations": {
"min_pkId": {
"value_count": {
"field": "inpat_diagnosis.PK_ID"
}
}
}
}
}
}

}

}

4-ES使用painless腳本獲取時分秒

POST _scripts/ConvertStringToDate
{
   "script": {
            "lang": "painless",
            "source": """
            String fname = params.filedName;
            if(fname==null)
            {
              return -1;
              
            }
            String type = params.type;
         DateTimeFormatter dtf = DateTimeFormatter.ofPattern(
        "'date:' yyyy/MM/dd 'time:' HH:mm:ss");
ZonedDateTime zdt = ZonedDateTime.parse(fname, dtf);
       if(type!=null)
       {
         if(type=="year")
         {
           int year = zdt.getYear();
           return year;

            
         }
         if(type=="month")
         {
           int month = zdt.getMonthValue();
           return month;

           
         }
         if(type=="day")
         {
           int day = zdt.getDayOfMonth();
           return day;

           
         }
         if(type=="hour")
         {
           int hour = zdt.getHour();
           return hour;

           
         }
         if(type=="minute")
         {
           
           int minutes = zdt.getMinute();
           return minutes;

         }
         if(type=="second")
         {
           int seconds = zdt.getSecond();
           return seconds;
         }
         
       }
                  
                  return zdt;
           """
          }
}

  5-Es使用painless腳本轉化類型

POST _scripts/ConvertStringToInt
{
   "script": {
            "lang": "painless",
            "source": """
                    String type = params.type;
                    String fname = params.filedName;
               
	  try{
	               String fileValue= String.valueOf(doc[fname].value);
                    double iValue =   Double.parseDouble(fileValue);			 
                    return iValue;
	  }catch (Exception e){
                  return 0;  
                }
           """
          }
} 

 5-Es使用painless腳本時間年月轉數字

String fieldType = params.fieldType;
		   String fname = params.filedName;
		   if("date"==fieldType){Calendar c = Calendar.getInstance();JodaCompatibleZonedDateTime dateTime= doc[fname].value;c.setTime(Date.from(dateTime.toInstant()));int day=c.get(Calendar.DATE);int month = c.get(Calendar.MONTH);int iValue = month*100+day;
		   return iValue;}else{
		   int year = Integer.parseInt(doc[fname].value.substring(0, 4));
           int month = Integer.parseInt(doc[fname].value.substring(5, 7));
           int day = Integer.parseInt(doc[fname].value.substring(8, 10));
		   int iValue = month*100+day;
		   return iValue;
		   }

6-Es 寫入數據時利用腳本計算新變量(設置管道pipeLine)

                    IndexRequest request=  new IndexRequest(indexName, "doc",fixedNo ).source(map);
                  for(String s:scriptList)
                 {
                      request.setPipeline(s);//設置腳本管道
                  }

7-MYSQL類型與elasticsearch類型對照

  static String getMysqlToEsType(String type)
    {
        String esType="text";
        switch (type.toLowerCase())
        {
            case "tinyint":
            case "mediumint":
            case "smallint":
            case "int":
                esType="integer";
                break;
            case "decimal":
            case "double":
                esType="double";
                break;
            case "char":
            case "varchar":
            case "longtext":
            case "text":
            case "mediumtext":
            case "tinytext":
                esType="text";
                break;

            case "blob":
            case "binary":

                esType="binary";
                break;
            case "date":
            case "datetime":
            case "timestamp":
            case "time":
                esType="date";
                break;
            default:
                esType="text";


        }
        return esType;
    }

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM