Expand description
Generate Elasticsearch mapping definitions from Rust structs and enums.
This crate provides a derive macro to automatically generate Elasticsearch mapping definitions from Rust types. It respects serde attributes and supports various Elasticsearch-specific field configurations.
§Features
- Derive macro - Automatic mapping generation using
#[derive(Document)] - Serde compatibility - Respects
#[serde(rename)],#[serde(rename_all)],#[serde(flatten)] - Enum support - Handles externally tagged, internally tagged, and adjacently tagged enums
- Field annotations - Configure analyzers, keyword fields, and other Elasticsearch settings
- Optional features - Support for
chrono,url, anduuidtypes
§Quick Start
use elastic_mapping::{Document, MappingObject};
#[derive(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());
}§Field Annotations
Use the #[document(...)] attribute to configure Elasticsearch-specific field settings:
use elastic_mapping::Document;
#[derive(Document)]
struct Article {
/// Configure an analyzer for text analysis
#[document(analyzer = "english")]
title: String,
/// Add a keyword subfield that is not indexed
#[document(keyword(index = false))]
internal_id: String,
/// Add a keyword subfield with ignore_above setting
#[document(keyword(ignore_above = 256))]
category: String,
}§Serde Compatibility
The macro respects common serde attributes:
use elastic_mapping::Document;
use serde::Serialize;
#[derive(Serialize, Document)]
#[serde(rename_all = "camelCase")]
struct User {
first_name: String, // Maps to "firstName"
#[serde(rename = "mail")]
email: String, // Maps to "mail"
}§Enum Support
Different enum representations are supported:
use elastic_mapping::Document;
use serde::Serialize;
// Externally tagged (default)
#[derive(Document)]
enum Status {
Active { since: i64 },
Pending { until: i64 },
}
// Internally tagged
#[derive(Serialize, Document)]
#[serde(tag = "type")]
enum Message {
Text { content: String },
Image { url: String },
}
// Adjacently tagged
#[derive(Serialize, Document)]
#[serde(tag = "type", content = "data")]
enum Event {
Created { id: String },
Updated { id: String, changes: String },
}Re-exports§
pub use serde_json;
Structs§
- Mapping
Document - A wrapper around the generated Elasticsearch mapping JSON.
Traits§
- Mapping
Object - Trait for types that can be used as Elasticsearch document mappings.
- Mapping
Type - Trait for types that can be mapped to Elasticsearch field types.
Derive Macros§
- Document
- Derives Elasticsearch mapping generation for structs and enums.