elasticsearch 索引模板组件的创建

创建 mapping 模板 组件

PUT _component_template/student_template_mapping_001
{
  "template": {
    "mappings": {
      "properties": {
        "id": {
          "type": "long"
        },
        "name": {
          "type": "text",
          "fields": {
            "key": {
              "type": "keyword"
            }
          }
        },
        "year": {
          "type": "integer",
          "fields": {
            "key": {
              "type": "keyword"
            }
          }
        }
      }
    }
  }
}

 创建 setting 模板组件

PUT _component_template/student_template_setting_001
{
  "template": {
    "settings": {
      "number_of_shards": 3
    }
  }
}

创建 alias 模板组件 

PUT _component_template/student_template_alias_001
{
  "template": {
    "aliases": {
      "student": {
        "filter": {
          "term": {
            "year": "1"
          }
        }
      }
    }
  }
}

创建索引模板

PUT _index_template/student_template_002
{
  "index_patterns": [
    "student-*"
  ],
  "composed_of": [
    "student_template_setting_001",
    "student_template_mapping_001",
    "student_template_alias_001"
    ]
}

查看索引模板组件

GET _component_template

查看索引模板

添加数据 


PUT student-001/_doc/1?routing=1
{
  "id": 1,
  "name": "student-a",
  "year": 1
}

PUT student-001/_doc/2?routing=2
{
  "id": 2,
  "name": "student-b",
  "year": 2
}

PUT student-001/_doc/3?routing=3
{
  "id": 3,
  "name": "student-c",
  "year": 3
}
PUT student-001/_doc/4?routing=3
{
  "id": 4,
  "name": "student-c",
  "year": 1
}

成功查询到数据:

GET student_year_1/_search

 查询结果

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "student-001",
        "_type" : "_doc",
        "_id" : "6-nnM3gB_jopoKW0Srd_",
        "_score" : 1.0,
        "_source" : {
          "id" : 5,
          "name" : "student-5",
          "year" : 1
        }
      },
      {
        "_index" : "student-001",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 1.0,
        "_routing" : "3",
        "_source" : {
          "id" : 4,
          "name" : "student-c",
          "year" : 1
        }
      },
      {
        "_index" : "student-001",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_routing" : "1",
        "_source" : {
          "id" : 1,
          "name" : "student-a",
          "year" : 1
        }
      }
    ]
  }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值