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

# TFIDFVectoria Methods

> Methods for the TFIDFVectoria class

Methods available on the TFIDFVectoria class.

## addDocument()

Add a document to the index.

### Signature

```ts theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
addDocument(id: string, text: string, metadata: T): void
```

### Example

```ts theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
db.addDocument('tool1', 'User authentication tool', {
  id: 'tool1',
  toolName: 'auth',
  category: 'security',
});
```

<Warning>
  After adding documents, you must call `reindex()` before searching.
</Warning>

***

## reindex()

Rebuild the TF-IDF index. Required after document changes.

### Signature

```ts theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
reindex(): void
```

### Example

```ts theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
db.addDocument('doc1', 'Text one', metadata1);
db.addDocument('doc2', 'Text two', metadata2);

// MUST reindex before searching
db.reindex();

// Now search works
const results = db.search('query');
```

***

## search()

Search for documents using TF-IDF similarity.

### Signature

```ts theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
search(query: string, options?: SearchOptions<T>): SearchResult<T>[]
```

### Parameters

| Parameter           | Type       | Description     |
| ------------------- | ---------- | --------------- |
| `query`             | `string`   | Search query    |
| `options.topK`      | `number`   | Maximum results |
| `options.threshold` | `number`   | Minimum score   |
| `options.filter`    | `function` | Metadata filter |

### Example

```ts theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
const results = db.search('authentication', {
  topK: 5,
  threshold: 0.1,
  filter: (m) => m.category === 'security',
});
```

***

## removeDocument()

Remove a document from the index.

### Signature

```ts theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
removeDocument(id: string): boolean
```

### Example

```ts theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
const removed = db.removeDocument('tool1');
db.reindex(); // Reindex after removal
```

***

## getDocument()

Get a document by ID.

### Signature

```ts theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
getDocument(id: string): { id: string; text: string; metadata: T } | undefined
```

***

## hasDocument()

Check if a document exists.

### Signature

```ts theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
hasDocument(id: string): boolean
```

***

## size()

Get the number of documents.

### Signature

```ts theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
size(): number
```

***

## clear()

Remove all documents.

### Signature

```ts theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
clear(): void
```

## Complete Example

```ts theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
import { TFIDFVectoria } from 'vectoriadb';

interface Tool {
  id: string;
  name: string;
  category: string;
}

const toolSearch = new TFIDFVectoria<Tool>();

// Add documents
toolSearch.addDocument('user-create', 'Create new user account registration signup', {
  id: 'user-create',
  name: 'createUser',
  category: 'users',
});

toolSearch.addDocument('user-delete', 'Delete remove user account termination', {
  id: 'user-delete',
  name: 'deleteUser',
  category: 'users',
});

// Reindex
toolSearch.reindex();

// Search
const results = toolSearch.search('create account');
// Returns: user-create with high score
```

## Related

<CardGroup cols={2}>
  <Card title="TFIDFVectoria Constructor" icon="plus" href="/vectoriadb/api-reference/tfidf-vectoria/constructor">
    Create instance
  </Card>

  <Card title="TF-IDF Guide" icon="text" href="/vectoriadb/guides/alternatives/tfidf">
    Usage guide
  </Card>
</CardGroup>
