| Crates.io | elastic-mapping |
| lib.rs | elastic-mapping |
| version | 0.1.0 |
| created_at | 2025-12-09 09:16:24.754227+00 |
| updated_at | 2025-12-09 09:16:24.754227+00 |
| description | Generate Elasticsearch mapping definitions from Rust structs and enums using derive macros |
| homepage | https://github.com/Reclamefolder/elastic-mapping |
| repository | https://github.com/Reclamefolder/elastic-mapping |
| max_upload_size | |
| id | 1975153 |
| size | 57,006 |
Generate Elasticsearch mapping definitions from Rust structs and enums using derive macros.
#[serde(rename)], #[serde(rename_all)], #[serde(flatten)] and enum representations#[serde(tag = "...")]), and adjacently tagged (#[serde(tag = "...", content = "...")]) enumschrono, url, and uuid typeschrono - Adds support for chrono::DateTime<Utc> (mapped as date)url - Adds support for url::Url (mapped as text)uuid - Adds support for uuid::Uuid (mapped as text)full - Enables all optional featuresuse elastic_mapping::{Document, MappingObject};
use serde::Serialize;
#[derive(Debug, Serialize, Document)]
struct BlogPost {
title: String,
content: String,
published: bool,
views: i64,
}
fn main() {
let mapping = BlogPost::document_mapping();
println!("{}", serde_json::to_string_pretty(mapping.inner()).unwrap());
}
Output:
{
"mappings": {
"properties": {
"content": {
"type": "text"
},
"published": {
"type": "boolean"
},
"title": {
"type": "text"
},
"views": {
"type": "long"
}
}
}
}
use elastic_mapping::{Document, MappingObject};
#[derive(Debug, Document)]
struct Article {
#[document(analyzer = "english")]
title: String,
#[document(keyword(index = false))]
internal_id: String,
#[document(keyword(ignore_above = 256))]
category: String,
}
The macro respects serde attributes:
use elastic_mapping::{Document, MappingObject};
use serde::Serialize;
#[derive(Debug, Serialize, Document)]
#[serde(rename_all = "camelCase")]
struct UserProfile {
first_name: String,
last_name: String,
email_address: String,
}
// Field names will be mapped as: firstName, lastName, emailAddress
use elastic_mapping::{Document, MappingObject};
#[derive(Debug, Document)]
struct Address {
street: String,
city: String,
country: String,
}
#[derive(Debug, Document)]
struct User {
name: String,
address: Address,
tags: Vec<String>,
}
use elastic_mapping::{Document, MappingObject};
#[derive(Debug, Document)]
enum Event {
Created { id: String, timestamp: i64 },
Updated { id: String, changes: String },
Deleted { id: String },
}
use elastic_mapping::{Document, MappingObject};
use serde::Serialize;
#[derive(Debug, Serialize, Document)]
#[serde(tag = "type")]
enum Message {
Text { content: String },
Image { url: String, width: u32, height: u32 },
}
use elastic_mapping::{Document, MappingObject};
use serde::Serialize;
#[derive(Debug, Serialize, Document)]
#[serde(tag = "type", content = "data")]
enum Notification {
Email { to: String, subject: String },
SMS { phone: String, message: String },
Push { device_id: String, title: String },
}
use elastic_mapping::{Document, MappingObject};
use serde::Serialize;
#[derive(Debug, Serialize, Document)]
struct Metadata {
created_at: i64,
updated_at: i64,
}
#[derive(Debug, Serialize, Document)]
struct Document {
title: String,
content: String,
#[serde(flatten)]
metadata: Metadata,
}
// Fields created_at and updated_at will be flattened into Document
| Rust Type | Elasticsearch Type | Feature Flag |
|---|---|---|
chrono::DateTime<Utc> |
date |
chrono |
url::Url |
text |
url |
uuid::Uuid |
text |
uuid |
#[document(...)] Attributesanalyzer = "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 settingThe following serde attributes are respected:
#[serde(rename = "name")] - Rename a field or variant#[serde(rename_all = "case")] - Rename all fields or variants (supports camelCase, 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 representationThis project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.