Document

Derive Macro Document 

Source
#[derive(Document)]
{
    // Attributes available to this derive:
    #[document]
}
Expand description

Derives Elasticsearch mapping generation for structs and enums.

This macro implements the MappingType and MappingObject traits for your type, allowing you to call document_mapping() to generate Elasticsearch mappings.

§Supported Attributes

§Container-level (Struct/Enum) Attributes

  • #[serde(rename_all = "...")] - Rename all fields/variants according to the given case convention
    • Supported cases: camelCase, PascalCase, snake_case, SCREAMING_SNAKE_CASE, kebab-case, etc.
  • #[serde(tag = "...")] - Use internally tagged enum representation
  • #[serde(tag = "...", content = "...")] - Use adjacently tagged enum representation

§Field-level Attributes

  • #[document(analyzer = "analyzer_name")] - Sets the analyzer for a text field
  • #[document(keyword(index = bool))] - Adds a keyword subfield with index configuration
  • #[document(keyword(ignore_above = u32))] - Adds a keyword subfield with ignore_above setting
  • #[serde(rename = "name")] - Rename this field/variant
  • #[serde(flatten)] - Flatten nested structure fields into the parent

§Examples

§Basic Struct

use elastic_mapping::Document;

#[derive(Document)]
struct Product {
    name: String,
    price: f64,
    in_stock: bool,
}

§With Field Annotations

use elastic_mapping::Document;

#[derive(Document)]
struct Article {
    #[document(analyzer = "english")]
    title: String,
     
    #[document(keyword(index = false))]
    internal_id: String,
}

§With Serde Attributes

use elastic_mapping::Document;
use serde::Serialize;

#[derive(Serialize, Document)]
#[serde(rename_all = "camelCase")]
struct UserProfile {
    first_name: String,
    last_name: String,
}

§Enum Support

use elastic_mapping::Document;
use serde::Serialize;

// Adjacently tagged enum
#[derive(Serialize, Document)]
#[serde(tag = "type", content = "data")]
enum Event {
    Created { id: String, timestamp: i64 },
    Updated { id: String, changes: String },
    Deleted { id: String },
}