---
title: IndexHandle
description: Read one partition of an index live from an app, with find, count, groupBy, search and nearest.
related:
  - /data/query-across-lunas
  - /sdk/define-index
  - /sdk/luna-handle
---

An `IndexHandle` reads one partition of an index from an app, from `client.index(name, partition)`. Every read is a live query.

The index must say which apps may read it with `onBeforeConnect`. This one lets every app read every board.

```ts luna/lunas/board-cards.ts
import { defineIndex } from "luna";
import { Board } from "./board.ts";

export const BoardCards = defineIndex("BoardCards", {
  partition: "board",
  search: { columns: ["title"] },
  from: [Board.changes.cards.map((r, l) => ({ title: r.title, done: r.done, board: l.id }))],
  onBeforeConnect: () => ({}),
});
```

```ts app/cards.ts
import { createClient } from "luna/client";
import type { BoardCards } from "../luna/lunas/board-cards.ts";

const client = createClient({ url: "<LUNA_URL>", key: "<LUNA_KEY>" });
const cards = client.index<typeof BoardCards>("BoardCards", "b1");

const open = cards.find({ where: { done: false }, orderBy: "title" });
open.subscribe(() => console.log(open.get().data));
```

## Reference

### client.index(name, partition)

| Prop | Type | Default | Description |
| - | - | - | - |
| `name` | `string` | - | The index's name, as given to defineIndex. |
| `partition` | `string \| number` | - | The partition to read: a value of the index's partition column. |

Pass the index as a type argument, `client.index<typeof BoardCards>`, to type its rows. Without one, rows are `Record<string, unknown>`.

### Methods

Each method returns a [LiveQuery](/sdk/luna-handle), whose `get()` and `subscribe()` work as a luna's do.

| Prop | Type | Default | Description |
| - | - | - | - |
| `find(opts)?` | `LiveQuery<Row[]>` | - | Rows that match. opts takes where, orderBy and limit. |
| `count(opts)?` | `LiveQuery<number>` | - | How many rows match. opts takes where. |
| `groupBy(opts)?` | `LiveQuery<IndexGroup<Row>[]>` | - | Rows grouped by some columns, largest groups first. opts takes by, where, sum, min, max and limit. |
| `search(text, opts)?` | `LiveQuery<{ row: Row; score: number }[]>` | - | Full-text search over the index's search columns, best matches first. opts takes where, limit, match and prefix. |
| `nearest(column, to, opts)?` | `LiveQuery<{ row: Row; distance: number }[]>` | - | The rows whose vector column is nearest to, nearest first. opts takes k and where. |
| `read(op, opts)?` | `LiveQuery<...>` | - | Any of the reads above by name, such as read("search", { text: "milk" }). |

### Read options

| Prop | Type | Default | Description |
| - | - | - | - |
| `where?` | `IndexWhere<Row>` | - | Conditions on the rows' columns. |
| `orderBy?` | `IndexOrder<Row>` | - | A column name, or an object such as { title: "desc" }. find only. |
| `limit?` | `number` | - | The most rows or groups to return. search returns 20 unless set. |
| `by` | `string[]` | - | The columns to group by. groupBy only. |
| `sum, min, max?` | `string[]` | - | Columns to total or take the smallest and largest of in each group. groupBy only. |
| `match?` | `"all" \| "any" \| "phrase" \| "raw"` | `"all"` | How the words combine: every word, any word, the words in order, or SQLite FTS5 query syntax. search only. |
| `prefix?` | `boolean` | - | The last word also matches as a prefix, for search as you type. search only. |
| `k?` | `number` | `10` | How many rows, at most 1,000. nearest only. |

### IndexWhere

Each column takes a value, `null`, or an object of operators.

| Prop | Type | Default | Description |
| - | - | - | - |
| `eq?` | `V` | - | Equal to. |
| `ne?` | `V` | - | Not equal to. |
| `lt, lte, gt, gte?` | `V` | - | Less than, at most, more than, at least. |
| `in?` | `V[]` | - | Equal to one of these. |
| `isNull?` | `boolean` | - | true for rows where the column is null, false for rows where it is not. |

### IndexGroup

| Prop | Type | Default | Description |
| - | - | - | - |
| `<column>?` | `Row[column]` | - | The value of each column grouped by. |
| `count` | `number` | - | How many rows are in the group. |
| `sum?` | `Record<string, number \| null>` | - | The total of each column asked for. |
| `min?` | `Record<string, number \| null>` | - | The smallest value of each column asked for. |
| `max?` | `Record<string, number \| null>` | - | The largest value of each column asked for. |

## Good to know

- An index without `onBeforeConnect` cannot be read from an app. What the hook returns becomes the connection's `auth`.
- Each read runs again as the partition's rows change, and the app hears only a result that differs from the last.
- Everyone who watches the same read of a partition shares one run, so many viewers cost about what one does.
- An index's rows are read-only from an app. Your lunas fill it from the sources it names.
- `search` needs the index's `search` option, and `nearest` needs a vector column.

## Examples

### Count open cards

```ts app/cards.ts
const left = cards.count({ where: { done: false } });
```

### Group cards by state

```ts app/cards.ts
const byState = cards.groupBy({ by: ["done"] });
```

### Search as you type

```ts app/cards.ts
const hits = cards.search("mil", { prefix: true, limit: 10 });
hits.subscribe(() => {
  for (const { row, score } of hits.get().data ?? []) console.log(row.title, score);
});
```

### Read from React

`useIndex` and `useIndexQuery` wrap these reads. See [luna/react](/sdk/react).
