os_mapping.json


Overview

The os_mapping.json file is a comprehensive Elasticsearch index template configuration. It defines the settings and mappings for an Elasticsearch index, focusing on optimized data storage, custom similarity scoring, and flexible dynamic field mappings based on naming conventions.

This configuration is designed to:

Such a file is typically used during index creation or update to enforce consistent indexing behavior and enable advanced search features.


Detailed Explanation

The file is structured as a JSON object with two main keys:


settings

Defines index-level settings that control shards, replicas, refresh intervals, and similarity algorithms.

Properties:

scripted_sim similarity:

mappings

Defines how fields in documents are interpreted and indexed.


Properties:


dynamic_templates

This is the core feature enabling flexible field mapping without explicitly defining every field.

Each template includes:


Templates Summary:

Template Name

Match Pattern

Type

Additional Settings

int

*_int

integer

stored

ulong

*_ulong

unsigned_long

stored

long

*_long

long

stored

short

*_short

short

stored

numeric

*_flt

float

stored

tks

*_tks

text

analyzer: whitespace, similarity: scripted_sim, stored

ltks

*_ltks

text

analyzer: whitespace, stored

kwd

regex: [^(.*_(kwd

id

ids

dt

regex: [^.*(_dt

_time

_at)$](/projects/311/73485)

nested

*_nst

nested

object

*_obj

object

dynamic: true

string

regex: [^.*_(with_weight

list)$](/projects/311/73485)

text

rank_feature

*_fea

rank_feature

rank_features

*_feas

rank_features

knn_vector

*_512_vec, *_768_vec, *_1024_vec, *_1536_vec

knn_vector

index: true, space_type: cosinesimil, dimension varies (512, 768, 1024, 1536)

binary

*_bin

binary


Important Implementation Details


Usage Example

Suppose you index a document with fields like:

{
  "user_int": 42,
  "price_flt": 12.99,
  "title_tks": "Elasticsearch vector search",
  "created_dt": "2024-06-01 12:00:00",
  "embedding_512_vec": [0.01, 0.02, ..., 0.512],
  "location_lat_lon": "40.7128,-74.0060"
}

The dynamic templates will automatically map these fields as:


Interaction with Other System Components


Visual Diagram

The following Mermaid flowchart illustrates the workflow of dynamic field mapping and settings application in this os_mapping.json file:

flowchart TD
    A[Start: Index Creation] --> B[Apply Index Settings]
    B --> C{Enable k-NN Search?}
    C -- Yes --> D[Set knn: true]
    C -- No --> E[Skip k-NN]

    D --> F[Configure Shards, Replicas, Refresh]
    F --> G[Define Custom Similarity Script]

    G --> H[Apply Mappings]

    H --> I[Static Mapping: lat_lon (geo_point)]
    H --> J[Enable date_detection]

    H --> K[Dynamic Templates Start]

    K --> L1["*_int" → integer"]
    K --> L2["*_flt" → float"]
    K --> L3["*_tks" → text + scripted_sim"]
    K --> L4["*_512_vec" → knn_vector (dim 512)"]
    K --> L5[Regex Matches (keyword, date, string types)]
    K --> L6["*_nst" → nested"]
    K --> L7["*_obj" → object"]
    K --> L8["*_bin" → binary"]

    L1 & L2 & L3 & L4 & L5 & L6 & L7 & L8 --> M[End: Mapping Complete]

Summary

The os_mapping.json file is a powerful Elasticsearch index configuration enabling:

It is ideal for systems requiring flexible, high-performance search indexes with complex and evolving schemas.