|
@@ -91,7 +91,7 @@ calculates the day of the week based on the value of `timestamp`, and uses
|
|
|
|
|
|
[source,console]
|
|
|
----
|
|
|
-PUT my-index/
|
|
|
+PUT my-index-000001/
|
|
|
{
|
|
|
"mappings": {
|
|
|
"runtime": {
|
|
@@ -130,7 +130,7 @@ the index mapping as runtime fields:
|
|
|
|
|
|
[source,console]
|
|
|
----
|
|
|
-PUT my-index
|
|
|
+PUT my-index-000001
|
|
|
{
|
|
|
"mappings": {
|
|
|
"dynamic": "runtime",
|
|
@@ -152,7 +152,7 @@ a runtime field without a script, such as `day_of_week`:
|
|
|
|
|
|
[source,console]
|
|
|
----
|
|
|
-PUT my-index/
|
|
|
+PUT my-index-000001/
|
|
|
{
|
|
|
"mappings": {
|
|
|
"runtime": {
|
|
@@ -194,7 +194,7 @@ remove a runtime field from the mappings, set the value of the runtime field to
|
|
|
|
|
|
[source,console]
|
|
|
----
|
|
|
-PUT my-index/_mapping
|
|
|
+PUT my-index-000001/_mapping
|
|
|
{
|
|
|
"runtime": {
|
|
|
"day_of_week": null
|
|
@@ -233,7 +233,7 @@ and only within the context of this search request:
|
|
|
|
|
|
[source,console]
|
|
|
----
|
|
|
-GET my-index/_search
|
|
|
+GET my-index-000001/_search
|
|
|
{
|
|
|
"runtime_mappings": {
|
|
|
"day_of_week": {
|
|
@@ -262,7 +262,7 @@ other runtime fields. For example, let's say you bulk index some sensor data:
|
|
|
|
|
|
[source,console]
|
|
|
----
|
|
|
-POST my-index/_bulk?refresh=true
|
|
|
+POST my-index-000001/_bulk?refresh=true
|
|
|
{"index":{}}
|
|
|
{"@timestamp":1516729294000,"model_number":"QVKC92Q","measures":{"voltage":"5.2","start": "300","end":"8675309"}}
|
|
|
{"index":{}}
|
|
@@ -285,7 +285,7 @@ your indexed fields and modify the data type:
|
|
|
|
|
|
[source,console]
|
|
|
----
|
|
|
-PUT my-index/_mapping
|
|
|
+PUT my-index-000001/_mapping
|
|
|
{
|
|
|
"runtime": {
|
|
|
"measures.start": {
|
|
@@ -312,7 +312,7 @@ Now, you can easily run an
|
|
|
|
|
|
[source,console]
|
|
|
----
|
|
|
-GET my-index/_search
|
|
|
+GET my-index-000001/_search
|
|
|
{
|
|
|
"aggs": {
|
|
|
"avg_start": {
|
|
@@ -360,7 +360,7 @@ compute statistics over numeric values extracted from the aggregated documents.
|
|
|
|
|
|
[source,console]
|
|
|
----
|
|
|
-GET my-index/_search
|
|
|
+GET my-index-000001/_search
|
|
|
{
|
|
|
"runtime_mappings": {
|
|
|
"duration": {
|
|
@@ -413,11 +413,11 @@ script, and returns the value as part of the query. Because the runtime field
|
|
|
shadows the mapped field, you can override the value returned in search without
|
|
|
modifying the mapped field.
|
|
|
|
|
|
-For example, let's say you indexed the following documents into `my-index`:
|
|
|
+For example, let's say you indexed the following documents into `my-index-000001`:
|
|
|
|
|
|
[source,console]
|
|
|
----
|
|
|
-POST my-index/_bulk?refresh=true
|
|
|
+POST my-index-000001/_bulk?refresh=true
|
|
|
{"index":{}}
|
|
|
{"@timestamp":1516729294000,"model_number":"QVKC92Q","measures":{"voltage":5.2}}
|
|
|
{"index":{}}
|
|
@@ -442,7 +442,7 @@ If you search for documents where the model number matches `HG537PU`:
|
|
|
|
|
|
[source,console]
|
|
|
----
|
|
|
-GET my-index/_search
|
|
|
+GET my-index-000001/_search
|
|
|
{
|
|
|
"query": {
|
|
|
"match": {
|
|
@@ -468,7 +468,7 @@ The response includes indexed values for documents matching model number
|
|
|
"max_score" : 1.0296195,
|
|
|
"hits" : [
|
|
|
{
|
|
|
- "_index" : "my-index",
|
|
|
+ "_index" : "my-index-000001",
|
|
|
"_id" : "F1BeSXYBg_szTodcYCmk",
|
|
|
"_score" : 1.0296195,
|
|
|
"_source" : {
|
|
@@ -480,7 +480,7 @@ The response includes indexed values for documents matching model number
|
|
|
}
|
|
|
},
|
|
|
{
|
|
|
- "_index" : "my-index",
|
|
|
+ "_index" : "my-index-000001",
|
|
|
"_id" : "l02aSXYBkpNf6QRDO62Q",
|
|
|
"_score" : 1.0296195,
|
|
|
"_source" : {
|
|
@@ -509,7 +509,7 @@ for documents matching the search request:
|
|
|
|
|
|
[source,console]
|
|
|
----
|
|
|
-POST my-index/_search
|
|
|
+POST my-index-000001/_search
|
|
|
{
|
|
|
"runtime_mappings": {
|
|
|
"measures.voltage": {
|
|
@@ -549,7 +549,7 @@ which still returns in the response:
|
|
|
"max_score" : 1.0296195,
|
|
|
"hits" : [
|
|
|
{
|
|
|
- "_index" : "my-index",
|
|
|
+ "_index" : "my-index-000001",
|
|
|
"_id" : "F1BeSXYBg_szTodcYCmk",
|
|
|
"_score" : 1.0296195,
|
|
|
"_source" : {
|
|
@@ -566,7 +566,7 @@ which still returns in the response:
|
|
|
}
|
|
|
},
|
|
|
{
|
|
|
- "_index" : "my-index",
|
|
|
+ "_index" : "my-index-000001",
|
|
|
"_id" : "l02aSXYBkpNf6QRDO62Q",
|
|
|
"_score" : 1.0296195,
|
|
|
"_source" : {
|
|
@@ -607,7 +607,7 @@ the request so that new fields are added to the mapping as runtime fields.
|
|
|
|
|
|
[source,console]
|
|
|
----
|
|
|
-PUT my-index/
|
|
|
+PUT my-index-000001/
|
|
|
{
|
|
|
"mappings": {
|
|
|
"dynamic": "runtime",
|
|
@@ -634,7 +634,7 @@ Let's ingest some sample data, which will result in two indexed fields:
|
|
|
|
|
|
[source,console]
|
|
|
----
|
|
|
-POST /my-index/_bulk?refresh
|
|
|
+POST /my-index-000001/_bulk?refresh
|
|
|
{ "index": {}}
|
|
|
{ "@timestamp": "2020-06-21T15:00:01-05:00", "message" : "211.11.9.0 - - [2020-06-21T15:00:01-05:00] \"GET /english/index.html HTTP/1.0\" 304 0"}
|
|
|
{ "index": {}}
|
|
@@ -671,7 +671,7 @@ modify the mapping without changing any field values.
|
|
|
|
|
|
[source,console]
|
|
|
----
|
|
|
-GET my-index/_search
|
|
|
+GET my-index-000001/_search
|
|
|
{
|
|
|
"fields": [
|
|
|
"@timestamp",
|
|
@@ -688,7 +688,7 @@ the `message` field and will further refine the query:
|
|
|
|
|
|
[source,console]
|
|
|
----
|
|
|
-PUT /my-index/_mapping
|
|
|
+PUT /my-index-000001/_mapping
|
|
|
{
|
|
|
"runtime": {
|
|
|
"client_ip": {
|
|
@@ -707,7 +707,7 @@ runtime field:
|
|
|
|
|
|
[source,console]
|
|
|
----
|
|
|
-GET my-index/_search
|
|
|
+GET my-index-000001/_search
|
|
|
{
|
|
|
"size": 1,
|
|
|
"query": {
|
|
@@ -737,7 +737,7 @@ address.
|
|
|
"max_score" : 1.0,
|
|
|
"hits" : [
|
|
|
{
|
|
|
- "_index" : "my-index",
|
|
|
+ "_index" : "my-index-000001",
|
|
|
"_id" : "oWs5KXYB-XyJbifr9mrz",
|
|
|
"_score" : 1.0,
|
|
|
"_source" : {
|
|
@@ -787,11 +787,11 @@ valves. The connected sensors are only capable of reporting a fraction of
|
|
|
the true readings. Rather than outfit the pressure valves with new sensors,
|
|
|
you decide to calculate the values based on reported readings. Based on the
|
|
|
reported data, you define the following fields in your mapping for
|
|
|
-`my-index`:
|
|
|
+`my-index-000001`:
|
|
|
|
|
|
[source,console]
|
|
|
----
|
|
|
-PUT my-index/
|
|
|
+PUT my-index-000001/
|
|
|
{
|
|
|
"mappings": {
|
|
|
"properties": {
|
|
@@ -817,7 +817,7 @@ You then bulk index some sample data from your sensors. This data includes
|
|
|
|
|
|
[source,console]
|
|
|
----
|
|
|
-POST my-index/_bulk?refresh=true
|
|
|
+POST my-index-000001/_bulk?refresh=true
|
|
|
{"index":{}}
|
|
|
{"timestamp": 1516729294000, "temperature": 200, "voltage": 5.2, "node": "a"}
|
|
|
{"index":{}}
|
|
@@ -840,7 +840,7 @@ voltage and multiplies it by `2`:
|
|
|
|
|
|
[source,console]
|
|
|
----
|
|
|
-PUT my-index/_mapping
|
|
|
+PUT my-index-000001/_mapping
|
|
|
{
|
|
|
"runtime": {
|
|
|
"voltage_corrected": {
|
|
@@ -864,7 +864,7 @@ parameter on the `_search` API:
|
|
|
|
|
|
[source,console]
|
|
|
----
|
|
|
-GET my-index/_search
|
|
|
+GET my-index-000001/_search
|
|
|
{
|
|
|
"fields": [
|
|
|
"voltage_corrected",
|
|
@@ -889,7 +889,7 @@ GET my-index/_search
|
|
|
"max_score" : 1.0,
|
|
|
"hits" : [
|
|
|
{
|
|
|
- "_index" : "my-index",
|
|
|
+ "_index" : "my-index-000001",
|
|
|
"_id" : "z4TCrHgBdg9xpPrU6z9k",
|
|
|
"_score" : 1.0,
|
|
|
"_source" : {
|
|
@@ -908,7 +908,7 @@ GET my-index/_search
|
|
|
}
|
|
|
},
|
|
|
{
|
|
|
- "_index" : "my-index",
|
|
|
+ "_index" : "my-index-000001",
|
|
|
"_id" : "0ITCrHgBdg9xpPrU6z9k",
|
|
|
"_score" : 1.0,
|
|
|
"_source" : {
|
|
@@ -940,7 +940,7 @@ multiplier for reported sensor data should be `4`. To gain greater performance,
|
|
|
you decide to index the `voltage_corrected` runtime field with the new
|
|
|
`multiplier` parameter.
|
|
|
|
|
|
-In a new index named `my-index-00001`, copy the `voltage_corrected` runtime
|
|
|
+In a new index named `my-index-000001`, copy the `voltage_corrected` runtime
|
|
|
field definition into the mappings of the new index. It's that simple! You can
|
|
|
add an optional parameter named `on_script_error` that determines whether to
|
|
|
reject the entire document if the script throws an error at index time
|
|
@@ -948,7 +948,7 @@ reject the entire document if the script throws an error at index time
|
|
|
|
|
|
[source,console]
|
|
|
----
|
|
|
-PUT my-index-00001/
|
|
|
+PUT my-index-000001/
|
|
|
{
|
|
|
"mappings": {
|
|
|
"properties": {
|
|
@@ -984,11 +984,11 @@ PUT my-index-00001/
|
|
|
index time. Setting the value to `ignore` will register the field in the
|
|
|
document’s `_ignored` metadata field and continue indexing.
|
|
|
|
|
|
-Bulk index some sample data from your sensors into the `my-index-00001` index:
|
|
|
+Bulk index some sample data from your sensors into the `my-index-000001` index:
|
|
|
|
|
|
[source,console]
|
|
|
----
|
|
|
-POST my-index-00001/_bulk?refresh=true
|
|
|
+POST my-index-000001/_bulk?refresh=true
|
|
|
{ "index": {}}
|
|
|
{ "timestamp": 1516729294000, "temperature": 200, "voltage": 5.2, "node": "a"}
|
|
|
{ "index": {}}
|
|
@@ -1012,7 +1012,7 @@ the `_search` API to retrieve the fields you want:
|
|
|
|
|
|
[source,console]
|
|
|
----
|
|
|
-POST my-index-00001/_search
|
|
|
+POST my-index-000001/_search
|
|
|
{
|
|
|
"query": {
|
|
|
"range": {
|
|
@@ -1044,7 +1044,7 @@ match the range query, based on the calculated value of the included script:
|
|
|
"max_score" : 1.0,
|
|
|
"hits" : [
|
|
|
{
|
|
|
- "_index" : "my-index-00001",
|
|
|
+ "_index" : "my-index-000001",
|
|
|
"_id" : "yoSLrHgBdg9xpPrUZz_P",
|
|
|
"_score" : 1.0,
|
|
|
"_source" : {
|
|
@@ -1063,7 +1063,7 @@ match the range query, based on the calculated value of the included script:
|
|
|
}
|
|
|
},
|
|
|
{
|
|
|
- "_index" : "my-index-00001",
|
|
|
+ "_index" : "my-index-000001",
|
|
|
"_id" : "y4SLrHgBdg9xpPrUZz_P",
|
|
|
"_score" : 1.0,
|
|
|
"_source" : {
|
|
@@ -1103,12 +1103,12 @@ time for these fields.
|
|
|
==== Define indexed fields as a starting point
|
|
|
|
|
|
You can start with a simple example by adding the `@timestamp` and `message`
|
|
|
-fields to the `my-index` mapping as indexed fields. To remain flexible, use
|
|
|
+fields to the `my-index-000001` mapping as indexed fields. To remain flexible, use
|
|
|
`wildcard` as the field type for `message`:
|
|
|
|
|
|
[source,console]
|
|
|
----
|
|
|
-PUT /my-index/
|
|
|
+PUT /my-index-000001/
|
|
|
{
|
|
|
"mappings": {
|
|
|
"properties": {
|
|
@@ -1128,7 +1128,7 @@ PUT /my-index/
|
|
|
==== Ingest some data
|
|
|
After mapping the fields you want to retrieve, index a few records from
|
|
|
your log data into {es}. The following request uses the <<docs-bulk,bulk API>>
|
|
|
-to index raw log data into `my-index`. Instead of indexing all of your log
|
|
|
+to index raw log data into `my-index-000001`. Instead of indexing all of your log
|
|
|
data, you can use a small sample to experiment with runtime fields.
|
|
|
|
|
|
The final document is not a valid Apache log format, but we can account for
|
|
@@ -1136,7 +1136,7 @@ that scenario in our script.
|
|
|
|
|
|
[source,console]
|
|
|
----
|
|
|
-POST /my-index/_bulk?refresh
|
|
|
+POST /my-index-000001/_bulk?refresh
|
|
|
{"index":{}}
|
|
|
{"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"}
|
|
|
{"index":{}}
|
|
@@ -1158,7 +1158,7 @@ At this point, you can view how {es} stores your raw data.
|
|
|
|
|
|
[source,console]
|
|
|
----
|
|
|
-GET /my-index
|
|
|
+GET /my-index-000001
|
|
|
----
|
|
|
// TEST[continued]
|
|
|
|
|
@@ -1167,7 +1167,7 @@ The mapping contains two fields: `@timestamp` and `message`.
|
|
|
[source,console-result]
|
|
|
----
|
|
|
{
|
|
|
- "my-index" : {
|
|
|
+ "my-index-000001" : {
|
|
|
"aliases" : { },
|
|
|
"mappings" : {
|
|
|
"properties" : {
|
|
@@ -1187,24 +1187,24 @@ The mapping contains two fields: `@timestamp` and `message`.
|
|
|
}
|
|
|
}
|
|
|
----
|
|
|
-// TESTRESPONSE[s/\.\.\./"settings": $body.my-index.settings/]
|
|
|
+// TESTRESPONSE[s/\.\.\./"settings": $body.my-index-000001.settings/]
|
|
|
|
|
|
[[runtime-examples-grok]]
|
|
|
==== Define a runtime field with a grok pattern
|
|
|
If you want to retrieve results that include `clientip`, you can add that
|
|
|
field as a runtime field in the mapping. The following runtime script defines a
|
|
|
-grok pattern that extracts structured fields out of a single text
|
|
|
+<<grok,grok pattern>> that extracts structured fields out of a single text
|
|
|
field within a document. A grok pattern is like a regular expression that
|
|
|
-supports aliased expressions that you can reuse. See <<grok-basics,Grok basics>> to learn more about grok syntax.
|
|
|
+supports aliased expressions that you can reuse.
|
|
|
|
|
|
The script matches on the `%{COMMONAPACHELOG}` log pattern, which understands
|
|
|
the structure of Apache logs. If the pattern matches, the script emits the
|
|
|
-value matching IP address. If the pattern doesn't match
|
|
|
+value of the matching IP address. If the pattern doesn't match
|
|
|
(`clientip != null`), the script just returns the field value without crashing.
|
|
|
|
|
|
[source,console]
|
|
|
----
|
|
|
-PUT my-index/_mappings
|
|
|
+PUT my-index-000001/_mappings
|
|
|
{
|
|
|
"runtime": {
|
|
|
"http.clientip": {
|
|
@@ -1221,6 +1221,37 @@ PUT my-index/_mappings
|
|
|
<1> This condition ensures that the script doesn't crash even if the pattern of
|
|
|
the message doesn't match.
|
|
|
|
|
|
+Alternatively, you can define the same runtime field but in the context of a
|
|
|
+search request. The runtime definition and the script are exactly the same as
|
|
|
+the one defined previously in the index mapping. Just copy that definition into
|
|
|
+the search request under the `runtime_mappings` section and include a query
|
|
|
+that matches on the runtime field. This query returns the same results as if
|
|
|
+you defined a search query for the `http.clientip` runtime field in your index
|
|
|
+mappings, but only in the context of this specific search:
|
|
|
+
|
|
|
+[source,console]
|
|
|
+----
|
|
|
+GET my-index-000001/_search
|
|
|
+{
|
|
|
+ "runtime_mappings": {
|
|
|
+ "http.clientip": {
|
|
|
+ "type": "ip",
|
|
|
+ "script": """
|
|
|
+ String clientip=grok('%{COMMONAPACHELOG}').extract(doc["message"].value)?.clientip;
|
|
|
+ if (clientip != null) emit(clientip);
|
|
|
+ """
|
|
|
+ }
|
|
|
+ },
|
|
|
+ "query": {
|
|
|
+ "match": {
|
|
|
+ "http.clientip": "40.135.0.0"
|
|
|
+ }
|
|
|
+ },
|
|
|
+ "fields" : ["http.clientip"]
|
|
|
+}
|
|
|
+----
|
|
|
+// TEST[continued]
|
|
|
+
|
|
|
[[runtime-examples-grok-ip]]
|
|
|
===== Search for a specific IP address
|
|
|
Using the `http.clientip` runtime field, you can define a simple query to run a
|
|
@@ -1228,7 +1259,7 @@ search for a specific IP address and return all related fields.
|
|
|
|
|
|
[source,console]
|
|
|
----
|
|
|
-GET my-index/_search
|
|
|
+GET my-index-000001/_search
|
|
|
{
|
|
|
"query": {
|
|
|
"match": {
|
|
@@ -1267,7 +1298,7 @@ data that doesn't match the grok pattern.
|
|
|
"max_score" : 1.0,
|
|
|
"hits" : [
|
|
|
{
|
|
|
- "_index" : "my-index",
|
|
|
+ "_index" : "my-index-000001",
|
|
|
"_id" : "FdLqu3cBhqheMnFKd0gK",
|
|
|
"_score" : 1.0,
|
|
|
"_source" : {
|
|
@@ -1301,7 +1332,7 @@ You can also run a <<query-dsl-range-query,range query>> that operates on the
|
|
|
|
|
|
[source,console]
|
|
|
----
|
|
|
-GET my-index/_search
|
|
|
+GET my-index-000001/_search
|
|
|
{
|
|
|
"query": {
|
|
|
"range": {
|
|
@@ -1329,7 +1360,7 @@ timestamp falls within the defined range.
|
|
|
"max_score" : 1.0,
|
|
|
"hits" : [
|
|
|
{
|
|
|
- "_index" : "my-index",
|
|
|
+ "_index" : "my-index-000001",
|
|
|
"_id" : "hdEhyncBRSB6iD-PoBqe",
|
|
|
"_score" : 1.0,
|
|
|
"_source" : {
|
|
@@ -1338,7 +1369,7 @@ timestamp falls within the defined range.
|
|
|
}
|
|
|
},
|
|
|
{
|
|
|
- "_index" : "my-index",
|
|
|
+ "_index" : "my-index-000001",
|
|
|
"_id" : "htEhyncBRSB6iD-PoBqe",
|
|
|
"_score" : 1.0,
|
|
|
"_source" : {
|
|
@@ -1368,7 +1399,7 @@ successful dissect patterns.
|
|
|
|
|
|
[source,console]
|
|
|
----
|
|
|
-PUT my-index/_mappings
|
|
|
+PUT my-index-000001/_mappings
|
|
|
{
|
|
|
"runtime": {
|
|
|
"http.client.ip": {
|
|
@@ -1387,7 +1418,7 @@ Similarly, you can define a dissect pattern to extract the https://developer.moz
|
|
|
|
|
|
[source,console]
|
|
|
----
|
|
|
-PUT my-index/_mappings
|
|
|
+PUT my-index-000001/_mappings
|
|
|
{
|
|
|
"runtime": {
|
|
|
"http.response": {
|
|
@@ -1407,7 +1438,7 @@ You can then run a query to retrieve a specific HTTP response using the
|
|
|
|
|
|
[source,console]
|
|
|
----
|
|
|
-GET my-index/_search
|
|
|
+GET my-index-000001/_search
|
|
|
{
|
|
|
"query": {
|
|
|
"match": {
|
|
@@ -1433,7 +1464,7 @@ The response includes a single document where the HTTP response is `304`:
|
|
|
"max_score" : 1.0,
|
|
|
"hits" : [
|
|
|
{
|
|
|
- "_index" : "my-index",
|
|
|
+ "_index" : "my-index-000001",
|
|
|
"_id" : "A2qDy3cBWRMvVAuI7F8M",
|
|
|
"_score" : 1.0,
|
|
|
"_source" : {
|