|
@@ -10,16 +10,34 @@ match a query:
|
|
|
|
|
|
[source,js]
|
|
|
--------------------------------------------------
|
|
|
+POST /sales/_search?size=0
|
|
|
{
|
|
|
"aggs" : {
|
|
|
- "author_count" : {
|
|
|
+ "type_count" : {
|
|
|
"cardinality" : {
|
|
|
- "field" : "author"
|
|
|
+ "field" : "type"
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
--------------------------------------------------
|
|
|
+// CONSOLE
|
|
|
+// TEST[setup:sales]
|
|
|
+
|
|
|
+Response:
|
|
|
+
|
|
|
+[source,js]
|
|
|
+--------------------------------------------------
|
|
|
+{
|
|
|
+ ...
|
|
|
+ "aggregations" : {
|
|
|
+ "type_count" : {
|
|
|
+ "value" : 3
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+--------------------------------------------------
|
|
|
+// TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
|
|
|
|
|
|
==== Precision control
|
|
|
|
|
@@ -29,17 +47,20 @@ experimental[The `precision_threshold` option is specific to the current interna
|
|
|
|
|
|
[source,js]
|
|
|
--------------------------------------------------
|
|
|
+POST /sales/_search?size=0
|
|
|
{
|
|
|
"aggs" : {
|
|
|
- "author_count" : {
|
|
|
+ "type_count" : {
|
|
|
"cardinality" : {
|
|
|
- "field" : "author_hash",
|
|
|
+ "field" : "type",
|
|
|
"precision_threshold": 100 <1>
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
--------------------------------------------------
|
|
|
+// CONSOLE
|
|
|
+// TEST[setup:sales]
|
|
|
|
|
|
<1> The `precision_threshold` options allows to trade memory for accuracy, and
|
|
|
defines a unique count below which counts are expected to be close to
|
|
@@ -159,33 +180,37 @@ however since hashes need to be computed on the fly.
|
|
|
|
|
|
[source,js]
|
|
|
--------------------------------------------------
|
|
|
+POST /sales/_search?size=0
|
|
|
{
|
|
|
"aggs" : {
|
|
|
- "author_count" : {
|
|
|
+ "type_promoted_count" : {
|
|
|
"cardinality" : {
|
|
|
"script": {
|
|
|
"lang": "painless",
|
|
|
- "inline": "doc['author.first_name'].value + ' ' + doc['author.last_name'].value"
|
|
|
+ "inline": "doc['type'].value + ' ' + doc['promoted'].value"
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
--------------------------------------------------
|
|
|
+// CONSOLE
|
|
|
+// TEST[setup:sales]
|
|
|
|
|
|
This will interpret the `script` parameter as an `inline` script with the `painless` script language and no script parameters. To use a file script use the following syntax:
|
|
|
|
|
|
[source,js]
|
|
|
--------------------------------------------------
|
|
|
+POST /sales/_search?size=0
|
|
|
{
|
|
|
"aggs" : {
|
|
|
- "author_count" : {
|
|
|
+ "type_promoted_count" : {
|
|
|
"cardinality" : {
|
|
|
"script" : {
|
|
|
"file": "my_script",
|
|
|
"params": {
|
|
|
- "first_name_field": "author.first_name",
|
|
|
- "last_name_field": "author.last_name"
|
|
|
+ "type_field": "type",
|
|
|
+ "promoted_field": "promoted"
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -193,6 +218,8 @@ This will interpret the `script` parameter as an `inline` script with the `painl
|
|
|
}
|
|
|
}
|
|
|
--------------------------------------------------
|
|
|
+// CONSOLE
|
|
|
+// TEST[skip:no script]
|
|
|
|
|
|
TIP: for indexed scripts replace the `file` parameter with an `id` parameter.
|
|
|
|
|
@@ -204,6 +231,7 @@ had a value.
|
|
|
|
|
|
[source,js]
|
|
|
--------------------------------------------------
|
|
|
+POST /sales/_search?size=0
|
|
|
{
|
|
|
"aggs" : {
|
|
|
"tag_cardinality" : {
|
|
@@ -215,5 +243,6 @@ had a value.
|
|
|
}
|
|
|
}
|
|
|
--------------------------------------------------
|
|
|
-
|
|
|
+// CONSOLE
|
|
|
+// TEST[setup:sales]
|
|
|
<1> Documents without a value in the `tag` field will fall into the same bucket as documents that have the value `N/A`.
|