From 3a8e880fe68d9f1498bbd820752b7af438c2c25d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Istv=C3=A1n=20Zolt=C3=A1n=20Szab=C3=B3?= Date: Thu, 2 Apr 2020 08:44:35 +0200 Subject: [PATCH] [DOCS] Adds snippet comparing two indices to the painless examples (#54563) --- .../transform/painless-examples.asciidoc | 78 ++++++++++++++++++- 1 file changed, 75 insertions(+), 3 deletions(-) diff --git a/docs/reference/transform/painless-examples.asciidoc b/docs/reference/transform/painless-examples.asciidoc index f31f45ffa9a..a0e3cf87351 100644 --- a/docs/reference/transform/painless-examples.asciidoc +++ b/docs/reference/transform/painless-examples.asciidoc @@ -14,6 +14,8 @@ more about the Painless scripting language in the * <> * <> * <> +* <> +* <> NOTE: While the context of the following examples is the {transform} use case, the Painless scripts in the snippets below can be used in other {es} search @@ -59,8 +61,8 @@ scripted metric aggregation in a {transform}, which provides a metric output. `last_doc` in the `state` object. <2> The `map_script` defines `current_date` based on the timestamp of the document, then compares `current_date` with `state.timestamp_latest`, finally -returns `state.last_doc` from the shard. By using `new HashMap(...)` we copy the -source document, this is important whenever you want to pass the full source +returns `state.last_doc` from the shard. By using `new HashMap(...)` you copy +the source document, this is important whenever you want to pass the full source object from one phase to the next. <3> The `combine_script` returns `state` from each shard. <4> The `reduce_script` iterates through the value of `s.timestamp_latest` @@ -398,4 +400,74 @@ and `other` properties, then iterates through the value of `responses` returned by each shard and assigns the different response types to the appropriate properties of the `counts` object; error responses to the error counts, success responses to the success counts, and other responses to the other counts. -Finally, returns the `counts` array with the response counts. \ No newline at end of file +Finally, returns the `counts` array with the response counts. + + +[discrete] +[[painless-compare]] +==== Comparing indices by using scripted metric aggregations + +This example shows how to compare the content of two indices by a {transform} +that uses a scripted metric aggregation. + +[source,console] +-------------------------------------------------- +POST _transform/_preview +{ + "id" : "index_compare", + "source" : { <1> + "index" : [ + "index1", + "index2" + ], + "query" : { + "match_all" : { } + } + }, + "dest" : { <2> + "index" : "compare" + }, + "pivot" : { + "group_by" : { + "unique-id" : { + "terms" : { + "field" : "" <3> + } + } + }, + "aggregations" : { + "compare" : { <4> + "scripted_metric" : { + "init_script" : "", + "map_script" : "state.doc = new HashMap(params['_source'])", <5> + "combine_script" : "return state", <6> + "reduce_script" : """ <7> + if (states.size() != 2) { + return "count_mismatch" + } + if (states.get(0).equals(states.get(1))) { + return "match" + } else { + return "mismatch" + } + """ + } + } + } + } +} +-------------------------------------------------- +// TEST[skip:setup kibana sample data] + +<1> The indices referenced in the `source` object are compared to each other. +<2> The `dest` index contains the results of the comparison. +<3> The `group_by` field needs to be a unique identifier for each document. +<4> Object of the `scripted_metric` aggregation. +<5> The `map_script` defines `doc` in the state object. By using +`new HashMap(...)` you copy the source document, this is important whenever you +want to pass the full source object from one phase to the next. +<6> The `combine_script` returns `state` from each shard. +<7> The `reduce_script` checks if the size of the indices are equal. If they are +not equal, than it reports back a `count_mismatch`. Then it iterates through all +the values of the two indices and compare them. If the values are equal, then it +returns a `match`, otherwise returns a `mismatch`. \ No newline at end of file