Browse Source

[DOCS] Fixing Painless tests (#68157)

* Fixing Painless tests.

* Update runtime field context to fix test cases.

* Remove watcher logging from usage API and replace test.

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
Adam Locke 4 years ago
parent
commit
a39eef6309

+ 3 - 1
docs/build.gradle

@@ -1365,7 +1365,9 @@ setups['seats'] = '''
             {"index":{"_id": "9"}}
             {"theatre": "Skyline", "play": "Sunnyside Down", "actors": ["Krissy Smith", "Joe Muir", "Ryan Earns", "Nora Blue", "Mike Candlestick", "Jacey Bell"], "date": "2018-6-12", "time": "4:00PM", "cost": 21.25, "row": 8, "number": 15, "sold": true}
             {"index":{"_id": "10"}}
-            {"theatre": "Graye", "play": "Line and Single", "actors": ["Nora Blue", "Mike Candlestick"], "date": "2018-6-5", "time": "2:00PM", "cost": 30.0, "row": 1, "number": 2, "sold": false }
+            {"theatre": "Graye", "play": "Line and Single", "actors": ["Nora Blue", "Mike Candlestick"], "date": "2018-6-5", "time": "2:00PM", "cost": 30.0, "row": 1, "number": 2, "sold": false}
+            {"index":{"_id":"11"}}
+            {"theatre": "Graye", "play": "Hamilton", "actors": ["Lin-Manuel Miranda", "Leslie Odom Jr."] ,"date":"2018-6-5", "time": "2:00PM", "cost": 5000.0, "row":1, "number": 20, "sold": true}
             '''
 setups['kibana_sample_data_ecommerce'] = '''
   - do:

+ 2 - 0
docs/painless/painless-contexts/painless-context-examples.asciidoc

@@ -103,5 +103,7 @@ POST seats/_bulk?pipeline=seats&refresh=true
 {"theatre":"Skyline","play":"Sunnyside Down","actors":["Krissy Smith","Joe Muir","Ryan Earns","Nora Blue","Mike Candlestick","Jacey Bell"],"date":"2018-6-12","time":"4:00PM","cost":21.25,"row":8,"number":15,"sold":true}
 {"create":{"_index":"seats","_id":"10"}}
 {"theatre":"Graye","play":"Line and Single","actors":["Nora Blue","Mike Candlestick"],"date":"2018-6-5","time":"2:00PM","cost":30,"row":1,"number":2,"sold":false}
+{"create":{"_index":"seats","_id":"11"}}
+{"theatre":"Graye","play":"Hamilton","actors":["Lin-Manuel Miranda","Leslie Odom Jr."],"date":"2018-6-5","time":"2:00PM","cost":5000,"row":1,"number":20,"sold":true}
 ----
 // TEST[continued]

+ 4 - 4
docs/painless/painless-contexts/painless-runtime-fields-context.asciidoc

@@ -96,7 +96,7 @@ defined in the mappings.
   ...
   "hits" : {
     "total" : {
-      "value" : 10,
+      "value" : 11,
       "relation" : "eq"
     },
     "max_score" : null,
@@ -108,11 +108,11 @@ defined in the mappings.
       "sum_other_doc_count" : 0,
       "buckets" : [
         {
-          "key" : "THURSDAY",
-          "doc_count" : 4
+          "key" : "TUESDAY",
+          "doc_count" : 5
         },
         {
-          "key" : "TUESDAY",
+          "key" : "THURSDAY",
           "doc_count" : 4
         },
         {

+ 10 - 10
docs/painless/painless-contexts/painless-watcher-condition-context.asciidoc

@@ -78,7 +78,7 @@ the elements of the list in a pipeline.
 The following action condition script controls execution of the my_log action based
 on the value of the seats sold for the plays in the data set. The script aggregates
 the total sold seats for each play and returns true if there is at least one play
-that has sold over $50,000.
+that has sold over $10,000.
 
 [source,console]
 ----
@@ -94,12 +94,18 @@ POST _watcher/watch/_execute
             "query" : {
               "term": { "sold": "true"}
             },
+            "size": 0,
             "aggs" : {
               "theatres" : {
                 "terms" : { "field" : "play" },
                 "aggs" : {
                   "money" : {
-                    "sum": { "field" : "cost" }
+                    "sum": {
+                      "field" : "cost",
+                      "script": {
+                       "source": "doc.cost.value * doc.number.value"
+                      }
+                    }
                   }
                 }
               }
@@ -114,11 +120,11 @@ POST _watcher/watch/_execute
           "script" :
           """
             return ctx.payload.aggregations.theatres.buckets.stream()
-              .anyMatch(theatre -> theatre.money.value > 50000)       <2>
+              .anyMatch(theatre -> theatre.money.value > 10000)       <2>
           """
         },
         "logging" : {
-          "text" : "At least one play has grossed over $50,000: {{ctx.payload.aggregations.theatres.buckets}}"
+          "text" : "At least one play has grossed over $10,000: {{ctx.payload.aggregations.theatres.buckets}}"
         }
       }
     }
@@ -133,9 +139,3 @@ differences below are subtle and are worth calling out.
 <1> The location of the condition is no longer at the top level, but is within
 an individual action.
 <2> Instead of a filter, `anyMatch` is used to return a boolean value
-
-The following example shows scripted watch and action conditions within the
-context of a complete watch. This watch also uses a scripted
-<<painless-watcher-transform-context, transform>>.
-
-include::painless-watcher-context-example.asciidoc[]

+ 0 - 159
docs/painless/painless-contexts/painless-watcher-context-example.asciidoc

@@ -1,159 +0,0 @@
-[source,console]
-----
-POST _watcher/watch/_execute
-{
-  "watch" : {
-    "metadata" : { "high_threshold": 50000, "low_threshold": 15000 },
-    "trigger" : { "schedule" : { "interval" : "24h" } },
-    "input" : {
-      "search" : {
-        "request" : {
-          "indices" : [ "seats" ],
-          "body" : {
-            "query" : {
-              "term": { "sold": "true"}
-            },
-            "aggs" : {
-              "theatres" : {
-                "terms" : { "field" : "play" },
-                "aggs" : {
-                  "money" : {
-                    "sum": { "field" : "cost" }
-                  }
-                }
-              }
-            }
-          }
-        }
-      }
-    },
-    "condition" : {
-      "script" :
-      """
-        return ctx.payload.aggregations.theatres.buckets.stream()
-          .anyMatch(theatre -> theatre.money.value < ctx.metadata.low_threshold ||
-                               theatre.money.value > ctx.metadata.high_threshold)
-      """
-    },
-    "transform" : {
-      "script":
-      """
-        return [
-          'money_makers': ctx.payload.aggregations.theatres.buckets.stream()
-            .filter(t -> {
-                return t.money.value > ctx.metadata.high_threshold
-            })
-            .map(t -> {
-                return ['play': t.key, 'total_value': t.money.value ]
-            }).collect(Collectors.toList()),
-          'duds' : ctx.payload.aggregations.theatres.buckets.stream()
-            .filter(t -> {
-                return t.money.value < ctx.metadata.low_threshold
-            })
-            .map(t -> {
-                return ['play': t.key, 'total_value': t.money.value ]
-            }).collect(Collectors.toList())
-          ]
-      """
-    },
-    "actions" : {
-      "log_money_makers" : {
-        "condition": {
-          "script" : "return ctx.payload.money_makers.size() > 0"
-        },
-        "transform": {
-          "script" :
-          """
-          def formatter = NumberFormat.getCurrencyInstance();
-          return [
-            'plays_value': ctx.payload.money_makers.stream()
-              .map(t-> formatter.format(t.total_value) + ' for the play ' + t.play)
-              .collect(Collectors.joining(", "))
-          ]
-          """
-        },
-        "logging" : {
-          "text" : "The following plays contain the highest grossing total income: {{ctx.payload.plays_value}}"
-        }
-      },
-      "log_duds" : {
-        "condition": {
-          "script" : "return ctx.payload.duds.size() > 0"
-        },
-        "transform": {
-          "script" :
-          """
-          def formatter = NumberFormat.getCurrencyInstance();
-          return [
-            'plays_value': ctx.payload.duds.stream()
-              .map(t-> formatter.format(t.total_value) + ' for the play ' + t.play)
-              .collect(Collectors.joining(", "))
-          ]
-          """
-        },
-        "logging" : {
-          "text" : "The following plays need more advertising due to their low total income: {{ctx.payload.plays_value}}"
-        }
-      }
-    }
-  }
-}
-----
-// TEST[setup:seats]
-
-The following example shows the use of metadata and transforming dates into a readable format.
-
-[source,console]
-----
-POST _watcher/watch/_execute
-{
-  "watch" : {
-    "metadata" : { "min_hits": 10000 },
-    "trigger" : { "schedule" : { "interval" : "24h" } },
-    "input" : {
-      "search" : {
-        "request" : {
-          "indices" : [ "seats" ],
-          "body" : {
-            "query" : {
-              "term": { "sold": "true"}
-            },
-            "aggs" : {
-              "theatres" : {
-                "terms" : { "field" : "play" },
-                "aggs" : {
-                  "money" : {
-                    "sum": { "field" : "cost" }
-                  }
-                }
-              }
-            }
-          }
-        }
-      }
-    },
-    "condition" : {
-      "script" :
-      """
-        return ctx.payload.hits.total > ctx.metadata.min_hits
-      """
-    },
-    "transform" : {
-      "script" :
-      """
-        def theDate = ZonedDateTime.ofInstant(ctx.execution_time.toInstant(), ctx.execution_time.getZone());
-        return ['human_date': DateTimeFormatter.RFC_1123_DATE_TIME.format(theDate),
-                'aggregations': ctx.payload.aggregations]
-      """
-    },
-    "actions" : {
-      "my_log" : {
-        "logging" : {
-          "text" : "The watch was successfully executed on {{ctx.payload.human_date}} and contained {{ctx.payload.aggregations.theatres.buckets.size}} buckets"
-        }
-      }
-    }
-  }
-}
-----
-// TEST[setup:seats]

+ 164 - 1
docs/painless/painless-contexts/painless-watcher-transform-context.asciidoc

@@ -157,4 +157,167 @@ The following example shows scripted watch and action transforms within the
 context of a complete watch. This watch also uses a scripted
 <<painless-watcher-condition-context, condition>>.
 
-include::painless-watcher-context-example.asciidoc[]
+[source,console]
+----
+POST _watcher/watch/_execute
+{
+  "watch" : {
+    "metadata" : { "high_threshold": 4000, "low_threshold": 1000 },
+    "trigger" : { "schedule" : { "interval" : "24h" } },
+    "input" : {
+      "search" : {
+        "request" : {
+          "indices" : [ "seats" ],
+          "body" : {
+            "query" : {
+              "term": { "sold": "true"}
+            },
+            "aggs" : {
+              "theatres" : {
+                "terms" : { "field" : "play" },
+                "aggs" : {
+                  "money" : {
+                    "sum": {
+                      "field" : "cost",
+                      "script": {
+                       "source": "doc.cost.value * doc.number.value"
+                      }
+                    }
+                  }
+                }
+              }
+            }
+          }
+        }
+      }
+    },
+    "condition" : {
+      "script" :
+      """
+        return ctx.payload.aggregations.theatres.buckets.stream()
+          .anyMatch(theatre -> theatre.money.value < ctx.metadata.low_threshold ||
+                               theatre.money.value > ctx.metadata.high_threshold)
+      """
+    },
+    "transform" : {
+      "script":
+      """
+        return [
+          'money_makers': ctx.payload.aggregations.theatres.buckets.stream()
+            .filter(t -> {
+                return t.money.value > ctx.metadata.high_threshold
+            })
+            .map(t -> {
+                return ['play': t.key, 'total_value': t.money.value ]
+            }).collect(Collectors.toList()),
+          'duds' : ctx.payload.aggregations.theatres.buckets.stream()
+            .filter(t -> {
+                return t.money.value < ctx.metadata.low_threshold
+            })
+            .map(t -> {
+                return ['play': t.key, 'total_value': t.money.value ]
+            }).collect(Collectors.toList())
+          ]
+      """
+    },
+    "actions" : {
+      "log_money_makers" : {
+        "condition": {
+          "script" : "return ctx.payload.money_makers.size() > 0"
+        },
+        "transform": {
+          "script" :
+          """
+          def formatter = NumberFormat.getCurrencyInstance();
+          return [
+            'plays_value': ctx.payload.money_makers.stream()
+              .map(t-> formatter.format(t.total_value) + ' for the play ' + t.play)
+              .collect(Collectors.joining(", "))
+          ]
+          """
+        },
+        "logging" : {
+          "text" : "The following plays contain the highest grossing total income: {{ctx.payload.plays_value}}"
+        }
+      },
+      "log_duds" : {
+        "condition": {
+          "script" : "return ctx.payload.duds.size() > 0"
+        },
+        "transform": {
+          "script" :
+          """
+          def formatter = NumberFormat.getCurrencyInstance();
+          return [
+            'plays_value': ctx.payload.duds.stream()
+              .map(t-> formatter.format(t.total_value) + ' for the play ' + t.play)
+              .collect(Collectors.joining(", "))
+          ]
+          """
+        },
+        "logging" : {
+          "text" : "The following plays need more advertising due to their low total income: {{ctx.payload.plays_value}}"
+        }
+      }
+    }
+  }
+}
+----
+// TEST[setup:seats]
+
+The following example shows the use of metadata and transforming dates into a readable format.
+
+[source,console]
+----
+POST _watcher/watch/_execute
+{
+  "watch" : {
+    "metadata" : { "min_hits": 10 },
+    "trigger" : { "schedule" : { "interval" : "24h" } },
+    "input" : {
+      "search" : {
+        "request" : {
+          "indices" : [ "seats" ],
+          "body" : {
+            "query" : {
+              "term": { "sold": "true"}
+            },
+            "aggs" : {
+              "theatres" : {
+                "terms" : { "field" : "play" },
+                "aggs" : {
+                  "money" : {
+                    "sum": { "field" : "cost" }
+                  }
+                }
+              }
+            }
+          }
+        }
+      }
+    },
+    "condition" : {
+      "script" :
+      """
+        return ctx.payload.hits.total > ctx.metadata.min_hits
+      """
+    },
+    "transform" : {
+      "script" :
+      """
+        def theDate = ZonedDateTime.ofInstant(ctx.execution_time.toInstant(), ctx.execution_time.getZone());
+        return ['human_date': DateTimeFormatter.RFC_1123_DATE_TIME.format(theDate),
+                'aggregations': ctx.payload.aggregations]
+      """
+    },
+    "actions" : {
+      "my_log" : {
+        "logging" : {
+          "text" : "The watch was successfully executed on {{ctx.payload.human_date}} and contained {{ctx.payload.aggregations.theatres.buckets.size}} buckets"
+        }
+      }
+    }
+  }
+}
+----
+// TEST[setup:seats]

+ 4 - 6
docs/reference/rest-api/usage.asciidoc

@@ -39,6 +39,9 @@ include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=master-timeout]
 ------------------------------------------------------------
 GET /_xpack/usage
 ------------------------------------------------------------
+// TEST[s/usage/usage?filter_path=-watcher.execution.actions.logging*/]
+// This response filter removes watcher logging results if they are included
+// to avoid errors in the CI builds.
 
 [source,console-result]
 ------------------------------------------------------------
@@ -68,10 +71,6 @@ GET /_xpack/usage
       "enabled" : true,
       "execution" : {
         "actions" : {
-          "logging" : {
-            "total" : 0,
-            "total_time_in_ms" : 0
-          },
           "_all" : {
             "total" : 0,
             "total_time_in_ms" : 0
@@ -373,7 +372,6 @@ GET /_xpack/usage
   }
 }
 ------------------------------------------------------------
-// TESTRESPONSE[skip:Awaits fix https://github.com/elastic/elasticsearch/issues/68101]
 // TESTRESPONSE[s/"detectors" : \{[^\}]*\},/"detectors" : $body.$_path,/]
 // TESTRESPONSE[s/"model_size" : \{[^\}]*\},/"model_size" : $body.$_path,/]
 // TESTRESPONSE[s/"eql" : \{[^\}]*\},/"eql" : $body.$_path,/]
@@ -382,7 +380,7 @@ GET /_xpack/usage
 // TESTRESPONSE[s/ : true/ : $body.$_path/]
 // TESTRESPONSE[s/ : false/ : $body.$_path/]
 // TESTRESPONSE[s/ : (\-)?[0-9]+/ : $body.$_path/]
-// TESTRESPONSE[s/"total" : 0/"total" : $body.watcher.execution.actions.logging.total/]
+
 // These replacements do a few things:
 // 1. Ignore the contents of the `policy_stats` object because we don't know all
 //    of the policies that will be in it. And because we figure folks don't need