
# 使用DSL加工函数进行事件判断
通过事件判断可以更好地对符合特定条件的数据进行相应操作，让加工逻辑更可靠。本文主要介绍使用函数进行事件判断的常见场景和最佳方案示例。
#### 场景一：判断字段是否存在
- 原始日志
  ```
  {
    "a": "a_value"
  }
  ```
  
- 加工规则
  ```
  e_if(e_has("a"),e_set("has_a", true))
  e_if(e_has("b"),e_set("has_b", true))
  e_if(e_not_has("a"),e_set("not_has_a", true))
  e_if(e_not_has("b"),e_set("not_has_b", true))
  ```
  
- 加工结果
  ```
  {
  "a": "a_value",
  "not_has_b": true,
  "has_a": true
  }
  ```
  
 
#### 场景二：判断字段值是否存在且不为空
- 原始日志
  ```
  {
    "a": "a_value",
    "b":""
  }
  ```
  
- 加工规则
  ```
  e_if_else(v("a"), e_set("not_empty_a", true),e_set("not_empty_a", false))
  e_if_else(v("b"), e_set("not_empty_b", true),e_set("not_empty_b", false))
  ```
  
- 加工结果
  ```
  {
  "a": "a_value",
  "not_empty_b": false,
  "b": "",
  "not_empty_a": true
  }
  ```
  
 
#### 场景三：判断字段值是否存在且为空
- 原始日志
  ```
  {
    "a": "a_value",
    "b":""
  }
  ```
  
- 加工规则
  ```
  e_if_else(op_and(e_has("a"), op_not(v("a"))), e_set("empty_a", true),e_set("empty_a", false))
  e_if_else(op_and(e_has("b"), op_not(v("b"))), e_set("empty_b", true),e_set("empty_b", false))
  ```
  
- 加工结果
  ```
  {
  "a": "a_value",
  "b": "",
  "empty_b": true,
  "empty_a": false
  }
  ```
  
 
#### 场景四：基于字段值的逻辑查询判断
- 原始日志
  ```
  [
  {
  "http_host": "example.com",
  "status": 200,
  "request_method": "GET",
  "scheme": "https",
  "header_length": 700,
  "body_length": 1200
  },
  {
  "http_host": "example.org",
  "status": 200,
  "request_method": "POST",
  "scheme": "https",
  "header_length": 100,
  "body_length": 800
  },
  {
  "http_host": "example.net",
  "status": 200,
  "request_method": "GET",
  "scheme": "http",
  "header_length": 200,
  "body_length": 800
  },
  {
  "http_host": "example.cn",
  "status": 404,
  "request_method": "GET",
  "scheme": "https",
  "header_length": 100,
  "body_length": 300
  }
  ]
  ```
  
- 加工需求1：为所有status字段值为200的日志事件，添加一个新字段type，其值为normal。
  加工规则：
  ```
  e_if(e_match("status", "200"), e_set("type", "normal"))
  ```
  加工结果：
  ```
  {
  "scheme": "https",
  "header_length": 700,
  "request_method": "GET",
  "type": "normal",
  "http_host": "example.com",
  "status": 200,
  "body_length": 1200
  }
  {
  "scheme": "https",
  "header_length": 100,
  "request_method": "POST",
  "type": "normal",
  "http_host": "example.org",
  "status": 200,
  "body_length": 800
  }
  {
  "scheme": "http",
  "header_length": 200,
  "request_method": "GET",
  "type": "normal",
  "http_host": "example.net",
  "status": 200,
  "body_length": 800
  }
  {
  "scheme": "https",
  "header_length": 100,
  "request_method": "GET",
  "http_host": "example.cn",
  "status": 404,
  "body_length": 300
  }
  ```
  
- 加工需求2：为所有status字段值为200，且request_method字段值为GET，且scheme字段值为https的日志事件添加一个新字段type，其值为normal。
  加工规则：
  ```
  e_if(e_match_all("status", "200", "request_method","GET", "scheme", "https"), e_set("type", "normal"))
  ```
  加工结果：
  ```
  {
  "scheme": "https",
  "header_length": 700,
  "request_method": "GET",
  "type": "normal",
  "http_host": "example.com",
  "status": 200,
  "body_length": 1200
  }
  {
  "scheme": "https",
  "header_length": 100,
  "request_method": "POST",
  "http_host": "example.org",
  "status": 200,
  "body_length": 800
  }
  {
  "scheme": "http",
  "header_length": 200,
  "request_method": "GET",
  "http_host": "example.net",
  "status": 200,
  "body_length": 800
  }
  {
  "scheme": "https",
  "header_length": 100,
  "request_method": "GET",
  "http_host": "example.cn",
  "status": 404,
  "body_length": 300
  }
  ```
  
- 加工需求3：为所有status字段值为200，或request_method字段值为GET，或scheme字段值为https的日志事件添加一个字段type，其值为normal。
  加工规则：
  ```
  e_if(e_match_any("status", "200", "request_method","GET", "scheme", "https"), e_set("type", "normal"))
  ```
  加工结果：
  ```
  {
  "scheme": "https",
  "header_length": 700,
  "request_method": "GET",
  "type": "normal",
  "http_host": "example.com",
  "status": 200,
  "body_length": 1200
  }
  {
  "scheme": "https",
  "header_length": 100,
  "request_method": "POST",
  "type": "normal",
  "http_host": "example.org",
  "status": 200,
  "body_length": 800
  }
  {
  "scheme": "http",
  "header_length": 200,
  "request_method": "GET",
  "type": "normal",
  "http_host": "example.net",
  "status": 200,
  "body_length": 800
  }
  {
  "scheme": "https",
  "header_length": 100,
  "request_method": "GET",
  "type": "normal",
  "http_host": "example.cn",
  "status": 404,
  "body_length": 300
  }
  ```
  
 
