Skip to content

Commit

Permalink
Add full-text search and TTL index examples
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrymomot committed Jan 21, 2024
1 parent 1b4f752 commit 7acbff6
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions README.md
Expand Up @@ -66,6 +66,79 @@ func findUsers(repo repository.Repository[User]) {
}
```

### Full-Text Search

The package includes a full-text search builder to create text queries easily. The text search query uses the [MongoDB text search](https://docs.mongodb.com/manual/text-search/) feature.

#### Creating a Text Index in Your Collection

When you want to create a text index, specify the field and use the TextIndex option. This field typically stores the text you want to search. MongoDB uses this field to determine if a document is a match. You can also specify the weights for each field to control the relative score of each field.

```go
// Create a text index
err := repo.CreateIndex( context.TODO(), "name", mongorepository.TextIndex(
mongorepository.NewTextIndexConfig(
map[string]int32{
"name": 10,
"bio": 5,
"tags": 1,
},
),
))
```

#### Document Structure

Ensure your documents have a field (like name) that stores the text you want to search. This field is used by MongoDB to determine if a document is a match.

```go
type User struct {
ID primitive.ObjectID `bson:"_id,omitempty"`
Name string `bson:"name"`
Bio string `bson:"bio"`
Tags []string `bson:"tags"`
// Other fields...
}
```

#### Searching for Documents

To search for documents, use the Text helper to create a text search query. The text search query uses the [MongoDB text search](https://docs.mongodb.com/manual/text-search/) feature.

```go
users, err := repo.FindManyByFilter(ctx, 0, 10, mongorepository.TextSearch("John"))
```

### TTL Index

To create an index with a Time-To-Live (TTL) in MongoDB, which automatically deletes documents after a certain amount of time, you need to specify the TTL when creating the index. MongoDB uses a special background task that runs periodically to remove expired documents.

#### Creating a TTL Index in Your Collection

When you want to create an index with a TTL, specify the field and use the TTL option. This field typically stores the creation time of the document and should be of BSON date type.

```go
// Create an index with a TTL of 24 hours
err := repo.CreateIndex(context.TODO(), "createdAt", mongorepository.TTL(24*time.Hour))
```

#### Document Structure

Ensure your documents have a field (like createdAt) that stores the time when the document was created. This field is used by MongoDB to determine if a document is expired.

```go
type YourDocumentType struct {
ID primitive.ObjectID `bson:"_id,omitempty"`
CreatedAt time.Time `bson:"createdAt"`
// Other fields...
}
```

#### Notes

- MongoDB runs a background task every 60 seconds to remove expired documents, so there may be a slight delay before documents are actually deleted.
- This approach is commonly used for data that needs to be retained only for a specific duration, such as logs, temporary data, or session information.

## Contributing

Contributions to the `mongo-repository` package are welcome! Here are some ways you can contribute:
Expand Down

0 comments on commit 7acbff6

Please sign in to comment.