美文网首页
Elasticsearch 7.x 深入【6】Template

Elasticsearch 7.x 深入【6】Template

作者: 孙瑞锴 | 来源:发表于2020-05-03 20:12 被阅读0次

    1. 借鉴

    极客时间 阮一鸣老师的Elasticsearch核心技术与实战
    官网 search-template

    2. 开始

    一、Index Template

    按照设置,自动为匹配的索引设置mapping和setting

    • 模板仅在索引被创建时起作用
    • 可以设置多个模板,根据order属性,进行合并(order数值越大,优先级越高)

    那我们如何来创建一个模板呢?

    PUT /_template/test_template_1
    {
      "index_patterns": ["test_*"],
      "order": 0,
      "version": 1,
      "mappings": {
        "date_detection": true,
        "numeric_detection": true
      }
    }
    
    • 其中mappings和settings中的设置是可以选择的
      我们接着索引一个文档
    PUT /test_mytemp/_doc/1
    {
      "createTime": "2020-05-03 10:00:00",
      "count": 10
    }
    
    • 我们看下这个索引的mapping
    GET /test_mytemp/_mapping
    
    • 可以看到我们在模板中设置的date_detection和numeric_detection已经加入到我们的索引中了
    {
      "test_mytemp" : {
        "mappings" : {
          "date_detection" : true,
          "numeric_detection" : true,
          "properties" : {
            "count" : {
              "type" : "long"
            },
            "createTime" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            }
          }
        }
      }
    }
    

    如何查询我创建的模板呢?

    • GET /test_mytemp/_search
    • GET /test_mytemp/_mapping

    如何删除呢?

    • DELETE /_template/test_template_1

    二、Dynamic Template

    动态设置字段类型

    PUT /my_dy_index
    {
      "mappings": {
        "dynamic_templates": [
        {
          "full_name": {
            "path_match": "name.*",
            "mapping": {
              "type": "text",
              "copy_to": "full_name"
            }
          }
        },
        {
          "string_as_boolean": {
            "match_mapping_type": "string",
            "match": "is*",
            "mapping": {
              "type": "boolean"
            }
          }
        }]
      }
    }
    
    • 我们索引一篇文档,然后看下mapping信息
    PUT /my_dy_index/_doc/1
    {
      "name": {
        "first": "sun",
        "last": "ruikai"
      },
      "isVip": "true"
    }
    
    GET /my_dy_index/_mapping
    
    • mapping信息如下,我们看到isVip已经变为boolean类型:
    {
      "my_dy_index" : {
        "mappings" : {
          "dynamic_templates" : [
            {
              "full_name" : {
                "path_match" : "name.*",
                "mapping" : {
                  "copy_to" : "full_name",
                  "type" : "text"
                }
              }
            },
            {
              "string_as_boolean" : {
                "match" : "is*",
                "match_mapping_type" : "string",
                "mapping" : {
                  "type" : "boolean"
                }
              }
            }
          ],
          "properties" : {
            "full_name" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "isVip" : {
              "type" : "boolean"
            },
            "name" : {
              "properties" : {
                "first" : {
                  "type" : "text",
                  "copy_to" : [
                    "full_name"
                  ]
                },
                "last" : {
                  "type" : "text",
                  "copy_to" : [
                    "full_name"
                  ]
                }
              }
            }
          }
        }
      }
    }
    

    三、Search Template

    • 数据请看数据准备
    # 删除之前的模板[如果有的话]
    DELETE /_scripts/template_tmdb_movies
    
    # 创建搜索模板
    POST /_scripts/template_tmdb_movies
    {
      "script": {
        "lang": "mustache",
        "source": {
          "_source": [
            "title", "overview"
            ],
            "size": "{{s}}",
            "query": {
              "multi_match": {
                "query": "{{q}}",
                "fields": ["title", "overview"]
              }
            }
        }
      }
    }
    
    • 查询索引为tmdb_movies,使用模板template_tmdb_movies
    GET tmdb_movies/_search/template
    {
      "id": "template_tmdb_movies",
      "params": {
        "s": 10,
        "q": "Avatar"
      }
    }
    

    3. 大功告成

    相关文章

      网友评论

          本文标题:Elasticsearch 7.x 深入【6】Template

          本文链接:https://www.haomeiwen.com/subject/ygdjghtx.html