elastic-mapping
Generate Elasticsearch mapping definitions from Rust structs and enums using derive macros.
Features
- Derive macro for automatic mapping generation from Rust types
- Serde compatibility - respects
#[serde(rename)],#[serde(rename_all)],#[serde(flatten)]and enum representations - Enum support - handles externally tagged, internally tagged (
#[serde(tag = "...")]), and adjacently tagged (#[serde(tag = "...", content = "...")]) enums - Field annotations - configure analyzers, keyword fields, and other Elasticsearch-specific settings
- Optional feature flags - support for
chrono,url, anduuidtypes
Available Features
chrono- Adds support forchrono::DateTime<Utc>(mapped asdate)url- Adds support forurl::Url(mapped astext)uuid- Adds support foruuid::Uuid(mapped astext)full- Enables all optional features
Usage
Basic Example
use ;
use Serialize;
Output:
Field Annotations
use ;
Serde Compatibility
The macro respects serde attributes:
use ;
use Serialize;
// Field names will be mapped as: firstName, lastName, emailAddress
Nested Structures
use ;
Enum Support
Externally Tagged Enums
use ;
Internally Tagged Enums
use ;
use Serialize;
Adjacently Tagged Enums
use ;
use Serialize;
Flatten Support
use ;
use Serialize;
// Fields created_at and updated_at will be flattened into Document
With Optional Features
| Rust Type | Elasticsearch Type | Feature Flag |
|---|---|---|
chrono::DateTime<Utc> |
date |
chrono |
url::Url |
text |
url |
uuid::Uuid |
text |
uuid |
Available Attributes
Field-level #[document(...)] Attributes
analyzer = "analyzer_name"- Sets the analyzer for a text fieldkeyword(index = bool)- Adds a keyword subfield with index configurationkeyword(ignore_above = u32)- Adds a keyword subfield with ignore_above setting
Serde Attributes
The following serde attributes are respected:
#[serde(rename = "name")]- Rename a field or variant#[serde(rename_all = "case")]- Rename all fields or variants (supportscamelCase,kebab-case,snake_case,SCREAMING_SNAKE_CASE, etc.)#[serde(flatten)]- Flatten nested structure fields#[serde(tag = "...")]- Internally tagged enum representation#[serde(tag = "...", content = "...")]- Adjacently tagged enum representation
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.