#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Oct 14 15:46:44 2021

@author: rico
"""

from elasticsearch import Elasticsearch
import pandas as pd
import time
class ES_Client:
    
    def __init__(self,host,index_name):
        
        self.host = host
        self.index_name = index_name
        self.es_scroll_size = 1000
        self.node_dict = {
                 '数据一致性校验':'SAME_CHECK_FAILD',
                 '停用词校验':'STOP_WORD_FAILD',
                 '品牌提取与标化':'SYS_GET_BRAND_FAILD',
                 '类别提取与标化':'SYS_GET_CATEGORY_FAILD',
                 '非重点类型号提取与标化':'SYS_GET_MODEL_FAILD_NONPOINT',
                 '非还原类型号提取与标化':'SYS_GET_MODEL_FAILD_NONREDUCTIVE',
                 '还原类匹配项参数提取与标化':'SYS_GET_PARAMS_FAILD_REDUCTIVE',
                 '非还原类匹配项参数提取与标化':'SYS_GET_PARAMS_FAILD_NONREDUCTIVE',
                 '建库必填项参数提取':'SYS_GET_PARAMS_FAILD_POINT_END'
                 }
        self.basic_field = ['DOC_ID','DATA_PROJECT_NAME','DATA_BATCH','FLOW_NODE_STATUS','FLOW_FINISH_STATUS','SOURCE_CHANNEL_NAME','SOURCE_P_SKU','SOURCE_P_NAME','SOURCE_P_LASTCATEGORY_NAME','SOURCE_P_BRAND_NAME','SOURCE_P_MODEL','SOURCE_P_PARAMS','SOURCE_P_PRICE','SOURCE_P_URL']
        self.node_field = {
                 '数据一致性校验':[],#待定
                 '停用词校验':['DEAL_MATCH_STOP_WORDS','FLOW_FINISH_STATUS','FINISH_P_REMARK'],
                 '品牌提取与标化':['DEAL_RECOMMEND_BRAND_LIST','DEAL_CHOOSE_BRAND','DEAL_INPUT_BRAND','FINISH_P_BRAND_NAME','FINISH_P_BRAND_CODE'],
                 '类别提取与标化':['FINISH_P_BRAND_NAME','DEAL_REFLECTION_LASTCATEGORY_NAME','DEAL_REFLECTION_LASTCATEGORY_KEYWORDS','DEAL_REFLECTION_LASTCATEGORY_CONFIRM_KEYWORDS','DEAL_MODEL_LASTCATEGORY_NAME','DEAL_INPUT_LASTCATEGORY_NAME','FINISH_P_LASTCATEGORY_NAME','FINISH_P_LASTCATEGORY_CODE','FINISH_P_REMARK'],
                 '非重点类型号提取与标化':['FINISH_P_BRAND_NAME','FINISH_P_LASTCATEGORY_NAME','DEAL_DEVSCRIPT_MODEL','DEAL_PROSCRIPT_MODEL','DEAL_DB_MODEL','DEAL_INPUT_MODEL','FINISH_P_MODEL'],
                 '非还原类型号提取与标化':['FINISH_P_BRAND_NAME','FINISH_P_LASTCATEGORY_NAME','DEAL_DEVSCRIPT_MODEL','DEAL_PROSCRIPT_MODEL','DEAL_DB_MODEL','DEAL_INPUT_MODEL','FINISH_P_MODEL'],
                 '还原类匹配项参数提取与标化':['FINISH_P_BRAND_NAME','FINISH_P_LASTCATEGORY_NAME','DEAL_UNSTD_PARAMS_DICT','DEAL_STD_PARAMS_DICT','DEAL_NORMAL_PARAMS_DICT','DEAL_INPUT_PARAMS_DICT','DEAL_MISSINGPRICE_PARTS','FINISH_P_PARAMS'],
                 '非还原类匹配项参数提取与标化':['FINISH_P_BRAND_NAME','FINISH_P_LASTCATEGORY_NAME','FINISH_P_MODEL','DEAL_UNSTD_PARAMS_DICT','DEAL_STD_PARAMS_DICT','DEAL_NORMAL_PARAMS_DICT','DEAL_INPUT_PARAMS_DICT','FINISH_P_PARAMS'],
                 '建库必填项参数提取':['FINISH_P_BRAND_NAME','FINISH_P_LASTCATEGORY_NAME','FINISH_P_MODEL','DEAL_UNSTD_PARAMS_DICT','DEAL_STD_PARAMS_DICT','DEAL_NORMAL_PARAMS_DICT','DEAL_INPUT_PARAMS_DICT','DEAL_MISSINGPRICE_PARTS','FINISH_P_PARAMS']
            }
        self.es = self.GetConnect()
        
    
    def GetConnect(self):
        
        es_client = False
        host = self.host
        try:
            es_client = Elasticsearch(
                [host],
                timeout=10
                )
            print(f"{host} connect success")
        except Exception as err:
            print("连接数据库失败, %s" % err)
            
        return es_client
    
    def data2df(self,search_data):
        
        hits_source = search_data['hits']['hits']
        # es查询出的结果总量
        total = search_data['hits']['total']
        # 游标用于输出es查询出的所有结果
        scroll_id = search_data['_scroll_id']
        for i in range(0, int(total['value'] / self.es_scroll_size) + 1):
            # scroll参数必须指定否则会报错
            query_scroll = self.es.scroll(scroll_id=scroll_id, scroll='5m')['hits']['hits']
            hits_source += query_scroll
        if hits_source and len(hits_source) > 0:
            source_arr = []
            for source in hits_source:
                source_arr.append(source['_source'])
            data = source_arr
            
        data_df = pd.DataFrame(data)
        
        return data_df
        
    
    def SearchAll(self):
        

        body = {"query": {
                      	"match_all":{}
                }
            }
        
        search_data = self.es.search(index=self.index_name,size=self.es_scroll_size, scroll='5m',body=body)
        data_df = self.data2df(search_data)
        
        return data_df
    
    def SearchByCondition(self,field,field_value_list):

        body = {
            'query': {
                'terms': {
                    field: field_value_list
                    }
                }
            }

            
        search_data = self.es.search(index=self.index_name,size=self.es_scroll_size, scroll='5m',body=body)

        data_df = self.data2df(search_data)
        
        return data_df
    
    def SearchAllByProjectName(self,data_projct_name):
        

        body={
                "query":{
                    "bool":{
                        "must":[
                            {
                            "match":{
                                    "DATA_PROJECT_NAME.keyword":data_projct_name
                                        }
                                }
                            ]
                        }
                    
                    }
                }
        
        search_data = self.es.search(index=self.index_name,size=self.es_scroll_size, scroll='5m',body=body)
        data_df = self.data2df(search_data)
        
        return data_df
    
    
    
    def SearchAimAllByCondition(self,field,field_value):
        

        body={
                "query":{
                    "bool":{
                        "must":[
                            {
                            "match":{
                                    field+".keyword":field_value
                                        }
                                }
                            ]
                        }
                    
                    }
                }
        
        search_data = self.es.search(index=self.index_name,size=self.es_scroll_size, scroll='5m',body=body)
        data_df = self.data2df(search_data)
        
        return data_df
    
    def SearchAllByTempCondition(self,data_projct_name):
        

        body={"query": {
                  "bool": {
                  	"must":[{
                  		"match":{
                  			"DATA_PROJECT_NAME.keyword":data_projct_name
                  		}
                  		}
                  	],
                  	
                    "must_not": [
                      {
                       "term": {
                          "FINISH_P_LASTCATEGORY_NAME.keyword": "台式机"
                        }
                      },
                      {
                       "term": {
                          "FINISH_P_LASTCATEGORY_NAME.keyword": "笔记本"
                        }
                      },
                      {
                       "term": {
                          "FINISH_P_LASTCATEGORY_NAME.keyword": "一体电脑"
                        }
                      },
                      {
                       "term": {
                          "FINISH_P_LASTCATEGORY_NAME.keyword": "复印纸"
                        }
                      },
                      {
                       "term": {
                          "FINISH_P_LASTCATEGORY_NAME.keyword": "工作站"
                        }
                      },
                      {
                       "term": {
                          "FLOW_NODE_STATUS.keyword": "SYS_FINISH_MATCH"
                        }
                      },
                      {
                       "term": {
                          "FLOW_NODE_STATUS.keyword": "SYS_GET_PARAMS_SUCCESS_END"
                        }
                      }
                      
                    ]
                  }
                }
            }
        
        search_data = self.es.search(index=self.index_name,size=self.es_scroll_size, scroll='5m',body=body)
        data_df = self.data2df(search_data)
        
        return data_df
    
    
    def SearchByNode(self,data_projct_name,data_batch,node_name):
        
        node_status = self.node_dict[node_name]
        
        body={
                "query":{
                    "bool":{
                        "must":[
                            {
                            "match":{
                                    "DATA_PROJECT_NAME.keyword":data_projct_name
                                        }
                                },
                            {
                            "match":{
                                    "DATA_BATCH.keyword":data_batch
                                        }
                                },
                            {
                            "match":{
                                    "FLOW_NODE_STATUS.keyword":node_status
                                        }
                                }
                            ]
                        }
                    
                    }
                }
        
        search_data = self.es.search(index=self.index_name,size=self.es_scroll_size, scroll='5m',body=body)
        data_df = self.data2df(search_data)
        
        field_list = self.basic_field + self.node_field[node_name]
        data_df = data_df[field_list]
        return data_df

    def DeleteByCondition(self,field,field_value):
        
        if field == 'DOC_ID':
            body = {
                'query': {
                    'match': {
                        field: field_value
                        }
                    }
                }
        else:
            body = {
                'query': {
                    'match': {
                        field + ".keyword": field_value
                        }
                    }
                }
        print(self.es.delete_by_query(index=self.index_name, body=body))
        
        
    def UpdateReturnData(self,type,doc_id,remark_detail,now_time):
        
        if type == 'brand':
            
            remark = "审核不通过，品牌异常。"+ remark_detail
            
        elif type == 'category':
            
            remark = "审核不通过，类别异常。"+ remark_detail
            
        elif type == 'model' or type == 'model_nopoint' or type == 'model_nonreductice':
            
            remark = "审核不通过，型号异常。"+ remark_detail
        
        elif type == 'params_reductive':
            
            remark = "审核不通过，该产品关键参数缺失。"+ remark_detail
            
        elif type == 'params_nonreductive':
            
            remark = "审核不通过，该产品关键参数缺失。"+ remark_detail
        
        else:
            
            print("type 参数异常，无法更新驳回数据")
            return False
        
        
        body = {
            "query":{
        		"match":{
        			"DOC_ID":doc_id
        		}
        	},
            "script": {
                "params":{
                    "remark":remark,
                    "time":now_time
                    },
                "inline":"ctx._source['FLOW_FINISH_STATUS']='Y';ctx._source['FINISH_P_REMARK']=params.remark;ctx._source['DATA_END_TIME']=params.time"
              }
            }

        print(self.es.update_by_query(index=self.index_name,body=body))
        
    def UpdateDealData(self,type,doc_id,deal_data):
        
        if type == 'brand':
            
            DEAL_COL_NAME = "DEAL_INPUT_BRAND"
            FLOW_NODE_STATUS = "FIRST_CHECK_SUCCESS"
            FINISH_COL = "FINISH_P_BRAND_NAME"
        
            
        elif type == 'category':
            
            DEAL_COL_NAME = "DEAL_INPUT_LASTCATEGORY_NAME"
            FINISH_COL = "FINISH_P_LASTCATEGORY_NAME"
            FLOW_NODE_STATUS = "SYS_GET_BRAND_SUCCESS"
            
        elif type == 'model_nopoint':
            DEAL_COL_NAME = "DEAL_INPUT_MODEL"
            FINISH_COL = "FINISH_P_MODEL"
            FLOW_NODE_STATUS = "SYS_GET_CATEGORY_SUCCESS_NONPOINT"
            body = {
                "query":{
            		"match":{
            			"DOC_ID":doc_id
            		}
            	},
                "script": {
                    "params":{
                        "deal_col":DEAL_COL_NAME,
                        "finish_col":FINISH_COL,
                        "params_info":str(deal_data),
                        "flow_node_status":FLOW_NODE_STATUS
                        },
                    "inline":"ctx._source[params.deal_col]=params.params_info;ctx._source[params.finish_col]=params.params_info;ctx._source['FLOW_NODE_STATUS']=params.flow_node_status;ctx._source['FLOW_CHECK_STATUS']='null'"
                  }
                }
    
    
            print(self.es.update_by_query(index=self.index_name,body=body))  
            return True
        
        elif type == 'model_nonreductice':
            DEAL_COL_NAME = "DEAL_INPUT_MODEL"
            FINISH_COL = "FINISH_P_MODEL"
            FLOW_NODE_STATUS = "SYS_GET_CATEGORY_SUCCESS_NONREDUCTIVE"
            body = {
                "query":{
            		"match":{
            			"DOC_ID":doc_id
            		}
            	},
                "script": {
                    "params":{
                        "deal_col":DEAL_COL_NAME,
                        "finish_col":FINISH_COL,
                        "params_info":str(deal_data),
                        "flow_node_status":FLOW_NODE_STATUS
                        },
                    "inline":"ctx._source[params.deal_col]=params.params_info;ctx._source[params.finish_col]=params.params_info;ctx._source['FLOW_NODE_STATUS']=params.flow_node_status;ctx._source['FLOW_CHECK_STATUS']='null'"
                  }
                }
    
    
            print(self.es.update_by_query(index=self.index_name,body=body))  
            return True
        
        elif type == 'params_reductive':
            
            body = {
                "query":{
            		"match":{
            			"DOC_ID":doc_id
            		}
            	},
                "script": {
                    "params":{
                        "params_info":str(deal_data)
                        },
                    "inline":"ctx._source['DEAL_STD_PARAMS_DICT']=params.params_info;ctx._source['DEAL_INPUT_PARAMS_DICT']=params.params_info;ctx._source['FLOW_NODE_STATUS']='SYS_GET_CATEGORY_SUCCESS_REDUCTIVE';ctx._source['FLOW_CHECK_STATUS']='null'"
                  }
                }


            print(self.es.update_by_query(index=self.index_name,body=body))
            return True
            
        elif type == 'params_nonreductive':
            
            body = {
                "query":{
            		"match":{
            			"DOC_ID":doc_id
            		}
            	},
                "script": {
                    "params":{
                        "params_info":str(deal_data)
                        },
                    "inline":"ctx._source['DEAL_STD_PARAMS_DICT']=params.params_info;ctx._source['DEAL_INPUT_PARAMS_DICT']=params.params_info;ctx._source['FLOW_NODE_STATUS']='SYS_GET_CATEGORY_SUCCESS_NONREDUCTIVE';ctx._source['FLOW_CHECK_STATUS']='null'"
                  }
                }


            print(self.es.update_by_query(index=self.index_name,body=body)) 
            return True
        
        else:
            
            print("type 参数异常，无法更新驳回数据")
            return False
        
        
        body = {
            "query":{
        		"match":{
        			"DOC_ID":doc_id
        		}
        	},
            "script": {
                "params":{
                    "deal_col":DEAL_COL_NAME,
                    "finish_col":FINISH_COL,
                    "params_info":str(deal_data),
                    "flow_node_status":FLOW_NODE_STATUS
                    },
                "inline":"ctx._source[params.deal_col]=params.params_info;ctx._source[params.finish_col]=params.params_info;ctx._source['FLOW_NODE_STATUS']=params.flow_node_status;ctx._source['FLOW_CHECK_STATUS']='null'"
              }
            }


        print(self.es.update_by_query(index=self.index_name,body=body))  
        '''
        body = {
            "query":{
        		"match":{
        			"DOC_ID":doc_id
        		}
        	},
            "script": {
                "params":{
                    "deal_col":DEAL_COL_NAME,
                    "params_info":str(deal_data),
                    "flow_node_status":FLOW_NODE_STATUS
                    },
                "inline":"ctx._source[params.deal_col]=params.params_info"
              }
            }

        
        print(self.es.update_by_query(index=self.index_name,body=body))  
        '''
    
    def UpdateNodeReRun(self,type,doc_id_list):
        
        if type == 'brand':
            
            DEAL_COL_NAME = "DEAL_INPUT_BRAND"
            FLOW_NODE_STATUS = "FIRST_CHECK_SUCCESS"

            body = {
                "query":{
            		"terms":{
            			"DOC_ID":doc_id_list
            		}
            	},
                "script": {
                    "params":{
                        "deal_col":DEAL_COL_NAME,
                        "flow_node_status":FLOW_NODE_STATUS
                        },
                    "inline":"ctx._source[params.deal_col]='null';ctx._source['FLOW_NODE_STATUS']=params.flow_node_status;ctx._source['FLOW_CHECK_STATUS']='null';ctx._source['FLOW_FINISH_STATUS']='N'"
                  }
                }
    
    
            print(self.es.update_by_query(index=self.index_name,body=body))
            
        if type == 'category':
            
            DEAL_COL_NAME = "DEAL_INPUT_LASTCATEGORY_NAME"
            FLOW_NODE_STATUS = "SYS_GET_BRAND_SUCCESS"
            
            
            
            body = {
                "query":{
            		"terms":{
            			"DOC_ID":doc_id_list
            		}
            	},
                "script": {
                    "params":{
                        "deal_col":DEAL_COL_NAME,
                        "flow_node_status":FLOW_NODE_STATUS
                        },
                    "inline":"ctx._source[params.deal_col]='null';ctx._source['FLOW_NODE_STATUS']=params.flow_node_status;ctx._source['FLOW_CHECK_STATUS']='null';ctx._source['FLOW_FINISH_STATUS']='N'"
                  }
                }
    
    
            print(self.es.update_by_query(index=self.index_name,body=body))
        
        if type == 'model_nopoint':
            
            DEAL_COL_NAME = "DEAL_INPUT_MODEL"
            
            FLOW_NODE_STATUS = "SYS_GET_CATEGORY_SUCCESS_NONPOINT"
            
            body = {
                "query":{
            		"terms":{
            			"DOC_ID":doc_id_list
            		}
            	},
                "script": {
                    "params":{
                        "deal_col":DEAL_COL_NAME,
                        "flow_node_status":FLOW_NODE_STATUS
                        },
                    "inline":"ctx._source[params.deal_col]='null';ctx._source['FLOW_NODE_STATUS']=params.flow_node_status;ctx._source['FLOW_CHECK_STATUS']='null';ctx._source['FLOW_FINISH_STATUS']='N'"
                  }
                }
    
    
            print(self.es.update_by_query(index=self.index_name,body=body))
            
        if type == 'model_nonreductice':
            
            DEAL_COL_NAME = "DEAL_INPUT_MODEL"
            
            FLOW_NODE_STATUS = "SYS_GET_CATEGORY_SUCCESS_NONREDUCTIVE"
                
  
            body = {
                "query":{
            		"terms":{
            			"DOC_ID":doc_id_list
            		}
            	},
                "script": {
                    "params":{
                        "deal_col":DEAL_COL_NAME,
                        "flow_node_status":FLOW_NODE_STATUS
                        },
                    "inline":"ctx._source[params.deal_col]='null';ctx._source['FLOW_NODE_STATUS']=params.flow_node_status;ctx._source['FLOW_CHECK_STATUS']='null';ctx._source['FLOW_FINISH_STATUS']='N'"
                  }
                }
    
    
            print(self.es.update_by_query(index=self.index_name,body=body))
            
        if type == 'params_reductive':
        
            DEAL_COL_NAME = "DEAL_INPUT_PARAMS_DICT"
                
            FLOW_NODE_STATUS = "SYS_GET_CATEGORY_SUCCESS_REDUCTIVE"
            
            body = {
                "query":{
            		"terms":{
            			"DOC_ID":doc_id_list
            		}
            	},
                "script": {
                    "params":{
                        "deal_col":DEAL_COL_NAME,
                        "flow_node_status":FLOW_NODE_STATUS
                        },
                    "inline":"ctx._source[params.deal_col]='null';ctx._source['FLOW_NODE_STATUS']=params.flow_node_status;ctx._source['FLOW_CHECK_STATUS']='null';ctx._source['FLOW_FINISH_STATUS']='N'"
                  }
                }
    
    
            print(self.es.update_by_query(index=self.index_name,body=body))
            
        if type == 'params_nonreductive':
        
            DEAL_COL_NAME = "DEAL_INPUT_PARAMS_DICT"
            
                
            FLOW_NODE_STATUS = "SYS_GET_MODEL_SUCCESS_NONREDUCTIVE"
            
            body = {
                "query":{
            		"terms":{
            			"DOC_ID":doc_id_list
            		}
            	},
                "script": {
                    "params":{
                        "deal_col":DEAL_COL_NAME,
                        "flow_node_status":FLOW_NODE_STATUS
                        },
                    "inline":"ctx._source[params.deal_col]='null';ctx._source['FLOW_NODE_STATUS']=params.flow_node_status;ctx._source['FLOW_CHECK_STATUS']='null';ctx._source['FLOW_FINISH_STATUS']='N'"
                  }
                }
    
    
            print(self.es.update_by_query(index=self.index_name,body=body))
            
        if type == 'match_nonpoint':
        
                
            FLOW_NODE_STATUS = "SYS_GET_MODEL_SUCCESS_NONPOINT"
            
            body = {
                "query":{
            		"terms":{
            			"DOC_ID":doc_id_list
            		}
            	},
                "script": {
                    "params":{
                        "flow_node_status":FLOW_NODE_STATUS
                        },
                    "inline":"ctx._source['FLOW_NODE_STATUS']=params.flow_node_status;ctx._source['FLOW_CHECK_STATUS']='null';ctx._source['FLOW_FINISH_STATUS']='N'"
                  }
                }
    
    
            print(self.es.update_by_query(index=self.index_name,body=body))
        
        if type == 'match_nonreductive':
        
    
            FLOW_NODE_STATUS = "SYS_GET_PARAMS_SUCCESS_NONREDUCTIVE"
            
            body = {
                "query":{
            		"terms":{
            			"DOC_ID":doc_id_list
            		}
            	},
                "script": {
                    "params":{
                        "flow_node_status":FLOW_NODE_STATUS
                        },
                    "inline":"ctx._source['FLOW_NODE_STATUS']=params.flow_node_status;ctx._source['FLOW_CHECK_STATUS']='null';ctx._source['FLOW_FINISH_STATUS']='N'"
                  }
                }
    
    
            print(self.es.update_by_query(index=self.index_name,body=body))
            
        if type == 'match_reductive':
            
            FLOW_NODE_STATUS = "SYS_GET_PARAMS_SUCCESS_REDUCTIVE"
            
            body = {
                "query":{
            		"terms":{
            			"DOC_ID":doc_id_list
            		}
            	},
                "script": {
                    "params":{
                        "flow_node_status":FLOW_NODE_STATUS
                        },
                    "inline":"ctx._source['FLOW_NODE_STATUS']=params.flow_node_status;ctx._source['FLOW_CHECK_STATUS']='null';ctx._source['FLOW_FINISH_STATUS']='N'"
                  }
                }
    
    
            print(self.es.update_by_query(index=self.index_name,body=body))
            
        if type == 'needed_params':
        
            DEAL_COL_NAME = "DEAL_INPUT_PARAMS_DICT"
            FLOW_NODE_STATUS = "SYS_FINISH_MATCH"
            
            body = {
                "query":{
            		"terms":{
            			"DOC_ID":doc_id_list
            		}
            	},
                "script": {
                    "params":{
                        "deal_col":DEAL_COL_NAME,
                        "flow_node_status":FLOW_NODE_STATUS
                        },
                    "inline":"ctx._source[params.deal_col]='null';ctx._source['FLOW_NODE_STATUS']=params.flow_node_status;ctx._source['FLOW_CHECK_STATUS']='null';ctx._source['FLOW_FINISH_STATUS']='N'"
                  }
                }
    
    
            print(self.es.update_by_query(index=self.index_name,body=body)) 
        
    
    
    def UpdateParamsReturnData(self,search_field,search_field_value,remark_detail,now_time):
        
        
        if search_field == 'DOC_ID':
            
            body = {
                "query":{
            		"match":{
            			search_field:search_field_value
            		}
            	},
                "script": {
                    "params":{
                        "remark":"审核不通过，该产品关键参数缺失。"+ remark_detail,
                        "time":now_time
                        },
                    "inline":"ctx._source['FLOW_FINISH_STATUS']='Y';ctx._source['FINISH_P_REMARK']=params.remark;ctx._source['DATA_END_TIME']=params.time"
                  }
                }

            print(self.es.update_by_query(index=self.index_name,body=body))
        
    def UpdateParamsDealData(self,category_name,search_field,search_field_value,params_info):
        
        
        if search_field == 'DOC_ID':
            
            if category_name in ['笔记本','台式机','一体电脑','复印纸','工作站']:
            
                body = {
                    "query":{
                		"match":{
                			search_field:search_field_value
                		}
                	},
                    "script": {
                        "params":{
                            "params_info":str(params_info)
                            },
                        "inline":"ctx._source['DEAL_STD_PARAMS_DICT']=params.params_info;ctx._source['DEAL_INPUT_PARAMS_DICT']=params.params_info;ctx._source['FLOW_NODE_STATUS']='SYS_GET_CATEGORY_SUCCESS_REDUCTIVE';ctx._source['FLOW_CHECK_STATUS']='null'"
                      }
                    }
    
    
                print(self.es.update_by_query(index=self.index_name,body=body))
            else:
                body = {
                    "query":{
                		"match":{
                			search_field:search_field_value
                		}
                	},
                    "script": {
                        "params":{
                            "params_info":str(params_info)
                            },
                        "inline":"ctx._source['DEAL_STD_PARAMS_DICT']=params.params_info;ctx._source['DEAL_INPUT_PARAMS_DICT']=params.params_info;ctx._source['FLOW_NODE_STATUS']='SYS_GET_MODEL_SUCCESS_NONREDUCTIVE';ctx._source['FLOW_CHECK_STATUS']='null'"
                      }
                    }
    
    
                print(self.es.update_by_query(index=self.index_name,body=body))
        
        
    def UpdateByCondition(self,search_field,search_field_value,update_field,update_field_value):
        

        inline = "ctx._source['" + str(update_field) + "']='" + str(update_field_value) + "'"
        
        if search_field == 'DOC_ID':
            
            body = {
                "query":{
            		"match":{
            			search_field:search_field_value
            		}
            	},
                "script": {
                    "params":{
                        "update_field":str(update_field),
                        "update_field_value":str(update_field_value)
                        },
                    "inline":"ctx._source[params.update_field]=params.update_field_value"
                  }
                }

        else:
            body = {
                "query":{
            		"match":{
            			search_field + ".keyword":search_field_value
            		}
            	},
                "script": {
                    "inline": inline
                  }
                }
        
        print(self.es.update_by_query(index=self.index_name,body=body))

    def UpdateByMultipleCondition(self,search_field_list,search_field_value_list,update_field,update_field_value):
    
        inline = "ctx._source['" + str(update_field) + "']='" + str(update_field_value) + "'"
        search_field_1 = search_field_list[0]
        search_field_value_1 = search_field_value_list[0]
        search_field_2 = search_field_list[1]
        search_field_value_2 = search_field_value_list[1]
        body = {
            "query":{
                "bool":{
                    "must":[{
                            "match":{
                    			search_field_1 + ".keyword":search_field_value_1
                    		}
                        },
                        {
                            "match":{
                    			search_field_2 + ".keyword":search_field_value_2
                    		}
                        }
                        
                        ]
                    
                    }
        		
        	},
            "script": {
                "inline": inline
              }
            }
        
    
        print(self.es.update_by_query(index=self.index_name,body=body))
        
        
        
    def UpdateByMultipleThreeCondition(self,search_field_list,search_field_value_list,update_field,update_field_value):

        inline = "ctx._source['" + str(update_field) + "']='" + str(update_field_value) + "'"
        search_field_1 = search_field_list[0]
        search_field_value_1 = search_field_value_list[0]
        search_field_2 = search_field_list[1]
        search_field_value_2 = search_field_value_list[1]
        search_field_3 = search_field_list[2]
        search_field_value_3 = search_field_value_list[2]
        body = {
            "query":{
                "bool":{
                    "must":[{
                            "match":{
                    			search_field_1 + ".keyword":search_field_value_1
                    		}
                        },
                        {
                            "match":{
                    			search_field_2 + ".keyword":search_field_value_2
                    		}
                        },
                        {
                            "match":{
                    			search_field_3 + ".keyword":search_field_value_3
                    		}
                        }
                        
                        ]
                    
                    }
        		
        	},
            "script": {
                "inline": inline
              }
            }
        
    
        print(self.es.update_by_query(index=self.index_name,body=body))
        
    def UpdateByMultipleFourCondition(self,search_field_list,search_field_value_list,update_field,update_field_value):

        inline = "ctx._source['" + str(update_field) + "']='" + str(update_field_value) + "'"
        search_field_1 = search_field_list[0]
        search_field_value_1 = search_field_value_list[0]
        search_field_2 = search_field_list[1]
        search_field_value_2 = search_field_value_list[1]
        search_field_3 = search_field_list[2]
        search_field_value_3 = search_field_value_list[2]
        search_field_4 = search_field_list[3]
        search_field_value_4 = search_field_value_list[3]
        body = {
            "query":{
                "bool":{
                    "must":[{
                            "match":{
                    			search_field_1 + ".keyword":search_field_value_1
                    		}
                        },
                        {
                            "match":{
                    			search_field_2 + ".keyword":search_field_value_2
                    		}
                        },
                        {
                            "match":{
                    			search_field_3 + ".keyword":search_field_value_3
                    		}
                        },{
                            "match":{
                    			search_field_4 + ".keyword":search_field_value_4
                    		}
                        }
                        
                        ]
                    
                    }
        		
        	},
            "script": {
                "inline": inline
              }
            }
        
        res = self.es.update_by_query(index=self.index_name,body=body)
        print(res)
        return res
    def UpdateByMultipleFiveCondition(self,search_field_list,search_field_value_list,update_field,update_field_value):

        inline = "ctx._source['" + str(update_field) + "']='" + str(update_field_value) + "'"
        search_field_1 = search_field_list[0]
        search_field_value_1 = search_field_value_list[0]
        search_field_2 = search_field_list[1]
        search_field_value_2 = search_field_value_list[1]
        search_field_3 = search_field_list[2]
        search_field_value_3 = search_field_value_list[2]
        search_field_4 = search_field_list[3]
        search_field_value_4 = search_field_value_list[3]
        search_field_5 = search_field_list[4]
        search_field_value_5 = search_field_value_list[4]
        body = {
            "query":{
                "bool":{
                    "must":[{
                            "match":{
                    			search_field_1 + ".keyword":search_field_value_1
                    		}
                        },
                        {
                            "match":{
                    			search_field_2 + ".keyword":search_field_value_2
                    		}
                        },
                        {
                            "match":{
                    			search_field_3 + ".keyword":search_field_value_3
                    		}
                        },{
                            "match":{
                    			search_field_4 + ".keyword":search_field_value_4
                    		}
                        },{
                            "match":{
                    			search_field_5 + ".keyword":search_field_value_5
                    		}
                        }
                        
                        ]
                    
                    }
        		
        	},
            "script": {
                "inline": inline
              }
            }
        
    
        print(self.es.update_by_query(index=self.index_name,body=body))
        
    def SyncDataByDOCID(self,col_mode,source_index,sync_col_list,doc_id_list,target_index):
        if col_mode == 'all':
            body = {
                      "source": {
                        "index": source_index,
                        "type": "doc",
                        "query": { 
                          "terms": {
                            "DOC_ID": doc_id_list
                          }
                        }
                      },
                      "dest": {
                        "type":"doc",
                        "index": target_index
                      }
                    } 
    
            print(self.es.reindex(body=body))
        else:
            body = {
                      "source": {
                        "index": source_index,
                        "type": "doc",
                        "_source": sync_col_list,
                        "query": { 
                          "terms": {
                            "DOC_ID": doc_id_list
                          }
                        }
                      },
                      "dest": {
                        "type":"doc",
                        "index": target_index
                      }
                    } 
    
            print(self.es.reindex(body=body))
            
    def SyncDataByCondition(self):

        body = {
                  "source": {
                    "index": "model_params_test",
                    "type": "doc",
                    "query": { 
                      "terms": {
                        "DATA_BATCH": ['202111161740200001','202111160942440001']
                      }
                    }
                  },
                  "dest": {
                    "type":"doc",
                    "index": "model_params_test_copy_1118"
                  }
                } 

        print(self.es.reindex(body=body))
       


