# GEOSEARCH

> Search for members in an area.

Use `GEOSEARCH` to find the members of a geospatial index that fall inside a circle or a rectangle.

The center is either an existing member of the index (`FROMMEMBER`) or an explicit coordinate pair (`FROMLONLAT`). The area is either a circle of a given radius (`BYRADIUS`) or an axis-aligned box of a given width and height centered on that point (`BYBOX`), which is the shape to use when you are covering a map viewport rather than a "within N km" question.

By default only member names come back. `WITHDIST` adds the distance from the center in the unit of the query, `WITHCOORD` the member's coordinates, and `WITHHASH` its raw geohash score. `ASC` and `DESC` sort by distance, and `COUNT` caps the number of results; adding `ANY` lets the server return as soon as it has enough matches, which is faster but no longer gives you the nearest ones.

`GEOSEARCH` replaces the deprecated `GEORADIUS` and `GEORADIUSBYMEMBER` commands and is the command to use for new code. Use [`GEOSEARCHSTORE`](/redis/commands/geo/geosearchstore) when the result should be stored instead of returned.

## Syntax

```redis
GEOSEARCH <key>
  (FROMMEMBER <member> | FROMLONLAT <longitude> <latitude>)
  (BYRADIUS <radius> (m | km | ft | mi) |
    BYBOX <width> <height> (m | km | ft | mi))
  [ASC | DESC]
  [COUNT <count> [ANY]]
  [WITHCOORD]
  [WITHDIST]
  [WITHHASH]
```

## Arguments

| Argument | Required | Repeatable | Description |
| --- | --- | --- | --- |
| `<key>` | Yes | No | Redis key targeted by the command. |
| `(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. |
| `WITHCOORD` | No | No | Also return the longitude and latitude of each match. |
| `WITHDIST` | No | No | Also return the distance from the center, in the requested unit. |
| `WITHHASH` | No | No | Also return the raw 52-bit geohash score of each match. |

## 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 | Array of bulk-string members or member-detail arrays |
| RESP3 | Array of bulk-string members or member-detail arrays |

<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
GEOSEARCH my-key FROMMEMBER member BYRADIUS 1.5 m
```

</Accordion>

<Accordion title="@upstash/redis" icon="node-js" iconType="brands">

```ts
import { Redis } from "@upstash/redis";

const redis = Redis.fromEnv();
const result = await redis.geosearch(
  "my-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
from upstash_redis import Redis

redis = Redis.from_env()
result = redis.geosearch("my-key", member="member", radius=1.5, unit="M")
print(result)
```

</Accordion>

<Accordion title="ioredis" icon="node-js" iconType="brands">

```ts
import Redis from "ioredis";

const redis = new Redis(process.env.REDIS_URL!);
const result = await redis.geosearch("my-key", "FROMMEMBER", "member", "BYRADIUS", "1.5", "m");
console.log(result);
```

</Accordion>

<Accordion title="node-redis" icon="node-js" iconType="brands">

```ts
import { createClient } from "redis";

const client = await createClient({ url: process.env.REDIS_URL })
  .on("error", console.error)
  .connect();
const result = await client.geoSearch("my-key", "member", { radius: 1.5, unit: "m" });
console.log(result);
```

</Accordion>

<Accordion title="redis-py" icon="python" iconType="brands">

```python
import os
import redis

client = redis.from_url(os.environ["REDIS_URL"])
result = client.geosearch("my-key", member="member", radius=1.5, unit="m")
print(result)
```

</Accordion>

<Accordion title="go-redis" icon="golang" iconType="brands">

```go
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.GeoSearch(context.Background(), "my-key", &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
import java.net.URI;

import redis.clients.jedis.Jedis;

try (Jedis jedis = new Jedis(new URI(System.getenv("REDIS_URL")))) {
  Object result = jedis.geosearch("my-key", "member", 1.5, redis.clients.jedis.args.GeoUnit.M);
  System.out.println(result);
}
```

</Accordion>

<Accordion title="redis-rs" icon="rust" iconType="brands">

```rust
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("GEOSEARCH");
    command.arg("my-key");
    command.arg("member");
    command.arg("1.5");
    command.arg("m");
    let result: redis::Value = command.query(&mut connection)?;
    println!("{result:?}");
    Ok(())
}
```

</Accordion>

</AccordionGroup>
