> ## Documentation Index
> Fetch the complete documentation index at: https://upstash.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# SEARCH.COUNT

> Count documents matching a query.

Use `SEARCH.COUNT` to count documents matching a query without retrieving them.

The filter is the same JSON object that [`SEARCH.QUERY`](/docs/redis/commands/search/search-query) takes, but only the number of matching documents is computed and returned, so nothing depends on the size of the result and no `LIMIT` gets in the way of the total. That makes it the right command for result counters, pagination totals, and cheap existence checks over a filter.

See [Counting](/docs/redis/search/counting) for feature examples and common use cases.

## Syntax

```redis theme={"system"}
SEARCH.COUNT <name> '<json_filter>'
```

`<json_filter>` is an [Upstash JSON filter](/docs/redis/search/querying). The command accepts an index name or alias.

## Response

Returns the number of matching documents as an integer. Returns `-1` if the index does not exist.

## Examples

> **Redis CLI**
>
> ```bash theme={"system"}
> SEARCH.COUNT products '{"inStock": true}'
> ```

> **@upstash/redis**
>
> ```ts theme={"system"}
> import { Redis } from "@upstash/redis";
>
> const redis = Redis.fromEnv();
> const products = redis.search.index({ name: "products" });
>
> const result = await products.count({
>   filter: { inStock: true },
> });
> console.log(result.count);
> ```

> **upstash\_redis**
>
> ```python theme={"system"}
> from upstash_redis import Redis
>
> redis = Redis.from_env()
> products = redis.search.index(name="products")
>
> result = products.count(filter={"inStock": True})
> print(result.count)
> ```

> **ioredis**
>
> ```ts theme={"system"}
> import IORedis from "ioredis";
> import { createSearch } from "@upstash/search-ioredis";
>
> const redis = new IORedis(process.env.REDIS_URL!);
> const search = createSearch(redis);
> const products = search.index({ name: "products" });
>
> const result = await products.count({
>   filter: { inStock: true },
> });
> console.log(result.count);
> ```

> **node-redis**
>
> ```ts theme={"system"}
> import { createClient } from "redis";
> import { createSearch } from "@upstash/search-redis";
>
> const client = await createClient({ url: process.env.REDIS_URL })
>   .on("error", console.error)
>   .connect();
> const search = createSearch(client);
> const products = search.index({ name: "products" });
>
> const result = await products.count({
>   filter: { inStock: true },
> });
> console.log(result.count);
> ```

> **redis-py**
>
> ```python theme={"system"}
> import os
> import redis
>
> client = redis.from_url(os.environ["REDIS_URL"])
> result = client.execute_command("SEARCH.COUNT", "products", "{\"inStock\": true}")
> print(result)
> ```

> **go-redis**
>
> ```go theme={"system"}
> package main
>
> import (
>     "context"
>     "fmt"
>     "os"
>
>     "github.com/redis/go-redis/v9"
> )
>
> func main() {
>     opts, err := redis.ParseURL(os.Getenv("REDIS_URL"))
>     if err != nil {
>         panic(err)
>     }
>     client := redis.NewClient(opts)
>     result, err := client.Do(context.Background(), "SEARCH.COUNT", "products", "{\"inStock\": true}").Result()
>     if err != nil {
>         panic(err)
>     }
>     fmt.Println(result)
> }
> ```

> **jedis**
>
> ```java theme={"system"}
> import java.net.URI;
>
> import redis.clients.jedis.Jedis;
>
> try (Jedis jedis = new Jedis(new URI(System.getenv("REDIS_URL")))) {
>   Object result = jedis.sendCommand(() -> "SEARCH.COUNT".getBytes(), "products", "{\"inStock\": true}");
>   System.out.println(result);
> }
> ```

> **redis-rs**
>
> ```rust theme={"system"}
> fn main() -> redis::RedisResult<()> {
>     let url = std::env::var("REDIS_URL").expect("REDIS_URL is not set");
>     let client = redis::Client::open(url)?;
>     let mut connection = client.get_connection()?;
>
>     let mut command = redis::cmd("SEARCH.COUNT");
>     command.arg("products");
>     command.arg("{\"inStock\": true}");
>     let result: redis::Value = command.query(&mut connection)?;
>     println!("{result:?}");
>     Ok(())
> }
> ```

> **curl**
>
> ```bash theme={"system"}
> curl -X POST https://YOUR_ENDPOINT.upstash.io \
>   -H "Authorization: Bearer $UPSTASH_REDIS_REST_TOKEN" \
>   -d '["SEARCH.COUNT", "products", "{\"inStock\": true}"]'
> ```


## Related topics

- [E-commerce Search](/docs/redis/search/recipes/e-commerce-search.md)
- [Counting](/docs/redis/search/counting.md)
- [Search commands](/docs/redis/commands/search/overview.md)
- [Quickstart](/docs/redis/search/getting-started.md)
- [SEARCH.QUERY](/docs/redis/commands/search/search-query.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.