> ## 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.

# GEOSEARCHSTORE

> Store geosearch results.

Use `GEOSEARCHSTORE` to run the same query as [`GEOSEARCH`](/docs/redis/commands/geo/geosearch) and store the matching members in another key instead of returning them.

The destination is a sorted set holding the matches. By default their scores are the raw geohash values, so the destination is itself a valid geospatial index that can be queried further; with `STOREDIST` the score is the distance from the center in the unit of the query, which turns the result into a proximity-ordered list you can page through with [`ZRANGE`](/docs/redis/commands/sorted-set/zrange).

The destination is overwritten on every call, and it is deleted when the query matches nothing. The reply is the number of members stored. This is the usual way to materialize a "nearby" result once and then reuse it for pagination or further set operations.

## Syntax

```redis theme={"system"}
GEOSEARCHSTORE <destination> <source>
  (FROMMEMBER <member> | FROMLONLAT <longitude> <latitude>)
  (BYRADIUS <radius> (m | km | ft | mi) |
    BYBOX <width> <height> (m | km | ft | mi))
  [ASC | DESC]
  [COUNT <count> [ANY]]
  [STOREDIST]
```

## Arguments

| Argument                                                                                    | Required | Repeatable | Description                                                                                                                                                                                                                     |
| ------------------------------------------------------------------------------------------- | -------- | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `<destination>`                                                                             | Yes      | No         | Redis key used as destination.                                                                                                                                                                                                  |
| `<source>`                                                                                  | Yes      | No         | Redis key used as source.                                                                                                                                                                                                       |
| `(FROMMEMBER <member> \| FROMLONLAT <longitude> <latitude>)`                                | Yes      | No         | Where to center the search: `FROMMEMBER` uses the stored position of an existing member, `FROMLONLAT` uses the given coordinates.                                                                                               |
| `(BYRADIUS <radius> (m \| km \| ft \| mi) \| BYBOX <width> <height> (m \| km \| ft \| mi))` | Yes      | No         | The area to search: `BYRADIUS` a circle of the given radius, `BYBOX` an axis-aligned box of the given width and height centered on the search point. The unit is `m` (meters), `km` (kilometers), `ft` (feet), or `mi` (miles). |
| `(ASC \| DESC)`                                                                             | No       | No         | Sort the matches by distance from the center: `ASC` (nearest first) or `DESC` (farthest first). Unsorted when omitted.                                                                                                          |
| `COUNT <count> [ANY]`                                                                       | No       | No         | Return at most `<count>` matches. `ANY` returns as soon as enough matches are found, instead of sorting every match first.                                                                                                      |
| `STOREDIST`                                                                                 | No       | No         | Store each match's distance from the center instead of its geohash score.                                                                                                                                                       |

## Response

The reply reports the result of the operation. Error replies have the same shape in RESP2 and RESP3 and are surfaced as exceptions by the SDKs below.

| Protocol | Reply   |
| -------- | ------- |
| RESP2    | Integer |
| RESP3    | Integer |

<Note>
  Client libraries often decode bulk strings, maps, sets, and numeric strings into language-native values. The table describes the Redis wire reply.
</Note>

## Examples

TCP examples use the TLS `REDIS_URL` from the Upstash console. REST examples use `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN`.

<AccordionGroup>
  <Accordion title="Redis CLI" icon="terminal">
    ```bash theme={"system"}
    GEOSEARCHSTORE destination-key source-key FROMMEMBER member BYRADIUS 1.5 m
    ```
  </Accordion>

  <Accordion title="@upstash/redis" icon="node-js" iconType="brands">
    ```ts theme={"system"}
    import { Redis } from "@upstash/redis";

    const redis = Redis.fromEnv();
    const result = await redis.geosearchstore(
      "destination-key",
      "source-key",
      { type: "FROMMEMBER", member: "member" },
      { type: "BYRADIUS", radius: 1.5, radiusType: "M" },
      "ASC",
    );
    console.log(result);
    ```
  </Accordion>

  <Accordion title="upstash_redis" icon="python" iconType="brands">
    ```python theme={"system"}
    from upstash_redis import Redis

    redis = Redis.from_env()
    result = redis.geosearchstore("destination-key", "source-key", member="member", radius=1.5, unit="M")
    print(result)
    ```
  </Accordion>

  <Accordion title="ioredis" icon="node-js" iconType="brands">
    ```ts theme={"system"}
    import Redis from "ioredis";

    const redis = new Redis(process.env.REDIS_URL!);
    const result = await redis.geosearchstore("destination-key", "source-key", "FROMMEMBER", "member", "BYRADIUS", "1.5", "m");
    console.log(result);
    ```
  </Accordion>

  <Accordion title="node-redis" icon="node-js" iconType="brands">
    ```ts theme={"system"}
    import { createClient } from "redis";

    const client = await createClient({ url: process.env.REDIS_URL })
      .on("error", console.error)
      .connect();
    const result = await client.geoSearchStore("destination-key", "source-key", "member", { radius: 1.5, unit: "m" });
    console.log(result);
    ```
  </Accordion>

  <Accordion title="redis-py" icon="python" iconType="brands">
    ```python theme={"system"}
    import os
    import redis

    client = redis.from_url(os.environ["REDIS_URL"])
    result = client.geosearchstore("destination-key", "source-key", member="member", radius=1.5, unit="m")
    print(result)
    ```
  </Accordion>

  <Accordion title="go-redis" icon="golang" iconType="brands">
    ```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.GeoSearchStore(context.Background(), "source-key", "destination-key", &redis.GeoSearchStoreQuery{GeoSearchQuery: redis.GeoSearchQuery{Member: "member", Radius: 1.5, RadiusUnit: "m"}}).Result()
        if err != nil {
            panic(err)
        }
        fmt.Println(result)
    }
    ```
  </Accordion>

  <Accordion title="jedis" icon="java" iconType="brands">
    ```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.geosearchStore("destination-key", "source-key", "member", 1.5, redis.clients.jedis.args.GeoUnit.M);
      System.out.println(result);
    }
    ```
  </Accordion>

  <Accordion title="redis-rs" icon="rust" iconType="brands">
    ```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("GEOSEARCHSTORE");
        command.arg("destination-key");
        command.arg("source-key");
        command.arg("member");
        command.arg("1.5");
        command.arg("m");
        let result: redis::Value = command.query(&mut connection)?;
        println!("{result:?}");
        Ok(())
    }
    ```
  </Accordion>
</AccordionGroup>


## Related topics

- [GEORADIUS](/docs/redis/commands/geo/georadius.md)
- [GEORADIUSBYMEMBER](/docs/redis/commands/geo/georadiusbymember.md)
- [GEOSEARCH](/docs/redis/commands/geo/geosearch.md)
