|
| 1 | +[[dissect]] |
| 2 | +=== Dissecting data |
| 3 | +Dissect matches a single text field against a defined pattern. A dissect |
| 4 | +pattern is defined by the parts of the string you want to discard. Paying |
| 5 | +special attention to each part of a string helps to build successful dissect |
| 6 | +patterns. |
| 7 | + |
| 8 | +If you don't need the power of regular expressions, use dissect patterns instead |
| 9 | +of grok. Dissect uses a much simpler syntax than grok and is typically faster |
| 10 | +overall. The syntax for dissect is transparent: tell dissect what you want and |
| 11 | +it will return those results to you. |
| 12 | + |
| 13 | +[[dissect-syntax]] |
| 14 | +==== Dissect patterns |
| 15 | +Dissect patterns are comprised of _variables_ and _separators_. Anything |
| 16 | +defined by a percent sign and curly braces `%{}` is considered a variable, |
| 17 | +such as `%{clientip}`. You can assign variables to any part of data in a field, |
| 18 | +and then return only the parts that you want. Separators are any values between |
| 19 | +variables, which could be spaces, dashes, or other delimiters. |
| 20 | + |
| 21 | +For example, let's say you have log data with a `message` field that looks like |
| 22 | +this: |
| 23 | + |
| 24 | +[source,js] |
| 25 | +---- |
| 26 | +"message" : "247.37.0.0 - - [30/Apr/2020:14:31:22 -0500] \"GET /images/hm_nbg.jpg HTTP/1.0\" 304 0" |
| 27 | +---- |
| 28 | +// NOTCONSOLE |
| 29 | + |
| 30 | +You assign variables to each part of the data to construct a successful |
| 31 | +dissect pattern. Remember, tell dissect _exactly_ what you want you want to |
| 32 | +match on. |
| 33 | + |
| 34 | +The first part of the data looks like an IP address, so you |
| 35 | +can assign a variable like `%{clientip}`. The next two characters are dashes |
| 36 | +with a space on either side. You can assign a variable for each dash, or a |
| 37 | +single variable to represent the dashes and spaces. Next are a set of brackets |
| 38 | +containing a timestamp. The brackets are a separator, so you include those in |
| 39 | +the dissect pattern. Thus far, the data and matching dissect pattern look like |
| 40 | +this: |
| 41 | + |
| 42 | +[source,js] |
| 43 | +---- |
| 44 | +247.37.0.0 - - [30/Apr/2020:14:31:22 -0500] <1> |
| 45 | +
|
| 46 | +%{clientip} %{ident} %{auth} [%{@timestamp}] <2> |
| 47 | +---- |
| 48 | +// NOTCONSOLE |
| 49 | +<1> The first chunks of data from the `message` field |
| 50 | +<2> Dissect pattern to match on the selected data chunks |
| 51 | + |
| 52 | +Using that same logic, you can create variables for the remaining chunks of |
| 53 | +data. Double quotation marks are separators, so include those in your dissect |
| 54 | +pattern. The pattern replaces `GET` with a `%{verb}` variable, but keeps `HTTP` |
| 55 | +as part of the pattern. |
| 56 | + |
| 57 | +[source,js] |
| 58 | +---- |
| 59 | +\"GET /images/hm_nbg.jpg HTTP/1.0\" 304 0 |
| 60 | +
|
| 61 | +"%{verb} %{request} HTTP/%{httpversion}" %{response} %{size} |
| 62 | +---- |
| 63 | +// NOTCONSOLE |
| 64 | + |
| 65 | +Combining the two patterns results in a dissect pattern that looks like this: |
| 66 | + |
| 67 | +[source,js] |
| 68 | +---- |
| 69 | +%{clientip} %{ident} %{auth} [%{@timestamp}] \"%{verb} %{request} HTTP/%{httpversion}\" %{status} %{size} |
| 70 | +---- |
| 71 | +// NOTCONSOLE |
| 72 | + |
| 73 | +Now that you have a dissect pattern, how do you test and use it? |
| 74 | + |
| 75 | +[[dissect-patterns-test]] |
| 76 | +==== Test dissect patterns with Painless |
| 77 | +You can incorporate dissect patterns into Painless scripts to extract |
| 78 | +data. To test your script, use either the {painless}/painless-execute-api.html#painless-execute-runtime-field-context[field contexts] of the Painless |
| 79 | +execute API or create a runtime field that includes the script. Runtime fields |
| 80 | +offer greater flexibility and accept multiple documents, but the Painless execute |
| 81 | +API is a great option if you don't have write access on a cluster where you're |
| 82 | +testing a script. |
| 83 | + |
| 84 | +For example, test your dissect pattern with the Painless execute API by |
| 85 | +including your Painless script and a single document that matches your data. |
| 86 | +Start by indexing the `message` field as a `wildcard` data type: |
| 87 | + |
| 88 | +[source,console] |
| 89 | +---- |
| 90 | +PUT my-index |
| 91 | +{ |
| 92 | + "mappings": { |
| 93 | + "properties": { |
| 94 | + "message": { |
| 95 | + "type": "wildcard" |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | +---- |
| 101 | + |
| 102 | +If you want to retrieve the HTTP response code, add your dissect pattern to a |
| 103 | +Painless script that extracts the `response` value. To extract values from a |
| 104 | +field, use this function: |
| 105 | + |
| 106 | +[source,painless] |
| 107 | +---- |
| 108 | +`.extract(doc["<field_name>"].value)?.<field_value>` |
| 109 | +---- |
| 110 | + |
| 111 | +In this example, `message` is the `<field_name>` and `response` is the |
| 112 | +`<field_value>`: |
| 113 | + |
| 114 | +[source,console] |
| 115 | +---- |
| 116 | +POST /_scripts/painless/_execute |
| 117 | +{ |
| 118 | + "script": { |
| 119 | + "source": """ |
| 120 | + String response=dissect('%{clientip} %{ident} %{auth} [%{@timestamp}] "%{verb} %{request} HTTP/%{httpversion}" %{response} %{size}').extract(doc["message"].value)?.response; |
| 121 | + if (response != null) emit(Integer.parseInt(response)); <1> |
| 122 | + """ |
| 123 | + }, |
| 124 | + "context": "long_field", <2> |
| 125 | + "context_setup": { |
| 126 | + "index": "my-index", |
| 127 | + "document": { <3> |
| 128 | + "message": """247.37.0.0 - - [30/Apr/2020:14:31:22 -0500] "GET /images/hm_nbg.jpg HTTP/1.0" 304 0""" |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | +---- |
| 133 | +// TEST[continued] |
| 134 | +<1> Runtime fields require the `emit` method to return values. |
| 135 | +<2> Because the response code is an integer, use the `long_field` context. |
| 136 | +<3> Include a sample document that matches your data. |
| 137 | + |
| 138 | +The result includes the HTTP response code: |
| 139 | + |
| 140 | +[source,console-result] |
| 141 | +---- |
| 142 | +{ |
| 143 | + "result" : [ |
| 144 | + 304 |
| 145 | + ] |
| 146 | +} |
| 147 | +---- |
| 148 | + |
| 149 | +[[dissect-patterns-runtime]] |
| 150 | +==== Use dissect patterns and scripts in runtime fields |
| 151 | +If you have a functional dissect pattern, you can add it to a runtime field to |
| 152 | +manipulate data. Because runtime fields don't require you to index fields, you |
| 153 | +have incredible flexibility to modify your script and how it functions. If you |
| 154 | +already <<dissect-patterns-test,tested your dissect pattern>> using the Painless |
| 155 | +execute API, you can use that _exact_ Painless script in your runtime field. |
| 156 | + |
| 157 | +To start, add the `message` field as a `wildcard` type like in the previous |
| 158 | +section, but also add `@timestamp` as a `date` in case you want to operate on |
| 159 | +that field for <<common-script-uses,other use cases>>: |
| 160 | + |
| 161 | +[source,console] |
| 162 | +---- |
| 163 | +PUT /my-index/ |
| 164 | +{ |
| 165 | + "mappings": { |
| 166 | + "properties": { |
| 167 | + "@timestamp": { |
| 168 | + "format": "strict_date_optional_time||epoch_second", |
| 169 | + "type": "date" |
| 170 | + }, |
| 171 | + "message": { |
| 172 | + "type": "wildcard" |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | +} |
| 177 | +---- |
| 178 | + |
| 179 | +If you want to extract the HTTP response code using your dissect pattern, you |
| 180 | +can create a runtime field like `http.response`: |
| 181 | + |
| 182 | +[source,console] |
| 183 | +---- |
| 184 | +PUT my-index/_mappings |
| 185 | +{ |
| 186 | + "runtime": { |
| 187 | + "http.response": { |
| 188 | + "type": "long", |
| 189 | + "script": """ |
| 190 | + String response=dissect('%{clientip} %{ident} %{auth} [%{@timestamp}] "%{verb} %{request} HTTP/%{httpversion}" %{response} %{size}').extract(doc["message"].value)?.response; |
| 191 | + if (response != null) emit(Integer.parseInt(response)); |
| 192 | + """ |
| 193 | + } |
| 194 | + } |
| 195 | +} |
| 196 | +---- |
| 197 | +// TEST[continued] |
| 198 | + |
| 199 | +After mapping the fields you want to retrieve, index a few records from |
| 200 | +your log data into {es}. The following request uses the <<docs-bulk,bulk API>> |
| 201 | +to index raw log data into `my-index`: |
| 202 | + |
| 203 | +[source,console] |
| 204 | +---- |
| 205 | +POST /my-index/_bulk?refresh=true |
| 206 | +{"index":{}} |
| 207 | +{"timestamp":"2020-04-30T14:30:17-05:00","message":"40.135.0.0 - - [30/Apr/2020:14:30:17 -0500] \"GET /images/hm_bg.jpg HTTP/1.0\" 200 24736"} |
| 208 | +{"index":{}} |
| 209 | +{"timestamp":"2020-04-30T14:30:53-05:00","message":"232.0.0.0 - - [30/Apr/2020:14:30:53 -0500] \"GET /images/hm_bg.jpg HTTP/1.0\" 200 24736"} |
| 210 | +{"index":{}} |
| 211 | +{"timestamp":"2020-04-30T14:31:12-05:00","message":"26.1.0.0 - - [30/Apr/2020:14:31:12 -0500] \"GET /images/hm_bg.jpg HTTP/1.0\" 200 24736"} |
| 212 | +{"index":{}} |
| 213 | +{"timestamp":"2020-04-30T14:31:19-05:00","message":"247.37.0.0 - - [30/Apr/2020:14:31:19 -0500] \"GET /french/splash_inet.html HTTP/1.0\" 200 3781"} |
| 214 | +{"index":{}} |
| 215 | +{"timestamp":"2020-04-30T14:31:22-05:00","message":"247.37.0.0 - - [30/Apr/2020:14:31:22 -0500] \"GET /images/hm_nbg.jpg HTTP/1.0\" 304 0"} |
| 216 | +{"index":{}} |
| 217 | +{"timestamp":"2020-04-30T14:31:27-05:00","message":"252.0.0.0 - - [30/Apr/2020:14:31:27 -0500] \"GET /images/hm_bg.jpg HTTP/1.0\" 200 24736"} |
| 218 | +{"index":{}} |
| 219 | +{"timestamp":"2020-04-30T14:31:28-05:00","message":"not a valid apache log"} |
| 220 | +---- |
| 221 | +// TEST[continued] |
| 222 | + |
| 223 | +You can define a simple query to run a search for a specific HTTP response and |
| 224 | +return all related fields. Use the `fields` parameter of the search API to |
| 225 | +retrieve the `http.response` runtime field. |
| 226 | + |
| 227 | +[source,console] |
| 228 | +---- |
| 229 | +GET my-index/_search |
| 230 | +{ |
| 231 | + "query": { |
| 232 | + "match": { |
| 233 | + "http.response": "304" |
| 234 | + } |
| 235 | + }, |
| 236 | + "fields" : ["http.response"] |
| 237 | +} |
| 238 | +---- |
| 239 | +// TEST[continued] |
| 240 | + |
| 241 | +Alternatively, you can define the same runtime field but in the context of a |
| 242 | +search request. The runtime definition and the script are exactly the same as |
| 243 | +the one defined previously in the index mapping. Just copy that definition into |
| 244 | +the search request under the `runtime_mappings` section and include a query |
| 245 | +that matches on the runtime field. This query returns the same results as the |
| 246 | +search query previously defined for the `http.response` runtime field in your |
| 247 | +index mappings, but only in the context of this specific search: |
| 248 | + |
| 249 | +[source,console] |
| 250 | +---- |
| 251 | +GET my-index/_search |
| 252 | +{ |
| 253 | + "runtime_mappings": { |
| 254 | + "http.response": { |
| 255 | + "type": "long", |
| 256 | + "script": """ |
| 257 | + String response=dissect('%{clientip} %{ident} %{auth} [%{@timestamp}] "%{verb} %{request} HTTP/%{httpversion}" %{response} %{size}').extract(doc["message"].value)?.response; |
| 258 | + if (response != null) emit(Integer.parseInt(response)); |
| 259 | + """ |
| 260 | + } |
| 261 | + }, |
| 262 | + "query": { |
| 263 | + "match": { |
| 264 | + "http.response": "304" |
| 265 | + } |
| 266 | + }, |
| 267 | + "fields" : ["http.response"] |
| 268 | +} |
| 269 | +---- |
| 270 | +// TEST[continued] |
| 271 | +// TEST[s/_search/_search\?filter_path=hits/] |
| 272 | + |
| 273 | +[source,console-result] |
| 274 | +---- |
| 275 | +{ |
| 276 | + "hits" : { |
| 277 | + "total" : { |
| 278 | + "value" : 1, |
| 279 | + "relation" : "eq" |
| 280 | + }, |
| 281 | + "max_score" : 1.0, |
| 282 | + "hits" : [ |
| 283 | + { |
| 284 | + "_index" : "my-index", |
| 285 | + "_id" : "D47UqXkBByC8cgZrkbOm", |
| 286 | + "_score" : 1.0, |
| 287 | + "_source" : { |
| 288 | + "timestamp" : "2020-04-30T14:31:22-05:00", |
| 289 | + "message" : "247.37.0.0 - - [30/Apr/2020:14:31:22 -0500] \"GET /images/hm_nbg.jpg HTTP/1.0\" 304 0" |
| 290 | + }, |
| 291 | + "fields" : { |
| 292 | + "http.response" : [ |
| 293 | + 304 |
| 294 | + ] |
| 295 | + } |
| 296 | + } |
| 297 | + ] |
| 298 | + } |
| 299 | +} |
| 300 | +---- |
| 301 | +// TESTRESPONSE[s/"_id" : "D47UqXkBByC8cgZrkbOm"/"_id": $body.hits.hits.0._id/] |
0 commit comments