Learn how to use Clay's powerful command filtering system to search and organize commands using various criteria
The Clay command filter system provides a powerful way to search and filter commands based on various criteria such as type, tags, path, name, and metadata. It uses Bleve as the search backend and offers a modern, type-safe API for building complex queries.
The filter system allows you to:
The filter command provides a CLI interface to the filtering system. It can easily be added to any tool using glazed commands.
# Filter by type
XXX commands filter --type http
# Filter by multiple types
XXX commands filter --types http,grpc
# Filter by tag
XXX commands filter --tag api
# Filter by multiple tags (any match)
XXX commands filter --tags api,stable
# Filter by multiple tags (all must match)
XXX commands filter --all-tags api,stable
# Filter by path
XXX commands filter --path "services/api"
# Filter by path glob pattern
XXX commands filter --path-glob "services/*/api"
# Filter by path prefix
XXX commands filter --path-prefix "services/"
# Filter by name
XXX commands filter --name "http-server"
# Filter by name pattern
XXX commands filter --name-pattern "http-*"
# Filter by metadata
XXX commands filter --metadata-key version --metadata-value "1.0.0"
The filter system provides a fluent builder API for constructing queries programmatically:
import (
"github.com/go-go-golems/clay/pkg/filters/command/builder"
)
// Create a new builder
b := builder.New()
// Build simple filters
typeFilter := b.Type("http")
tagFilter := b.Tag("api")
pathFilter := b.Path("services/api")
// Build pattern filters
namePattern := b.NamePattern("http-*")
pathGlob := b.PathGlob("services/*/api")
// Build complex filters
filter := b.Type("http").
And(b.AnyTags("api", "stable")).
And(b.PathPrefix("services/"))
Type Filters:
b.Type("http") // Single type
b.Types("http", "grpc") // Multiple types (OR)
Tag Filters:
b.Tag("api") // Single tag
b.Tags("api", "stable") // Multiple tags (OR)
b.AllTags("api", "stable") // Multiple tags (AND)
b.AnyTags("api", "stable") // Alias for Tags
Path Filters:
b.Path("services/api") // Exact path
b.PathGlob("services/*/api") // Glob pattern
b.PathPrefix("services/") // Path prefix
Name Filters:
b.Name("http-server") // Exact name
b.NamePattern("http-*") // Name pattern
Metadata Filters:
b.Metadata("version", "1.0.0") // Single field
b.MetadataMatch(map[string]interface{}{ // Multiple fields
"version": "1.0.0",
"stage": "prod",
})
Filters can be combined using boolean operations:
// AND combination
filter := b.Type("http").And(b.Tag("api"))
// OR combination
filter := b.Type("http").Or(b.Type("grpc"))
// Complex combinations
filter := b.Type("http").
And(
b.AnyTags("api", "stable").
Or(b.Tag("internal"))
).
And(b.PathPrefix("services/"))
The filter system uses specialized field mappings for different types of fields:
Keyword Fields (exact matching):
nametypetagsfull_pathText Fields (analyzed):
name (additional mapping for pattern matching)Dynamic Fields:
metadata.* (supports various value types)Query Building:
Filter Methods -> Query Builder -> Bleve Query
Execution:
Query -> Search Request -> Results -> Command List
Result Processing:
Command List -> Structured Output -> Formatted Display
Query Construction:
Path Handling:
Performance:
filter := b.Type("http").
And(b.Tag("api")).
And(b.PathPrefix("services/"))
filter := b.AnyTags("stable", "beta").
And(b.PathGlob("*/v[0-9]*"))
filter := b.MetadataMatch(map[string]interface{}{
"version": "1.0.0",
"stage": "prod",
})
The filter system includes comprehensive debug logging (when enabled):
// Enable debug logging
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).Level(zerolog.DebugLevel)
// Logs will show:
// - Query construction details
// - Field mapping information
// - Search execution details
// - Result processing
The system provides clear error messages for common issues:
Invalid Patterns:
"invalid glob pattern: [invalid-glob"
Missing Fields:
"command document must have a name"
"command document must have a type"
Search Errors:
"could not search commands: [error details]"