1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
# Search Example
This example shows how to search documents in Meilisearch with basic and advanced search features.
## What it does
```go
type Movie struct {
ID string `json:"id"`
Title string `json:"title"`
Year int `json:"year"`
Rating float64 `json:"rating"`
Genres []string `json:"genres"`
}
```
1. Create a "movies" index
2. Configure search settings (filterable and sortable attributes)
3. Index sample movie data
4. Simple search for "action" movies
5. Advanced search with filters and facets for "drama" movies
## Configuration
```bash
export MEILI_HOST="http://localhost:7700"
export MEILI_API_KEY="your-api-key"
```
## Run it
```bash
go run ./examples/search
```
```
## Best Practices Demonstrated
- **Settings Configuration**: Configure filterable/sortable attributes before indexing
- **Task Completion**: Always wait for indexing and settings tasks
- **Error Handling**: Comprehensive error handling throughout operations
- **Resource Management**: Proper client cleanup with `defer client.Close()`
- **Facet Processing**: Safe JSON unmarshalling with error handling
- **Environment Configuration**: Flexible host and API key setup
## Advanced Usage
The example demonstrates production-ready patterns:
- **Timeout Management**: 10-second timeout for indexing operations
- **Settings First**: Apply index settings before adding documents
- **Batch Operations**: Efficient bulk document indexing
- **Type Safety**: Proper type conversion for search results
- **JSON Handling**: Safe parsing of facet distribution data
## Troubleshooting
- Ensure Meilisearch server is running on the configured host
- Verify API key permissions if using authentication
- Check that filterable attributes are configured before filtering
- Confirm documents are indexed before searching
|