Elasticsearch 7.x 深入【6】Template

2020-05-03  本文已影响0人  孙瑞锴

1. 借鉴

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

2. 开始

一、Index Template

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

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

PUT /_template/test_template_1
{
  "index_patterns": ["test_*"],
  "order": 0,
  "version": 1,
  "mappings": {
    "date_detection": true,
    "numeric_detection": true
  }
}
PUT /test_mytemp/_doc/1
{
  "createTime": "2020-05-03 10:00:00",
  "count": 10
}
GET /test_mytemp/_mapping
{
  "test_mytemp" : {
    "mappings" : {
      "date_detection" : true,
      "numeric_detection" : true,
      "properties" : {
        "count" : {
          "type" : "long"
        },
        "createTime" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

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

如何删除呢?

二、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"
        }
      }
    }]
  }
}
PUT /my_dy_index/_doc/1
{
  "name": {
    "first": "sun",
    "last": "ruikai"
  },
  "isVip": "true"
}

GET /my_dy_index/_mapping
{
  "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"]
          }
        }
    }
  }
}
GET tmdb_movies/_search/template
{
  "id": "template_tmdb_movies",
  "params": {
    "s": 10,
    "q": "Avatar"
  }
}

3. 大功告成

上一篇下一篇

猜你喜欢

热点阅读