{
  "id": "prob",
  "title": "Probabilistic data types",
  "url": "https://redis.io/docs/latest/develop/clients/dotnet/nredisstack/prob/",
  "summary": "Learn how to use approximate calculations with Redis.",
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "kubernetes",
    "clients"
  ],
  "last_updated": "2026-04-29T10:21:19-05:00",
  "page_type": "content",
  "content_hash": "508c6bc5132a84cfe58f2872b7f0151215c7f74dacfecba9121555028e2c6dc2",
  "sections": [
    {
      "id": "set-operations",
      "title": "Set operations",
      "role": "content",
      "text": "Redis supports the following approximate set operations:\n\n-   [Membership](#set-membership): The\n    [Bloom filter](https://redis.io/docs/latest/develop/data-types/probabilistic/bloom-filter) and\n    [Cuckoo filter](https://redis.io/docs/latest/develop/data-types/probabilistic/cuckoo-filter)\n    data types let you track whether or not a given item is a member of a set.\n-   [Cardinality](#set-cardinality): The\n    [HyperLogLog](https://redis.io/docs/latest/develop/data-types/probabilistic/hyperloglogs)\n    data type gives you an approximate value for the number of items in a set, also\n    known as the *cardinality* of the set.\n\nThe sections below describe these operations in more detail."
    },
    {
      "id": "set-membership",
      "title": "Set membership",
      "role": "content",
      "text": "[Bloom filter](https://redis.io/docs/latest/develop/data-types/probabilistic/bloom-filter) and\n[Cuckoo filter](https://redis.io/docs/latest/develop/data-types/probabilistic/cuckoo-filter)\nobjects provide a set membership operation that lets you track whether or not a\nparticular item has been added to a set. These two types provide different\ntrade-offs for memory usage and speed, so you can select the best one for your\nuse case. Note that for both types, there is an asymmetry between presence and\nabsence of items in the set. If an item is reported as absent, then it is definitely\nabsent, but if it is reported as present, then there is a small chance it may really be\nabsent.\n\nInstead of storing strings directly, like a [set](https://redis.io/docs/latest/develop/data-types/sets),\na Bloom filter records the presence or absence of the\n[hash value](https://en.wikipedia.org/wiki/Hash_function) of a string.\nThis gives a very compact representation of the\nset's membership with a fixed memory size, regardless of how many items you\nadd. The following example adds some names to a Bloom filter representing\na list of users and checks for the presence or absence of users in the list.\nNote that you must use the `BF()` method to access the Bloom filter commands.\n\nFoundational: Use Bloom filters for memory-efficient set membership testing with false positive possibility\n\n**Difficulty:** Beginner\n\n**Available in:** C#, Go, Java (Synchronous - Jedis), PHP, Python, Rust (Asynchronous), Rust (Synchronous)\n\n##### C#\n\n[code example]\n\n##### Go\n\n[code example]\n\n##### Java (Synchronous - Jedis)\n\n[code example]\n\n##### PHP\n\n[code example]\n\n##### Python\n\n[code example]\n\n##### Rust (Asynchronous)\n\n[code example]\n\n##### Rust (Synchronous)\n\n[code example]\n\n\n\nA Cuckoo filter has similar features to a Bloom filter, but also supports\na deletion operation to remove hashes from a set, as shown in the example\nbelow. Note that you must use the `CF()` method to access the Cuckoo filter\ncommands.\n\nFoundational: Use Cuckoo filters for set membership testing with deletion support and faster lookups than Bloom filters\n\n**Difficulty:** Beginner\n\n**Available in:** C#, Go, Java (Synchronous - Jedis), PHP, Python, Rust (Asynchronous), Rust (Synchronous)\n\n##### C#\n\n[code example]\n\n##### Go\n\n[code example]\n\n##### Java (Synchronous - Jedis)\n\n[code example]\n\n##### PHP\n\n[code example]\n\n##### Python\n\n[code example]\n\n##### Rust (Asynchronous)\n\n[code example]\n\n##### Rust (Synchronous)\n\n[code example]\n\n\n\nWhich of these two data types you choose depends on your use case.\nBloom filters are generally faster than Cuckoo filters when adding new items,\nand also have better memory usage. Cuckoo filters are generally faster\nat checking membership and also support the delete operation. See the\n[Bloom filter](https://redis.io/docs/latest/develop/data-types/probabilistic/bloom-filter) and\n[Cuckoo filter](https://redis.io/docs/latest/develop/data-types/probabilistic/cuckoo-filter)\nreference pages for more information and comparison between the two types."
    },
    {
      "id": "set-cardinality",
      "title": "Set cardinality",
      "role": "content",
      "text": "A [HyperLogLog](https://redis.io/docs/latest/develop/data-types/probabilistic/hyperloglogs)\nobject calculates the cardinality of a set. As you add\nitems, the HyperLogLog tracks the number of distinct set members but\ndoesn't let you retrieve them or query which items have been added.\nYou can also merge two or more HyperLogLogs to find the cardinality of the\n[union](https://en.wikipedia.org/wiki/Union_(set_theory)) of the sets they\nrepresent.\n\nFoundational: Estimate set cardinality with HyperLogLog for memory-efficient counting of distinct items\n\n**Difficulty:** Beginner\n\n**Available in:** C#, Go, Java (Synchronous - Jedis), PHP, Python, Rust (Asynchronous), Rust (Synchronous)\n\n##### C#\n\n[code example]\n\n##### Go\n\n[code example]\n\n##### Java (Synchronous - Jedis)\n\n[code example]\n\n##### PHP\n\n[code example]\n\n##### Python\n\n[code example]\n\n##### Rust (Asynchronous)\n\n[code example]\n\n##### Rust (Synchronous)\n\n[code example]\n\n\n\nThe main benefit that HyperLogLogs offer is their very low\nmemory usage. They can count up to 2^64 items with less than\n1% standard error using a maximum 12KB of memory. This makes\nthem very useful for counting things like the total of distinct\nIP addresses that access a website or the total of distinct\nbank card numbers that make purchases within a day."
    },
    {
      "id": "statistics",
      "title": "Statistics",
      "role": "content",
      "text": "Redis supports several approximate statistical calculations\non numeric data sets:\n\n-   [Frequency](#frequency): The\n    [Count-min sketch](https://redis.io/docs/latest/develop/data-types/probabilistic/count-min-sketch)\n    data type lets you find the approximate frequency of a labeled item in a data stream.\n-   [Quantiles](#quantiles): The\n    [t-digest](https://redis.io/docs/latest/develop/data-types/probabilistic/t-digest)\n    data type estimates the quantile of a query value in a data stream.\n-   [Ranking](#ranking): The\n    [Top-K](https://redis.io/docs/latest/develop/data-types/probabilistic/top-k) data type\n    estimates the ranking of labeled items by frequency in a data stream.\n\nThe sections below describe these operations in more detail."
    },
    {
      "id": "frequency",
      "title": "Frequency",
      "role": "content",
      "text": "A [Count-min sketch](https://redis.io/docs/latest/develop/data-types/probabilistic/count-min-sketch)\n(CMS) object keeps count of a set of related items represented by\nstring labels. The count is approximate, but you can specify\nhow close you want to keep the count to the true value (as a fraction)\nand the acceptable probability of failing to keep it in this\ndesired range. For example, you can request that the count should\nstay within 0.1% of the true value and have a 0.05% probability\nof going outside this limit. The example below shows how to create\na Count-min sketch object, add data to it, and then query it.\nNote that you must use the `CMS()` method to access the Count-min\nsketch commands.\n\nFoundational: Track approximate item frequencies with Count-min sketch for memory-efficient statistics on data streams\n\n**Difficulty:** Intermediate\n\n**Available in:** C#, Go, Java (Synchronous - Jedis), PHP, Python, Rust (Asynchronous), Rust (Synchronous)\n\n##### C#\n\n[code example]\n\n##### Go\n\n[code example]\n\n##### Java (Synchronous - Jedis)\n\n[code example]\n\n##### PHP\n\n[code example]\n\n##### Python\n\n[code example]\n\n##### Rust (Asynchronous)\n\n[code example]\n\n##### Rust (Synchronous)\n\n[code example]\n\n\n\nThe advantage of using a CMS over keeping an exact count with a\n[sorted set](https://redis.io/docs/latest/develop/data-types/sorted-sets)\nis that that a CMS has very low and fixed memory usage, even for\nlarge numbers of items. Use CMS objects to keep daily counts of\nitems sold, accesses to individual web pages on your site, and\nother similar statistics."
    },
    {
      "id": "quantiles",
      "title": "Quantiles",
      "role": "content",
      "text": "A [quantile](https://en.wikipedia.org/wiki/Quantile) is the value\nbelow which a certain fraction of samples lie. For example, with\na set of measurements of people's heights, the quantile of 0.75 is\nthe value of height below which 75% of all people's heights lie.\n[Percentiles](https://en.wikipedia.org/wiki/Percentile) are equivalent\nto quantiles, except that the fraction is expressed as a percentage.\n\nA [t-digest](https://redis.io/docs/latest/develop/data-types/probabilistic/t-digest)\nobject can estimate quantiles from a set of values added to it\nwithout having to store each value in the set explicitly. This can\nsave a lot of memory when you have a large number of samples.\n\nThe example below shows how to add data samples to a t-digest\nobject and obtain some basic statistics, such as the minimum and\nmaximum values, the quantile of 0.75, and the\n[cumulative distribution function](https://en.wikipedia.org/wiki/Cumulative_distribution_function)\n(CDF), which is effectively the inverse of the quantile function. It also\nshows how to merge two or more t-digest objects to query the combined\ndata set. Note that you must use the `TDIGEST()` method to access the\nt-digest commands.\n\nFoundational: Estimate quantiles and percentiles with t-digest for memory-efficient statistical analysis of large datasets\n\n**Difficulty:** Intermediate\n\n**Available in:** C#, Go, Java (Synchronous - Jedis), PHP, Python, Rust (Asynchronous), Rust (Synchronous)\n\n##### C#\n\n[code example]\n\n##### Go\n\n[code example]\n\n##### Java (Synchronous - Jedis)\n\n[code example]\n\n##### PHP\n\n[code example]\n\n##### Python\n\n[code example]\n\n##### Rust (Asynchronous)\n\n[code example]\n\n##### Rust (Synchronous)\n\n[code example]\n\n\n\nA t-digest object also supports several other related commands, such\nas querying by rank. See the\n[t-digest](https://redis.io/docs/latest/develop/data-types/probabilistic/t-digest)\nreference for more information."
    },
    {
      "id": "ranking",
      "title": "Ranking",
      "role": "content",
      "text": "A [Top-K](https://redis.io/docs/latest/develop/data-types/probabilistic/top-k)\nobject estimates the rankings of different labeled items in a data\nstream according to frequency. For example, you could use this to\ntrack the top ten most frequently-accessed pages on a website, or the\ntop five most popular items sold.\n\nThe example below adds several different items to a Top-K object\nthat tracks the top three items (this is the second parameter to\nthe `TOPK().Reserve()` method). It also shows how to list the\ntop *k* items and query whether or not a given item is in the\nlist. Note that you must use the `TOPK()` method to access the\nTop-K commands.\n\nFoundational: Track top K most frequent items in a data stream with Top-K for efficient ranking without storing all items\n\n**Difficulty:** Intermediate\n\n**Available in:** C#, Go, Java (Synchronous - Jedis), PHP, Python, Rust (Asynchronous), Rust (Synchronous)\n\n##### C#\n\n[code example]\n\n##### Go\n\n[code example]\n\n##### Java (Synchronous - Jedis)\n\n[code example]\n\n##### PHP\n\n[code example]\n\n##### Python\n\n[code example]\n\n##### Rust (Asynchronous)\n\n[code example]\n\n##### Rust (Synchronous)\n\n[code example]"
    }
  ],
  "examples": [
    {
      "id": "set-membership-ex0",
      "language": "csharp",
      "code": "bool[] res1 = db.BF().MAdd(\n            \"recorded_users\", \"andy\", \"cameron\", \"david\", \"michelle\"\n        );\n        Console.WriteLine(string.Join(\", \", res1));\n        // >>> true, true, true, true\n\n        bool res2 = db.BF().Exists(\"recorded_users\", \"cameron\");\n        Console.WriteLine(res2); // >>> true\n\n        bool res3 = db.BF().Exists(\"recorded_users\", \"kaitlyn\");\n        Console.WriteLine(res3); // >>> false",
      "section_id": "set-membership"
    },
    {
      "id": "set-membership-ex1",
      "language": "go",
      "code": "res1, err := rdb.BFMAdd(\n\t\tctx,\n\t\t\"recorded_users\",\n\t\t\"andy\", \"cameron\", \"david\", \"michelle\",\n\t).Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res1) // >>> [true true true true]\n\n\tres2, err := rdb.BFExists(ctx,\n\t\t\"recorded_users\", \"cameron\",\n\t).Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res2) // >>> true\n\n\tres3, err := rdb.BFExists(ctx, \"recorded_users\", \"kaitlyn\").Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res3) // >>> false",
      "section_id": "set-membership"
    },
    {
      "id": "set-membership-ex2",
      "language": "java",
      "code": "List<Boolean> res1 = jedis.bfMAdd(\n            \"recorded_users\",\n            \"andy\", \"cameron\", \"david\", \"michelle\"\n        );\n        System.out.println(res1);  // >>> [true, true, true, true]\n\n        boolean res2 = jedis.bfExists(\"recorded_users\", \"cameron\");\n        System.out.println(res2);  // >>> true\n\n        boolean res3 = jedis.bfExists(\"recorded_users\", \"kaitlyn\");\n        System.out.println(res3);  // >>> false",
      "section_id": "set-membership"
    },
    {
      "id": "set-membership-ex3",
      "language": "php",
      "code": "$r->del('recorded_users');\n$r->bfreserve('recorded_users', 0.01, 1000);\n\n$added = $r->bfmadd('recorded_users', 'andy', 'cameron', 'david', 'michelle');\necho json_encode($added), PHP_EOL;\n// >>> [1,1,1,1]\n\n$known = $r->bfexists('recorded_users', 'cameron');\necho $known, PHP_EOL;\n// >>> 1\n\n$unknown = $r->bfexists('recorded_users', 'kaitlyn');\necho $unknown, PHP_EOL;\n// >>> 0",
      "section_id": "set-membership"
    },
    {
      "id": "set-membership-ex4",
      "language": "python",
      "code": "res1 = r.bf().madd(\"recorded_users\", \"andy\", \"cameron\", \"david\", \"michelle\")\nprint(res1)  # >>> [1, 1, 1, 1]\n\nres2 = r.bf().exists(\"recorded_users\", \"cameron\")\nprint(res2)  # >>> 1\n\nres3 = r.bf().exists(\"recorded_users\", \"kaitlyn\")\nprint(res3)  # >>> 0",
      "section_id": "set-membership"
    },
    {
      "id": "set-membership-ex5",
      "language": "rust",
      "code": "mod home_prob_dts_tests {\n    use redis::AsyncCommands;\n\n    async fn run() {\n        let mut r = match redis::Client::open(\"redis://127.0.0.1\") {\n            Ok(client) => match client.get_multiplexed_async_connection().await {\n                Ok(conn) => conn,\n                Err(e) => {\n                    println!(\"Failed to connect to Redis: {e}\");\n                    return;\n                }\n            },\n            Err(e) => {\n                println!(\"Failed to create Redis client: {e}\");\n                return;\n            }\n        };\n\n        let group1_added: bool = r\n            .pfadd(\"group:1\", &[\"andy\", \"cameron\", \"david\"])\n            .await\n            .expect(\"Failed to add items to group:1\");\n        println!(\"{group1_added}\"); // >>> true\n\n        let group1: usize = r.pfcount(\"group:1\").await.expect(\"Failed to count group:1\");\n        println!(\"{group1}\"); // >>> 3\n\n        let group2_added: bool = r\n            .pfadd(\"group:2\", &[\"kaitlyn\", \"michelle\", \"paolo\", \"rachel\"])\n            .await\n            .expect(\"Failed to add items to group:2\");\n        println!(\"{group2_added}\"); // >>> true\n\n        let group2: usize = r.pfcount(\"group:2\").await.expect(\"Failed to count group:2\");\n        println!(\"{group2}\"); // >>> 4\n\n        let _: () = r\n            .pfmerge(\"both_groups\", &[\"group:1\", \"group:2\"])\n            .await\n            .expect(\"Failed to merge HyperLogLogs\");\n        println!(\"OK\"); // >>> OK\n\n        let both_groups: usize = r\n            .pfcount(\"both_groups\")\n            .await\n            .expect(\"Failed to count both_groups\");\n        println!(\"{both_groups}\"); // >>> 7\n\n    }\n}",
      "section_id": "set-membership"
    },
    {
      "id": "set-membership-ex6",
      "language": "rust",
      "code": "mod home_prob_dts_tests {\n    use redis::Commands;\n\n    fn run() {\n        let mut r = match redis::Client::open(\"redis://127.0.0.1\") {\n            Ok(client) => match client.get_connection() {\n                Ok(conn) => conn,\n                Err(e) => {\n                    println!(\"Failed to connect to Redis: {e}\");\n                    return;\n                }\n            },\n            Err(e) => {\n                println!(\"Failed to create Redis client: {e}\");\n                return;\n            }\n        };\n\n        let group1_added: bool = r\n            .pfadd(\"group:1\", &[\"andy\", \"cameron\", \"david\"])\n            .expect(\"Failed to add items to group:1\");\n        println!(\"{group1_added}\"); // >>> true\n\n        let group1: usize = r.pfcount(\"group:1\").expect(\"Failed to count group:1\");\n        println!(\"{group1}\"); // >>> 3\n\n        let group2_added: bool = r\n            .pfadd(\"group:2\", &[\"kaitlyn\", \"michelle\", \"paolo\", \"rachel\"])\n            .expect(\"Failed to add items to group:2\");\n        println!(\"{group2_added}\"); // >>> true\n\n        let group2: usize = r.pfcount(\"group:2\").expect(\"Failed to count group:2\");\n        println!(\"{group2}\"); // >>> 4\n\n        let _: () = r\n            .pfmerge(\"both_groups\", &[\"group:1\", \"group:2\"])\n            .expect(\"Failed to merge HyperLogLogs\");\n        println!(\"OK\"); // >>> OK\n\n        let both_groups: usize = r\n            .pfcount(\"both_groups\")\n            .expect(\"Failed to count both_groups\");\n        println!(\"{both_groups}\"); // >>> 7\n\n    }\n}",
      "section_id": "set-membership"
    },
    {
      "id": "set-membership-ex7",
      "language": "csharp",
      "code": "bool res4 = db.CF().Add(\"other_users\", \"paolo\");\n        Console.WriteLine(res4); // >>> true\n\n        bool res5 = db.CF().Add(\"other_users\", \"kaitlyn\");\n        Console.WriteLine(res5); // >>> true\n\n        bool res6 = db.CF().Add(\"other_users\", \"rachel\");\n        Console.WriteLine(res6); // >>> true\n\n        bool[] res7 = db.CF().MExists(\"other_users\", \"paolo\", \"rachel\", \"andy\");\n        Console.WriteLine(string.Join(\", \", res7));\n        // >>> true, true, false\n\n        bool res8 = db.CF().Del(\"other_users\", \"paolo\");\n        Console.WriteLine(res8); // >>> true\n\n        bool res9 = db.CF().Exists(\"other_users\", \"paolo\");\n        Console.WriteLine(res9); // >>> false",
      "section_id": "set-membership"
    },
    {
      "id": "set-membership-ex8",
      "language": "go",
      "code": "res4, err := rdb.CFAdd(ctx, \"other_users\", \"paolo\").Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res4) // >>> true\n\n\tres5, err := rdb.CFAdd(ctx, \"other_users\", \"kaitlyn\").Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res5) // >>> true\n\n\tres6, err := rdb.CFAdd(ctx, \"other_users\", \"rachel\").Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res6) // >>> true\n\n\tres7, err := rdb.CFMExists(ctx,\n\t\t\"other_users\", \"paolo\", \"rachel\", \"andy\",\n\t).Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res7) // >>> [true true false]\n\n\tres8, err := rdb.CFDel(ctx, \"other_users\", \"paolo\").Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res8) // >>> true\n\n\tres9, err := rdb.CFExists(ctx, \"other_users\", \"paolo\").Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res9) // >>> false",
      "section_id": "set-membership"
    },
    {
      "id": "set-membership-ex9",
      "language": "java",
      "code": "boolean res4 = jedis.cfAdd(\"other_users\", \"paolo\");\n        System.out.println(res4);  // >>> true\n\n        boolean res5 = jedis.cfAdd(\"other_users\", \"kaitlyn\");\n        System.out.println(res5);  // >>> true\n\n        boolean res6 = jedis.cfAdd(\"other_users\", \"rachel\");\n        System.out.println(res6);  // >>> true\n\n        List<Boolean> res7 = jedis.cfMExists(\n            \"other_users\",\n            \"paolo\", \"rachel\", \"andy\"\n        );\n        System.out.println(res7);  // >>> [true, true, false]\n\n        boolean res8 = jedis.cfDel(\"other_users\", \"paolo\");\n        System.out.println(res8);  // >>> true\n\n        boolean res9 = jedis.cfExists(\"other_users\", \"paolo\");\n        System.out.println(res9);  // >>> false",
      "section_id": "set-membership"
    },
    {
      "id": "set-membership-ex10",
      "language": "php",
      "code": "$r->del('other_users');\n$r->cfreserve('other_users', 1000);\n\n$r->cfadd('other_users', 'paolo');\n$r->cfadd('other_users', 'kaitlyn');\n$r->cfadd('other_users', 'rachel');\n\n$beforeDelete = [\n    $r->cfexists('other_users', 'paolo'),\n    $r->cfexists('other_users', 'kaitlyn'),\n    $r->cfexists('other_users', 'rachel'),\n    $r->cfexists('other_users', 'andy'),\n];\necho json_encode($beforeDelete), PHP_EOL;\n// >>> [1,1,1,0]\n\n$r->cfdel('other_users', 'paolo');\n$afterDelete = $r->cfexists('other_users', 'paolo');\necho $afterDelete, PHP_EOL;\n// >>> 0",
      "section_id": "set-membership"
    },
    {
      "id": "set-membership-ex11",
      "language": "python",
      "code": "res4 = r.cf().add(\"other_users\", \"paolo\")\nprint(res4)  # >>> 1\n\nres5 = r.cf().add(\"other_users\", \"kaitlyn\")\nprint(res5)  # >>> 1\n\nres6 = r.cf().add(\"other_users\", \"rachel\")\nprint(res6)  # >>> 1\n\nres7 = r.cf().mexists(\"other_users\", \"paolo\", \"rachel\", \"andy\")\nprint(res7)  # >>> [1, 1, 0]\n\nres8 = r.cf().delete(\"other_users\", \"paolo\")\nprint(res8)  # >>> 1\n\nres9 = r.cf().exists(\"other_users\", \"paolo\")\nprint(res9)  # >>> 0",
      "section_id": "set-membership"
    },
    {
      "id": "set-membership-ex12",
      "language": "rust",
      "code": "mod home_prob_dts_tests {\n    use redis::AsyncCommands;\n\n    async fn run() {\n        let mut r = match redis::Client::open(\"redis://127.0.0.1\") {\n            Ok(client) => match client.get_multiplexed_async_connection().await {\n                Ok(conn) => conn,\n                Err(e) => {\n                    println!(\"Failed to connect to Redis: {e}\");\n                    return;\n                }\n            },\n            Err(e) => {\n                println!(\"Failed to create Redis client: {e}\");\n                return;\n            }\n        };\n\n        let group1_added: bool = r\n            .pfadd(\"group:1\", &[\"andy\", \"cameron\", \"david\"])\n            .await\n            .expect(\"Failed to add items to group:1\");\n        println!(\"{group1_added}\"); // >>> true\n\n        let group1: usize = r.pfcount(\"group:1\").await.expect(\"Failed to count group:1\");\n        println!(\"{group1}\"); // >>> 3\n\n        let group2_added: bool = r\n            .pfadd(\"group:2\", &[\"kaitlyn\", \"michelle\", \"paolo\", \"rachel\"])\n            .await\n            .expect(\"Failed to add items to group:2\");\n        println!(\"{group2_added}\"); // >>> true\n\n        let group2: usize = r.pfcount(\"group:2\").await.expect(\"Failed to count group:2\");\n        println!(\"{group2}\"); // >>> 4\n\n        let _: () = r\n            .pfmerge(\"both_groups\", &[\"group:1\", \"group:2\"])\n            .await\n            .expect(\"Failed to merge HyperLogLogs\");\n        println!(\"OK\"); // >>> OK\n\n        let both_groups: usize = r\n            .pfcount(\"both_groups\")\n            .await\n            .expect(\"Failed to count both_groups\");\n        println!(\"{both_groups}\"); // >>> 7\n\n    }\n}",
      "section_id": "set-membership"
    },
    {
      "id": "set-membership-ex13",
      "language": "rust",
      "code": "mod home_prob_dts_tests {\n    use redis::Commands;\n\n    fn run() {\n        let mut r = match redis::Client::open(\"redis://127.0.0.1\") {\n            Ok(client) => match client.get_connection() {\n                Ok(conn) => conn,\n                Err(e) => {\n                    println!(\"Failed to connect to Redis: {e}\");\n                    return;\n                }\n            },\n            Err(e) => {\n                println!(\"Failed to create Redis client: {e}\");\n                return;\n            }\n        };\n\n        let group1_added: bool = r\n            .pfadd(\"group:1\", &[\"andy\", \"cameron\", \"david\"])\n            .expect(\"Failed to add items to group:1\");\n        println!(\"{group1_added}\"); // >>> true\n\n        let group1: usize = r.pfcount(\"group:1\").expect(\"Failed to count group:1\");\n        println!(\"{group1}\"); // >>> 3\n\n        let group2_added: bool = r\n            .pfadd(\"group:2\", &[\"kaitlyn\", \"michelle\", \"paolo\", \"rachel\"])\n            .expect(\"Failed to add items to group:2\");\n        println!(\"{group2_added}\"); // >>> true\n\n        let group2: usize = r.pfcount(\"group:2\").expect(\"Failed to count group:2\");\n        println!(\"{group2}\"); // >>> 4\n\n        let _: () = r\n            .pfmerge(\"both_groups\", &[\"group:1\", \"group:2\"])\n            .expect(\"Failed to merge HyperLogLogs\");\n        println!(\"OK\"); // >>> OK\n\n        let both_groups: usize = r\n            .pfcount(\"both_groups\")\n            .expect(\"Failed to count both_groups\");\n        println!(\"{both_groups}\"); // >>> 7\n\n    }\n}",
      "section_id": "set-membership"
    },
    {
      "id": "set-cardinality-ex0",
      "language": "csharp",
      "code": "bool res10 = db.HyperLogLogAdd(\n            \"group:1\",\n            [\"andy\", \"cameron\", \"david\"]\n        );\n        Console.WriteLine(res10); // >>> true\n\n        long res11 = db.HyperLogLogLength(\"group:1\");\n        Console.WriteLine(res11); // >>> 3\n\n        bool res12 = db.HyperLogLogAdd(\n            \"group:2\",\n            [\"kaitlyn\", \"michelle\", \"paolo\", \"rachel\"]\n        );\n        Console.WriteLine(res12); // >>> true\n\n        long res13 = db.HyperLogLogLength(\"group:2\");\n        Console.WriteLine(res13); // >>> 4\n\n        db.HyperLogLogMerge(\n            \"both_groups\",\n            \"group:1\", \"group:2\"\n        );\n\n        long res14 = db.HyperLogLogLength(\"both_groups\");\n        Console.WriteLine(res14); // >>> 7",
      "section_id": "set-cardinality"
    },
    {
      "id": "set-cardinality-ex1",
      "language": "go",
      "code": "res10, err := rdb.PFAdd(\n\t\tctx,\n\t\t\"group:1\",\n\t\t\"andy\", \"cameron\", \"david\",\n\t).Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res10) // >>> 1\n\n\tres11, err := rdb.PFCount(ctx, \"group:1\").Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res11) // >>> 3\n\n\tres12, err := rdb.PFAdd(ctx,\n\t\t\"group:2\",\n\t\t\"kaitlyn\", \"michelle\", \"paolo\", \"rachel\",\n\t).Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res12) // >>> 1\n\n\tres13, err := rdb.PFCount(ctx, \"group:2\").Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res13) // >>> 4\n\n\tres14, err := rdb.PFMerge(\n\t\tctx,\n\t\t\"both_groups\",\n\t\t\"group:1\", \"group:2\",\n\t).Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res14) // >>> OK\n\n\tres15, err := rdb.PFCount(ctx, \"both_groups\").Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res15) // >>> 7",
      "section_id": "set-cardinality"
    },
    {
      "id": "set-cardinality-ex2",
      "language": "java",
      "code": "long res10 = jedis.pfadd(\"group:1\", \"andy\", \"cameron\", \"david\");\n        System.out.println(res10);  // >>> 1\n\n        long res11 = jedis.pfcount(\"group:1\");\n        System.out.println(res11);  // >>> 3\n\n        long res12 = jedis.pfadd(\n            \"group:2\",\n            \"kaitlyn\", \"michelle\", \"paolo\", \"rachel\"\n        );\n        System.out.println(res12);  // >>> 1\n\n        long res13 = jedis.pfcount(\"group:2\");\n        System.out.println(res13);  // >>> 4\n\n        String res14 = jedis.pfmerge(\"both_groups\", \"group:1\", \"group:2\");\n        System.out.println(res14);  // >>> OK\n\n        long res15 = jedis.pfcount(\"both_groups\");\n        System.out.println(res15);  // >>> 7",
      "section_id": "set-cardinality"
    },
    {
      "id": "set-cardinality-ex3",
      "language": "php",
      "code": "$r->del('group:1', 'group:2', 'both_groups');\n\n$r->pfadd('group:1', ['andy', 'cameron', 'david']);\n$group1 = $r->pfcount('group:1');\necho $group1, PHP_EOL;\n// >>> 3\n\n$r->pfadd('group:2', ['kaitlyn', 'michelle', 'paolo', 'rachel']);\n$group2 = $r->pfcount('group:2');\necho $group2, PHP_EOL;\n// >>> 4\n\n$r->pfmerge('both_groups', 'group:1', 'group:2');\n$bothGroups = $r->pfcount('both_groups');\necho $bothGroups, PHP_EOL;\n// >>> 7",
      "section_id": "set-cardinality"
    },
    {
      "id": "set-cardinality-ex4",
      "language": "python",
      "code": "res10 = r.pfadd(\"group:1\", \"andy\", \"cameron\", \"david\")\nprint(res10)  # >>> 1\n\nres11 = r.pfcount(\"group:1\")\nprint(res11)  # >>> 3\n\nres12 = r.pfadd(\"group:2\", \"kaitlyn\", \"michelle\", \"paolo\", \"rachel\")\nprint(res12)  # >>> 1\n\nres13 = r.pfcount(\"group:2\")\nprint(res13)  # >>> 4\n\nres14 = r.pfmerge(\"both_groups\", \"group:1\", \"group:2\")\nprint(res14)  # >>> True\n\nres15 = r.pfcount(\"both_groups\")\nprint(res15)  # >>> 7",
      "section_id": "set-cardinality"
    },
    {
      "id": "set-cardinality-ex5",
      "language": "rust",
      "code": "let group1_added: bool = r\n            .pfadd(\"group:1\", &[\"andy\", \"cameron\", \"david\"])\n            .await\n            .expect(\"Failed to add items to group:1\");\n        println!(\"{group1_added}\"); // >>> true\n\n        let group1: usize = r.pfcount(\"group:1\").await.expect(\"Failed to count group:1\");\n        println!(\"{group1}\"); // >>> 3\n\n        let group2_added: bool = r\n            .pfadd(\"group:2\", &[\"kaitlyn\", \"michelle\", \"paolo\", \"rachel\"])\n            .await\n            .expect(\"Failed to add items to group:2\");\n        println!(\"{group2_added}\"); // >>> true\n\n        let group2: usize = r.pfcount(\"group:2\").await.expect(\"Failed to count group:2\");\n        println!(\"{group2}\"); // >>> 4\n\n        let _: () = r\n            .pfmerge(\"both_groups\", &[\"group:1\", \"group:2\"])\n            .await\n            .expect(\"Failed to merge HyperLogLogs\");\n        println!(\"OK\"); // >>> OK\n\n        let both_groups: usize = r\n            .pfcount(\"both_groups\")\n            .await\n            .expect(\"Failed to count both_groups\");\n        println!(\"{both_groups}\"); // >>> 7",
      "section_id": "set-cardinality"
    },
    {
      "id": "set-cardinality-ex6",
      "language": "rust",
      "code": "let group1_added: bool = r\n            .pfadd(\"group:1\", &[\"andy\", \"cameron\", \"david\"])\n            .expect(\"Failed to add items to group:1\");\n        println!(\"{group1_added}\"); // >>> true\n\n        let group1: usize = r.pfcount(\"group:1\").expect(\"Failed to count group:1\");\n        println!(\"{group1}\"); // >>> 3\n\n        let group2_added: bool = r\n            .pfadd(\"group:2\", &[\"kaitlyn\", \"michelle\", \"paolo\", \"rachel\"])\n            .expect(\"Failed to add items to group:2\");\n        println!(\"{group2_added}\"); // >>> true\n\n        let group2: usize = r.pfcount(\"group:2\").expect(\"Failed to count group:2\");\n        println!(\"{group2}\"); // >>> 4\n\n        let _: () = r\n            .pfmerge(\"both_groups\", &[\"group:1\", \"group:2\"])\n            .expect(\"Failed to merge HyperLogLogs\");\n        println!(\"OK\"); // >>> OK\n\n        let both_groups: usize = r\n            .pfcount(\"both_groups\")\n            .expect(\"Failed to count both_groups\");\n        println!(\"{both_groups}\"); // >>> 7",
      "section_id": "set-cardinality"
    },
    {
      "id": "frequency-ex0",
      "language": "csharp",
      "code": "// Specify that you want to keep the counts within 0.01\n        // (0.1%) of the true value with a 0.005 (0.05%) chance\n        // of going outside this limit.\n        bool res15 = db.CMS().InitByProb(\"items_sold\", 0.01, 0.005);\n        Console.WriteLine(res15); // >>> true\n\n        long[] res16 = db.CMS().IncrBy(\n            \"items_sold\",\n            [\n                new(\"bread\", 300),\n                new(\"tea\", 200),\n                new(\"coffee\", 200),\n                new(\"beer\", 100)\n            ]\n        );\n        Console.WriteLine(string.Join(\", \", res16));\n        // >>> 300, 200, 200, 100\n\n        long[] res17 = db.CMS().IncrBy(\n            \"items_sold\",\n            [\n                new(\"bread\", 100),\n                new(\"coffee\", 150)\n            ]\n        );\n        Console.WriteLine(string.Join(\", \", res17));\n        // >>> 400, 350\n\n        long[] res18 = db.CMS().Query(\n            \"items_sold\",\n            \"bread\", \"tea\", \"coffee\", \"beer\"\n        );\n        Console.WriteLine(string.Join(\", \", res18));\n        // >>> 400, 200, 350, 100",
      "section_id": "frequency"
    },
    {
      "id": "frequency-ex1",
      "language": "go",
      "code": "// Specify that you want to keep the counts within 0.01\n\t// (0.1%) of the true value with a 0.005 (0.05%) chance\n\t// of going outside this limit.\n\tres16, err := rdb.CMSInitByProb(ctx, \"items_sold\", 0.01, 0.005).Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res16) // >>> OK\n\n\t// The parameters for `CMSIncrBy()` are two lists. The count\n\t// for each item in the first list is incremented by the\n\t// value at the same index in the second list.\n\tres17, err := rdb.CMSIncrBy(ctx, \"items_sold\",\n\t\t\"bread\", 300,\n\t\t\"tea\", 200,\n\t\t\"coffee\", 200,\n\t\t\"beer\", 100,\n\t).Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res17) // >>> [300 200 200 100]\n\n\tres18, err := rdb.CMSIncrBy(ctx, \"items_sold\",\n\t\t\"bread\", 100,\n\t\t\"coffee\", 150,\n\t).Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res18) // >>> [400 350]\n\n\tres19, err := rdb.CMSQuery(ctx,\n\t\t\"items_sold\",\n\t\t\"bread\", \"tea\", \"coffee\", \"beer\",\n\t).Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res19) // >>> [400 200 350 100]",
      "section_id": "frequency"
    },
    {
      "id": "frequency-ex2",
      "language": "java",
      "code": "// Specify that you want to keep the counts within 0.01\n        // (0.1%) of the true value with a 0.005 (0.05%) chance\n        // of going outside this limit.\n        String res16 = jedis.cmsInitByProb(\"items_sold\", 0.01, 0.005);\n        System.out.println(res16);  // >>> OK\n\n        Map<String, Long> firstItemIncrements = new HashMap<>();\n        firstItemIncrements.put(\"bread\", 300L);\n        firstItemIncrements.put(\"tea\", 200L);\n        firstItemIncrements.put(\"coffee\", 200L);\n        firstItemIncrements.put(\"beer\", 100L);\n\n        List<Long> res17 = jedis.cmsIncrBy(\"items_sold\",\n            firstItemIncrements\n        );\n        res17.sort(null);\n        System.out.println();  // >>> [100, 200, 200, 300]\n\n        Map<String, Long> secondItemIncrements = new HashMap<>();\n        secondItemIncrements.put(\"bread\", 100L);\n        secondItemIncrements.put(\"coffee\", 150L);\n\n        List<Long> res18 = jedis.cmsIncrBy(\"items_sold\",\n            secondItemIncrements\n        );\n        res18.sort(null);\n        System.out.println(res18);  // >>> [350, 400]\n\n        List<Long> res19 = jedis.cmsQuery(\n            \"items_sold\",\n            \"bread\", \"tea\", \"coffee\", \"beer\"\n        );\n        res19.sort(null);\n        System.out.println(res19);  // >>> [100, 200, 350, 400]",
      "section_id": "frequency"
    },
    {
      "id": "frequency-ex3",
      "language": "php",
      "code": "$r->del('items_sold');\n$r->cmsinitbyprob('items_sold', 0.01, 0.005);\n\n$firstCounts = $r->cmsincrby(\n    'items_sold',\n    'bread', 300,\n    'tea', 200,\n    'coffee', 200,\n    'beer', 100\n);\necho json_encode($firstCounts), PHP_EOL;\n// >>> [300,200,200,100]\n\n$secondCounts = $r->cmsincrby(\n    'items_sold',\n    'bread', 100,\n    'coffee', 150\n);\necho json_encode($secondCounts), PHP_EOL;\n// >>> [400,350]\n\n$queriedCounts = $r->cmsquery('items_sold', 'bread', 'tea', 'coffee', 'beer');\necho json_encode($queriedCounts), PHP_EOL;\n// >>> [400,200,350,100]",
      "section_id": "frequency"
    },
    {
      "id": "frequency-ex4",
      "language": "python",
      "code": "# Specify that you want to keep the counts within 0.01\n# (1%) of the true value with a 0.005 (0.5%) chance\n# of going outside this limit.\nres16 = r.cms().initbyprob(\"items_sold\", 0.01, 0.005)\nprint(res16)  # >>> True\n\n# The parameters for `incrby()` are two lists. The count\n# for each item in the first list is incremented by the\n# value at the same index in the second list.\nres17 = r.cms().incrby(\n    \"items_sold\",\n    [\"bread\", \"tea\", \"coffee\", \"beer\"],  # Items sold\n    [300, 200, 200, 100]\n)\nprint(res17)  # >>> [300, 200, 200, 100]\n\nres18 = r.cms().incrby(\n    \"items_sold\",\n    [\"bread\", \"coffee\"],\n    [100, 150]\n)\nprint(res18)  # >>> [400, 350]\n\nres19 = r.cms().query(\"items_sold\", \"bread\", \"tea\", \"coffee\", \"beer\")\nprint(res19)  # >>> [400, 200, 350, 100]",
      "section_id": "frequency"
    },
    {
      "id": "frequency-ex5",
      "language": "rust",
      "code": "mod home_prob_dts_tests {\n    use redis::AsyncCommands;\n\n    async fn run() {\n        let mut r = match redis::Client::open(\"redis://127.0.0.1\") {\n            Ok(client) => match client.get_multiplexed_async_connection().await {\n                Ok(conn) => conn,\n                Err(e) => {\n                    println!(\"Failed to connect to Redis: {e}\");\n                    return;\n                }\n            },\n            Err(e) => {\n                println!(\"Failed to create Redis client: {e}\");\n                return;\n            }\n        };\n\n        let group1_added: bool = r\n            .pfadd(\"group:1\", &[\"andy\", \"cameron\", \"david\"])\n            .await\n            .expect(\"Failed to add items to group:1\");\n        println!(\"{group1_added}\"); // >>> true\n\n        let group1: usize = r.pfcount(\"group:1\").await.expect(\"Failed to count group:1\");\n        println!(\"{group1}\"); // >>> 3\n\n        let group2_added: bool = r\n            .pfadd(\"group:2\", &[\"kaitlyn\", \"michelle\", \"paolo\", \"rachel\"])\n            .await\n            .expect(\"Failed to add items to group:2\");\n        println!(\"{group2_added}\"); // >>> true\n\n        let group2: usize = r.pfcount(\"group:2\").await.expect(\"Failed to count group:2\");\n        println!(\"{group2}\"); // >>> 4\n\n        let _: () = r\n            .pfmerge(\"both_groups\", &[\"group:1\", \"group:2\"])\n            .await\n            .expect(\"Failed to merge HyperLogLogs\");\n        println!(\"OK\"); // >>> OK\n\n        let both_groups: usize = r\n            .pfcount(\"both_groups\")\n            .await\n            .expect(\"Failed to count both_groups\");\n        println!(\"{both_groups}\"); // >>> 7\n\n    }\n}",
      "section_id": "frequency"
    },
    {
      "id": "frequency-ex6",
      "language": "rust",
      "code": "mod home_prob_dts_tests {\n    use redis::Commands;\n\n    fn run() {\n        let mut r = match redis::Client::open(\"redis://127.0.0.1\") {\n            Ok(client) => match client.get_connection() {\n                Ok(conn) => conn,\n                Err(e) => {\n                    println!(\"Failed to connect to Redis: {e}\");\n                    return;\n                }\n            },\n            Err(e) => {\n                println!(\"Failed to create Redis client: {e}\");\n                return;\n            }\n        };\n\n        let group1_added: bool = r\n            .pfadd(\"group:1\", &[\"andy\", \"cameron\", \"david\"])\n            .expect(\"Failed to add items to group:1\");\n        println!(\"{group1_added}\"); // >>> true\n\n        let group1: usize = r.pfcount(\"group:1\").expect(\"Failed to count group:1\");\n        println!(\"{group1}\"); // >>> 3\n\n        let group2_added: bool = r\n            .pfadd(\"group:2\", &[\"kaitlyn\", \"michelle\", \"paolo\", \"rachel\"])\n            .expect(\"Failed to add items to group:2\");\n        println!(\"{group2_added}\"); // >>> true\n\n        let group2: usize = r.pfcount(\"group:2\").expect(\"Failed to count group:2\");\n        println!(\"{group2}\"); // >>> 4\n\n        let _: () = r\n            .pfmerge(\"both_groups\", &[\"group:1\", \"group:2\"])\n            .expect(\"Failed to merge HyperLogLogs\");\n        println!(\"OK\"); // >>> OK\n\n        let both_groups: usize = r\n            .pfcount(\"both_groups\")\n            .expect(\"Failed to count both_groups\");\n        println!(\"{both_groups}\"); // >>> 7\n\n    }\n}",
      "section_id": "frequency"
    },
    {
      "id": "quantiles-ex0",
      "language": "csharp",
      "code": "bool res19 = db.TDIGEST().Create(\"male_heights\");\n        Console.WriteLine(res19); // >>> true\n\n        bool res20 = db.TDIGEST().Add(\n            \"male_heights\",\n            175.5, 181, 160.8, 152, 177, 196, 164\n        );\n        Console.WriteLine(res20); // >>> true\n\n        double res21 = db.TDIGEST().Min(\"male_heights\");\n        Console.WriteLine(res21); // >>> 152.0\n\n        double res22 = db.TDIGEST().Max(\"male_heights\");\n        Console.WriteLine(res22); // >>> 196.0\n\n        double[] res23 = db.TDIGEST().Quantile(\"male_heights\", 0.75);\n        Console.WriteLine(string.Join(\", \", res23)); // >>> 181.0\n\n        // Note that the CDF value for 181.0 is not exactly\n        // 0.75. Both values are estimates.\n        double[] res24 = db.TDIGEST().CDF(\"male_heights\", 181.0);\n        Console.WriteLine(string.Join(\", \", res24)); // >>> 0.7857142857142857\n\n        bool res25 = db.TDIGEST().Create(\"female_heights\");\n        Console.WriteLine(res25); // >>> true\n\n        bool res26 = db.TDIGEST().Add(\n            \"female_heights\",\n            155.5, 161, 168.5, 170, 157.5, 163, 171\n        );\n        Console.WriteLine(res26); // >>> true\n\n        double[] res27 = db.TDIGEST().Quantile(\"female_heights\", 0.75);\n        Console.WriteLine(string.Join(\", \", res27)); // >>> 170.0\n\n        // Specify 0 for `compression` and false for `override`.\n        bool res28 = db.TDIGEST().Merge(\n            \"all_heights\", 0, false, \"male_heights\", \"female_heights\"\n        );\n        Console.WriteLine(res28); // >>> true\n\n        double[] res29 = db.TDIGEST().Quantile(\"all_heights\", 0.75);\n        Console.WriteLine(string.Join(\", \", res29)); // >>> 175.5",
      "section_id": "quantiles"
    },
    {
      "id": "quantiles-ex1",
      "language": "go",
      "code": "res20, err := rdb.TDigestCreate(ctx, \"male_heights\").Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res20) // >>> OK\n\n\tres21, err := rdb.TDigestAdd(ctx, \"male_heights\",\n\t\t175.5, 181, 160.8, 152, 177, 196, 164,\n\t).Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res21) // >>> OK\n\n\tres22, err := rdb.TDigestMin(ctx, \"male_heights\").Result()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tfmt.Println(res22) // >>> 152\n\n\tres23, err := rdb.TDigestMax(ctx, \"male_heights\").Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res23) // >>> 196\n\n\tres24, err := rdb.TDigestQuantile(ctx, \"male_heights\", 0.75).Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res24) // >>> [181]\n\n\t// Note that the CDF value for 181 is not exactly\n\t// 0.75. Both values are estimates.\n\tres25, err := rdb.TDigestCDF(ctx, \"male_heights\", 181).Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Printf(\"%.4f\\n\", res25[0]) // >>> 0.7857\n\n\tres26, err := rdb.TDigestCreate(ctx, \"female_heights\").Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res26) // >>> OK\n\n\tres27, err := rdb.TDigestAdd(ctx, \"female_heights\",\n\t\t155.5, 161, 168.5, 170, 157.5, 163, 171,\n\t).Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res27) // >>> OK\n\n\tres28, err := rdb.TDigestQuantile(ctx, \"female_heights\", 0.75).Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res28) // >>> [170]\n\n\tres29, err := rdb.TDigestMerge(ctx, \"all_heights\",\n\t\tnil,\n\t\t\"male_heights\", \"female_heights\",\n\t).Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res29) // >>> OK\n\n\tres30, err := rdb.TDigestQuantile(ctx, \"all_heights\", 0.75).Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res30) // >>> [175.5]",
      "section_id": "quantiles"
    },
    {
      "id": "quantiles-ex2",
      "language": "java",
      "code": "String res20 = jedis.tdigestCreate(\"male_heights\");\n        System.out.println(res20);  // >>> OK\n\n        String res21 = jedis.tdigestAdd(\"male_heights\", \n            175.5, 181, 160.8, 152, 177, 196, 164);\n        System.out.println(res21);  // >>> OK\n\n        double res22 = jedis.tdigestMin(\"male_heights\");\n        System.out.println(res22);  // >>> 152.0\n\n        double res23 = jedis.tdigestMax(\"male_heights\");\n        System.out.println(res23);  // >>> 196.0\n\n        List<Double> res24 = jedis.tdigestQuantile(\"male_heights\", 0.75);\n        System.out.println(res24);  // >>> [181.0]\n\n        // Note that the CDF value for 181 is not exactly 0.75.\n        // Both values are estimates.\n        List<Double> res25 = jedis.tdigestCDF(\"male_heights\", 181);\n        System.out.println(res25);  // >>> [0.7857142857142857]\n\n        String res26 = jedis.tdigestCreate(\"female_heights\");\n        System.out.println(res26);  // >>> OK\n\n        String res27 = jedis.tdigestAdd(\"female_heights\",\n            155.5, 161, 168.5, 170, 157.5, 163, 171);\n        System.out.println(res27);  // >>> OK\n\n        List<Double> res28 = jedis.tdigestQuantile(\"female_heights\", 0.75);\n        System.out.println(res28);  // >>> [170.0]\n\n        String res29 = jedis.tdigestMerge(\n            \"all_heights\",\n            \"male_heights\", \"female_heights\"\n        );\n        System.out.println(res29);  // >>> OK\n        List<Double> res30 = jedis.tdigestQuantile(\"all_heights\", 0.75);\n        System.out.println(res30);  // >>> [175.5]",
      "section_id": "quantiles"
    },
    {
      "id": "quantiles-ex3",
      "language": "php",
      "code": "$r->del('male_heights', 'female_heights', 'all_heights');\n\n$r->tdigestcreate('male_heights');\n$r->tdigestadd('male_heights', 175.5, 181, 160.8, 152, 177, 196, 164);\n\n$maleMin = $r->tdigestmin('male_heights');\necho $maleMin, PHP_EOL;\n// >>> 152\n\n$maleMax = $r->tdigestmax('male_heights');\necho $maleMax, PHP_EOL;\n// >>> 196\n\n$maleQuantile = $r->tdigestquantile('male_heights', 0.75);\necho json_encode($maleQuantile), PHP_EOL;\n// >>> [\"181\"]\n\n$maleCdf = $r->tdigestcdf('male_heights', 181);\necho json_encode($maleCdf), PHP_EOL;\n// >>> [\"0.7857142857142857\"]\n\n$r->tdigestcreate('female_heights');\n$r->tdigestadd('female_heights', 155.5, 161, 168.5, 170, 157.5, 163, 171);\n\n$femaleQuantile = $r->tdigestquantile('female_heights', 0.75);\necho json_encode($femaleQuantile), PHP_EOL;\n// >>> [\"170\"]\n\n$r->tdigestmerge('all_heights', ['male_heights', 'female_heights']);\n$allQuantile = $r->tdigestquantile('all_heights', 0.75);\necho json_encode($allQuantile), PHP_EOL;\n// >>> [\"175.5\"]",
      "section_id": "quantiles"
    },
    {
      "id": "quantiles-ex4",
      "language": "python",
      "code": "res20 = r.tdigest().create(\"male_heights\")\nprint(res20)  # >>> True\n\nres21 = r.tdigest().add(\n    \"male_heights\",\n    [175.5, 181, 160.8, 152, 177, 196, 164]\n)\nprint(res21)  # >>> OK\n\nres22 = r.tdigest().min(\"male_heights\")\nprint(res22)  # >>> 152.0\n\nres23 = r.tdigest().max(\"male_heights\")\nprint(res23)  # >>> 196.0\n\nres24 = r.tdigest().quantile(\"male_heights\", 0.75)\nprint(res24)  # >>> 181\n\n# Note that the CDF value for 181 is not exactly\n# 0.75. Both values are estimates.\nres25 = r.tdigest().cdf(\"male_heights\", 181)\nprint(res25)  # >>> [0.7857142857142857]\n\nres26 = r.tdigest().create(\"female_heights\")\nprint(res26)  # >>> True\n\nres27 = r.tdigest().add(\n    \"female_heights\",\n    [155.5, 161, 168.5, 170, 157.5, 163, 171]\n)\nprint(res27)  # >>> OK\n\nres28 = r.tdigest().quantile(\"female_heights\", 0.75)\nprint(res28)  # >>> [170]\n\nres29 = r.tdigest().merge(\n    \"all_heights\", 2, \"male_heights\", \"female_heights\"\n)\nprint(res29)  # >>> OK\n\nres30 = r.tdigest().quantile(\"all_heights\", 0.75)\nprint(res30)  # >>> [175.5]",
      "section_id": "quantiles"
    },
    {
      "id": "quantiles-ex5",
      "language": "rust",
      "code": "mod home_prob_dts_tests {\n    use redis::AsyncCommands;\n\n    async fn run() {\n        let mut r = match redis::Client::open(\"redis://127.0.0.1\") {\n            Ok(client) => match client.get_multiplexed_async_connection().await {\n                Ok(conn) => conn,\n                Err(e) => {\n                    println!(\"Failed to connect to Redis: {e}\");\n                    return;\n                }\n            },\n            Err(e) => {\n                println!(\"Failed to create Redis client: {e}\");\n                return;\n            }\n        };\n\n        let group1_added: bool = r\n            .pfadd(\"group:1\", &[\"andy\", \"cameron\", \"david\"])\n            .await\n            .expect(\"Failed to add items to group:1\");\n        println!(\"{group1_added}\"); // >>> true\n\n        let group1: usize = r.pfcount(\"group:1\").await.expect(\"Failed to count group:1\");\n        println!(\"{group1}\"); // >>> 3\n\n        let group2_added: bool = r\n            .pfadd(\"group:2\", &[\"kaitlyn\", \"michelle\", \"paolo\", \"rachel\"])\n            .await\n            .expect(\"Failed to add items to group:2\");\n        println!(\"{group2_added}\"); // >>> true\n\n        let group2: usize = r.pfcount(\"group:2\").await.expect(\"Failed to count group:2\");\n        println!(\"{group2}\"); // >>> 4\n\n        let _: () = r\n            .pfmerge(\"both_groups\", &[\"group:1\", \"group:2\"])\n            .await\n            .expect(\"Failed to merge HyperLogLogs\");\n        println!(\"OK\"); // >>> OK\n\n        let both_groups: usize = r\n            .pfcount(\"both_groups\")\n            .await\n            .expect(\"Failed to count both_groups\");\n        println!(\"{both_groups}\"); // >>> 7\n\n    }\n}",
      "section_id": "quantiles"
    },
    {
      "id": "quantiles-ex6",
      "language": "rust",
      "code": "mod home_prob_dts_tests {\n    use redis::Commands;\n\n    fn run() {\n        let mut r = match redis::Client::open(\"redis://127.0.0.1\") {\n            Ok(client) => match client.get_connection() {\n                Ok(conn) => conn,\n                Err(e) => {\n                    println!(\"Failed to connect to Redis: {e}\");\n                    return;\n                }\n            },\n            Err(e) => {\n                println!(\"Failed to create Redis client: {e}\");\n                return;\n            }\n        };\n\n        let group1_added: bool = r\n            .pfadd(\"group:1\", &[\"andy\", \"cameron\", \"david\"])\n            .expect(\"Failed to add items to group:1\");\n        println!(\"{group1_added}\"); // >>> true\n\n        let group1: usize = r.pfcount(\"group:1\").expect(\"Failed to count group:1\");\n        println!(\"{group1}\"); // >>> 3\n\n        let group2_added: bool = r\n            .pfadd(\"group:2\", &[\"kaitlyn\", \"michelle\", \"paolo\", \"rachel\"])\n            .expect(\"Failed to add items to group:2\");\n        println!(\"{group2_added}\"); // >>> true\n\n        let group2: usize = r.pfcount(\"group:2\").expect(\"Failed to count group:2\");\n        println!(\"{group2}\"); // >>> 4\n\n        let _: () = r\n            .pfmerge(\"both_groups\", &[\"group:1\", \"group:2\"])\n            .expect(\"Failed to merge HyperLogLogs\");\n        println!(\"OK\"); // >>> OK\n\n        let both_groups: usize = r\n            .pfcount(\"both_groups\")\n            .expect(\"Failed to count both_groups\");\n        println!(\"{both_groups}\"); // >>> 7\n\n    }\n}",
      "section_id": "quantiles"
    },
    {
      "id": "ranking-ex0",
      "language": "csharp",
      "code": "bool res30 = db.TOPK().Reserve(\"top_3_songs\", 3, 7, 8, 0.9);\n        Console.WriteLine(res30); // >>> true\n\n        RedisResult[] res31 = db.TOPK().IncrBy(\n            \"top_3_songs\",\n            new Tuple<RedisValue, long>[] {\n                new(\"Starfish Trooper\", 3000),\n                new(\"Only one more time\", 1850),\n                new(\"Rock me, Handel\", 1325),\n                new(\"How will anyone know?\", 3890),\n                new(\"Average lover\", 4098),\n                new(\"Road to everywhere\", 770)\n            }\n        );\n        Console.WriteLine(\n            string.Join(\n                \", \",\n                string.Join(\n                    \", \",\n                    res31.Select(\n                        r => $\"{(r.IsNull ? \"Null\" : r)}\"\n                    )\n                )\n            )\n        );\n        // >>> Null, Null, Null, Rock me, Handel, Only one more time, Null\n\n        RedisResult[] res32 = db.TOPK().List(\"top_3_songs\");\n        Console.WriteLine(\n            string.Join(\n                \", \",\n                string.Join(\n                    \", \",\n                    res32.Select(\n                        r => $\"{(r.IsNull ? \"Null\" : r)}\"\n                    )\n                )\n            )\n        );\n        // >>> Average lover, How will anyone know?, Starfish Trooper\n\n        bool[] res33 = db.TOPK().Query(\n            \"top_3_songs\",\n            \"Starfish Trooper\", \"Road to everywhere\"\n        );\n        Console.WriteLine(string.Join(\", \", res33));\n        // >>> true, false",
      "section_id": "ranking"
    },
    {
      "id": "ranking-ex1",
      "language": "go",
      "code": "// Create a TopK filter that keeps track of the top 3 items\n\tres31, err := rdb.TopKReserve(ctx, \"top_3_songs\", 3).Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res31) // >>> OK\n\n\t// Add some items to the filter\n\tres32, err := rdb.TopKIncrBy(ctx,\n\t\t\"top_3_songs\",\n\t\t\"Starfish Trooper\", 3000,\n\t\t\"Only one more time\", 1850,\n\t\t\"Rock me, Handel\", 1325,\n\t\t\"How will anyone know?\", 3890,\n\t\t\"Average lover\", 4098,\n\t\t\"Road to everywhere\", 770,\n\t).Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res32)\n\t// >>> [   Rock me, Handel Only one more time ]\n\n\tres33, err := rdb.TopKList(ctx, \"top_3_songs\").Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res33)\n\t// >>> [Average lover How will anyone know? Starfish Trooper]\n\n\t// Query the count for specific items\n\tres34, err := rdb.TopKQuery(\n\t\tctx,\n\t\t\"top_3_songs\",\n\t\t\"Starfish Trooper\", \"Road to everywhere\",\n\t).Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res34) // >>> [true false]",
      "section_id": "ranking"
    },
    {
      "id": "ranking-ex2",
      "language": "java",
      "code": "String res31 = jedis.topkReserve(\"top_3_songs\", 3L, 2000L, 7L, 0.925D);\n        System.out.println(res31);  // >>> OK\n\n        Map<String, Long> songIncrements = new HashMap<>();\n        songIncrements.put(\"Starfish Trooper\", 3000L);\n        songIncrements.put(\"Only one more time\", 1850L);\n        songIncrements.put(\"Rock me, Handel\", 1325L);\n        songIncrements.put(\"How will anyone know?\", 3890L);\n        songIncrements.put(\"Average lover\", 4098L);\n        songIncrements.put(\"Road to everywhere\", 770L);\n\n        List<String> res32 = jedis.topkIncrBy(\"top_3_songs\",\n            songIncrements\n        );\n        System.out.println(res32);\n        // >>> [null, null, null, null, null, Rock me, Handel]\n\n        List<String> res33 = jedis.topkList(\"top_3_songs\");\n        System.out.println(res33);\n        // >>> [Average lover, How will anyone know?, Starfish Trooper]\n\n        List<Boolean> res34 = jedis.topkQuery(\"top_3_songs\",\n            \"Starfish Trooper\", \"Road to everywhere\"\n        );\n        System.out.println(res34);\n        // >>> [true, false]",
      "section_id": "ranking"
    },
    {
      "id": "ranking-ex3",
      "language": "php",
      "code": "$r->del('top_3_songs');\n$r->topkreserve('top_3_songs', 3, 7, 8, 0.9);\n\n$evicted = $r->topkadd(\n    'top_3_songs',\n    'Starfish Trooper',\n    'Only one more time',\n    'Rock me, Handel',\n    'How will anyone know?',\n    'Average lover',\n    'Road to everywhere'\n);\necho json_encode($evicted), PHP_EOL;\n// >>> [null,null,null,\"Rock me, Handel\",\"Only one more time\",null]\n\n$leaders = $r->topklist('top_3_songs');\necho json_encode($leaders), PHP_EOL;\n// >>> [\"Average lover\",\"How will anyone know?\",\"Starfish Trooper\"]\n\n$membership = $r->topkquery('top_3_songs', 'Starfish Trooper', 'Road to everywhere');\necho json_encode($membership), PHP_EOL;\n// >>> [1,0]",
      "section_id": "ranking"
    },
    {
      "id": "ranking-ex4",
      "language": "python",
      "code": "# The `reserve()` method creates the Top-K object with\n# the given key. The parameters are the number of items\n# in the ranking and values for `width`, `depth`, and\n# `decay`, described in the Top-K reference page.\nres31 = r.topk().reserve(\"top_3_songs\", 3, 7, 8, 0.9)\nprint(res31)  # >>> True\n\n# The parameters for `incrby()` are two lists. The count\n# for each item in the first list is incremented by the\n# value at the same index in the second list.\nres32 = r.topk().incrby(\n    \"top_3_songs\",\n    [\n        \"Starfish Trooper\",\n        \"Only one more time\",\n        \"Rock me, Handel\",\n        \"How will anyone know?\",\n        \"Average lover\",\n        \"Road to everywhere\"\n    ],\n    [\n        3000,\n        1850,\n        1325,\n        3890,\n        4098,\n        770\n    ]\n)\nprint(res32)\n# >>> [None, None, None, 'Rock me, Handel', 'Only one more time', None]\n\nres33 = r.topk().list(\"top_3_songs\")\nprint(res33)\n# >>> ['Average lover', 'How will anyone know?', 'Starfish Trooper']\n\nres34 = r.topk().query(\n    \"top_3_songs\", \"Starfish Trooper\", \"Road to everywhere\"\n)\nprint(res34)  # >>> [1, 0]",
      "section_id": "ranking"
    },
    {
      "id": "ranking-ex5",
      "language": "rust",
      "code": "mod home_prob_dts_tests {\n    use redis::AsyncCommands;\n\n    async fn run() {\n        let mut r = match redis::Client::open(\"redis://127.0.0.1\") {\n            Ok(client) => match client.get_multiplexed_async_connection().await {\n                Ok(conn) => conn,\n                Err(e) => {\n                    println!(\"Failed to connect to Redis: {e}\");\n                    return;\n                }\n            },\n            Err(e) => {\n                println!(\"Failed to create Redis client: {e}\");\n                return;\n            }\n        };\n\n        let group1_added: bool = r\n            .pfadd(\"group:1\", &[\"andy\", \"cameron\", \"david\"])\n            .await\n            .expect(\"Failed to add items to group:1\");\n        println!(\"{group1_added}\"); // >>> true\n\n        let group1: usize = r.pfcount(\"group:1\").await.expect(\"Failed to count group:1\");\n        println!(\"{group1}\"); // >>> 3\n\n        let group2_added: bool = r\n            .pfadd(\"group:2\", &[\"kaitlyn\", \"michelle\", \"paolo\", \"rachel\"])\n            .await\n            .expect(\"Failed to add items to group:2\");\n        println!(\"{group2_added}\"); // >>> true\n\n        let group2: usize = r.pfcount(\"group:2\").await.expect(\"Failed to count group:2\");\n        println!(\"{group2}\"); // >>> 4\n\n        let _: () = r\n            .pfmerge(\"both_groups\", &[\"group:1\", \"group:2\"])\n            .await\n            .expect(\"Failed to merge HyperLogLogs\");\n        println!(\"OK\"); // >>> OK\n\n        let both_groups: usize = r\n            .pfcount(\"both_groups\")\n            .await\n            .expect(\"Failed to count both_groups\");\n        println!(\"{both_groups}\"); // >>> 7\n\n    }\n}",
      "section_id": "ranking"
    },
    {
      "id": "ranking-ex6",
      "language": "rust",
      "code": "mod home_prob_dts_tests {\n    use redis::Commands;\n\n    fn run() {\n        let mut r = match redis::Client::open(\"redis://127.0.0.1\") {\n            Ok(client) => match client.get_connection() {\n                Ok(conn) => conn,\n                Err(e) => {\n                    println!(\"Failed to connect to Redis: {e}\");\n                    return;\n                }\n            },\n            Err(e) => {\n                println!(\"Failed to create Redis client: {e}\");\n                return;\n            }\n        };\n\n        let group1_added: bool = r\n            .pfadd(\"group:1\", &[\"andy\", \"cameron\", \"david\"])\n            .expect(\"Failed to add items to group:1\");\n        println!(\"{group1_added}\"); // >>> true\n\n        let group1: usize = r.pfcount(\"group:1\").expect(\"Failed to count group:1\");\n        println!(\"{group1}\"); // >>> 3\n\n        let group2_added: bool = r\n            .pfadd(\"group:2\", &[\"kaitlyn\", \"michelle\", \"paolo\", \"rachel\"])\n            .expect(\"Failed to add items to group:2\");\n        println!(\"{group2_added}\"); // >>> true\n\n        let group2: usize = r.pfcount(\"group:2\").expect(\"Failed to count group:2\");\n        println!(\"{group2}\"); // >>> 4\n\n        let _: () = r\n            .pfmerge(\"both_groups\", &[\"group:1\", \"group:2\"])\n            .expect(\"Failed to merge HyperLogLogs\");\n        println!(\"OK\"); // >>> OK\n\n        let both_groups: usize = r\n            .pfcount(\"both_groups\")\n            .expect(\"Failed to count both_groups\");\n        println!(\"{both_groups}\"); // >>> 7\n\n    }\n}",
      "section_id": "ranking"
    }
  ]
}
