Elasticsearch

Elasticsearch Dynamic Templates

joonsei 2022. 12. 6. 13:22

Dynamic mapping

색인 필드의 데이터에 대한 data type을 지정하지 않고 동적으로 elasticsearch 에서 적절한 type을 지정해 주는 기능
 mapping type을 미리 선언하지 않고 패턴이나 분석 특성에 맞춰 구성.

match_mapping_type

JSON 파서를 통해서 data type 을 detection 합니다.
long, double 의 경우 integer 와 float 에 대한 정확한 detection 이 어렵기 때문에 항상 long 과 double 로 detection 

match_pattern

이 설정은 위에서 "match":   "s_*" 과 같은 wildcard 패턴을 java 정규식으로 사용하기 위한 설정

 

[ ▼ 데이터 필드명의 prefix 값으로, 데이터 타입을 dynamic type 으로 사용하는 설정 ]

curl -XPUT "localhost:9200/_template/<indexName>?pretty" -H 'Content-Type: application/json' -d' {
    "index_patterns": [
        "<indexName>"
    ],
    "settings": {
        "number_of_shards": 1
    },
    "mappings": {
        "dynamic_templates": [
            {
                "integers": {
                    "match": "i_*",
                    "match_mapping_type": "*",
                    "mapping": {
                        "type": "integer"
                    }
                }
            },
            {
                "doubles": {
                    "match": "d_*",
                    "match_mapping_type": "*",
                    "mapping": {
                        "type": "double"
                    }
                }
            },
            {
                "floats": {
                    "match": "f_*",
                    "match_mapping_type": "*",
                    "mapping": {
                        "type": "float"
                    }
                }
            },
            {
                "booleans": {
                    "match": "b_*",
                    "match_mapping_type": "*",
                    "mapping": {
                        "type": "boolean"
                    }
                }
            },
            {
                "longs": {
                    "match": "l_*",
                    "match_mapping_type": "*",
                    "mapping": {
                        "type": "long"
                    }
                }
            },
            {
                "dates": {
                    "match": "date",
                    "match_mapping_type": "*",
                    "mapping": {
                        "type": "date"
                    }
                }
            },
            {
                "objects": {
                    "match": "o_*",
                    "match_mapping_type": "*",
                    "mapping": {
                        "type": "object"
                    }
                }
            },
            {
                "nesteds": {
                    "match": "n_*",
                    "match_mapping_type": "*",
                    "mapping": {
                        "type": "object"
                    }
                }
            },
            {
                "strings": {
                    "match": "s_*",
                    "match_mapping_type": "*",
                    "mapping": {
                        "type": "text",
                        "fields": {
                            "raw": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    }
                }
            },
            {
                "no_integers": {
                    "match": "xi_*",
                    "mapping": {
                        "type": "integer",
                        "index": false
                    }
                }
            },
            {
                "no_doubles": {
                    "match": "xd_*",
                    "mapping": {
                        "type": "double",
                        "index": false
                    }
                }
            },
            {
                "no_floats": {
                    "match": "xf_*",
                    "mapping": {
                        "type": "float",
                        "index": false
                    }
                }
            },
            {
                "no_booleans": {
                    "match": "xb_*",
                    "mapping": {
                        "type": "boolean",
                        "index": false
                    }
                }
            },
            {
                "no_longs": {
                    "match": "xl_*",
                    "mapping": {
                        "type": "long",
                        "index": false
                    }
                }
            },
            {
                "no_dates": {
                    "match": "xdate",
                    "mapping": {
                        "type": "date",
                        "index": false
                    }
                }
            },
            {
                "no_objects": {
                    "match": "xo_*",
                    "mapping": {
                        "type": "object"
                    }
                }
            },
            {
                "no_nesteds": {
                    "match": "xn_*",
                    "mapping": {
                        "type": "nested"
                    }
                }
            },
            {
                "no_strings": {
                    "match": "xs_*",
                    "mapping": {
                        "type": "text",
                        "index": false
                    }
                }
            }
        ]
    },
    "aliases": {}
} '