Elasticsearch索引别名、Filtered索引别名、Template

在使用elasticsearch的时候,常常会遇到须要淘汰掉历史数据的场景。html

为了方便数据淘汰,并使得数据管理更加灵活,咱们常常会以时间为粒度创建索引,例如:app

  • 每月创建一个索引:monthly-20170九、monthly-2017十、monthly-201711
  • 天天创建一个索引:daily-2017101五、daily-2017101六、daily-2017101七、daily-20171018

当不须要再继续使用历史数据的时候,咱们就能够将索引删除,释放资源。curl

为了很好的支撑这个场景,须要使用到Elasticsearch里的两个东西,索引别名和Template。elasticsearch

  • 索引别名:创建索引对外的统一视图

例如,若是创建了上述相似的索引时间序列,在查询的时候以wildcards的方式指定索引,例如index=monthly-*,或者index=daily-201710*。固然也可使用索引别名index=monthly。ide

  • Template:修改创建索引的默认配置

例如,你不想承担按期去维护索引的风险和工做量,能够在插入数据时自动建立索引,Template能够提供自动建立索引时候的默认配置。ui

下面详细解释一下。url

一、索引别名code

一个索引别名就比如一个快捷方式(Shortcut)或一个符号连接(Symbolic Link),索引别名能够指向一个或者多个索引,能够在任何须要索引名的API中使用。使用别名能够给咱们很是多的灵活性。它可以让咱们:htm

  • 在一个运行的集群中透明地从一个索引切换到另外一个索引
  • 让多个索引造成一个组,好比last_three_months
  • 为一个索引中的一部分文档建立一个视图(View)

如何建立索引别名呢?索引

1)建立索引

我这里建立audit-2017十、audit-201711两个索引

curl -XPOST "http://10.93.21.21:8049/kangaroo-201710?pretty"
curl -XPOST "http://10.93.21.21:8049/kangaroo-201711?pretty"

若是安装了head,你能够在可视化页面看到

从索引信息能够看到,咱们没有配置mapping和alias,shards和replicas也使用的默认值。

2)创建索引别名

curl -XPOST 'http://10.93.21.21:8049/_aliases' -d '
{
    "actions": [
        {"add": {"index": "kangaroo-201710", "alias": "kangaroo"}},
        {"add": {"index": "kangaroo-201711", "alias": "kangaroo"}} 
    ]
}'

这样就对kangaroo-201710和kangaroo-201711创建了索引别名kangaroo,再看head可视化

能够看到索引别名已经创建。

3)注意

写:不能直接对索引别名进行写入。因此在写数据的时候,要直接使用普通索引。

读:查询,对索引别名进行查询,查询会透明的下发到别名下挂的全部索引执行,设置的路由也会随之下发。

二、带filtered的索引别名

对于同一个索引,例如zoo,咱们如何给不一样人看到不一样的数据,即,所谓的多租户。

假设索引zoo的数据有个字段是group,group字段记录了该数据是那个“租户”的。多租户之间的数据应该是不可见的。

咱们模拟一下这个场景

1)建立索引zoo

curl -XPOST "http://10.93.21.21:8049/zoo?pretty"

2)设置mappings

curl -XPOST "http://10.93.21.21:8049/zoo/animal/_mapping?pretty" -d '
{ 
    "animal": {
        "properties": {
            "name": {"type": "string", index: "not_analyzed"},
            "group": {"type": "string", index: "not_analyzed"}
        }
    }
}'

3)设置带filter的别名

curl -XPOST "http://10.93.21.21:8049/_aliases?pretty" -d '
{
  "actions": [
    {
      "add": {
        "index": "zoo",
        "alias": "zoo_animal_vegetarian",
        "filter":{
            "term":{
                "group":"vegetarian"
            }
        }
      }
    },
    {
      "add": {
        "index": "zoo",
        "alias": "zoo_animal_carnivorous",
        "filter":{
            "term":{
                "group":"carnivorous"
            }
        }
      }
    }
  ]
}'

经过head看一下

咱们索引两条数据进去

老虎-肉食

curl -XPUT 'http://10.93.21.21:8049/zoo/animal/1' -d '{
    "name" : "tiger",
    "group" : "carnivorous"
}'

兔子-素食

curl -XPUT 'http://10.93.21.21:8049/zoo/animal/2' -d '{
    "name" : "rabbit",
    "group" : "vegetarian"
}'

使用带filter的索引查一下

素食的只有兔子

curl -XGET "http://10.93.21.21:8049/zoo_animal_vegetarian/_search?pretty"
{
  "took" : 32,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "zoo",
      "_type" : "animal",
      "_id" : "2",
      "_score" : 1.0,
      "_source":{
    "name" : "rabbit",
    "group" : "vegetarian"
}
    } ]
  }
}

肉食的只有老虎

curl -XGET "http://10.93.21.21:8049/zoo_animal_carnivorous/_search?pretty"
{
  "took" : 33,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "zoo",
      "_type" : "animal",
      "_id" : "1",
      "_score" : 1.0,
      "_source":{
    "name" : "tiger",
    "group" : "carnivorous"
}
    } ]
  }
}

当你创建索引时间序列的时候,遇到的问题是,须要不断的创建新索引,例如到了11月份,你能够须要新建kangaroo-201711这个索引。

固然,若是不建立索引,直接写入数据的话,ES会为你分析你写入的document的字段类型,并使用默认配置创建索引。

可是默认配置可能并非你想要的。例如ES对string类型默认是分析的,即,对string类型会进行分词,可是你的数据中可能有一些string类型的字段不但愿被分析。

那么怎么修改默认配置呢?能够建立一个template。

三、Template

template能够修改索引的默认配置。咱们如下面这个template为例说明一下。

1)咱们创建了一个template名称为kangaroo_template

2)"template": "kangaroo*",表示对于全部以kangaroo*开头的索引,默认配置使用template中的配置。

3)"settings","mappings","aliases",能够修改这些类型的默认配置

4)禁用了_source,对name字段设置string类型且不分析,索引别名设置为kangaroo

curl -XPUT "http://10.93.21.21:8049/_template/kangaroo_template?pretty" -d '{
  "template": "kangaroo*",
  "settings": {
    "number_of_shards": 10
  },
  "mappings": {
    "data": {
      "_source": {
        "enabled": false
      },
      "properties": {
        "name": {
          "type": "string",
          "index": "not_analyzed"
        },
        "id": {
          "type": "long"
        }
      }
    }
  },
  "aliases": {"kangaroo":{}}
}'

执行生效后,看一下template生效的内容,这里注意有一个"order"字段,该字段跟多template合并有关,后面咱们会讲。

curl -XGET "http://10.93.21.21:8049/_template/kangaroo_template?pretty"
{
  "kangaroo_template" : {
    "order" : 0,
    "template" : "kangaroo*",
    "settings" : {
      "index" : {
        "number_of_shards" : "10"
      }
    },
    "mappings" : {
      "data" : {
        "_source" : {
          "enabled" : false
        },
        "properties" : {
          "name" : {
            "index" : "not_analyzed",
            "type" : "string"
          },
          "id" : {
            "type" : "long"
          }
        }
      }
    },
    "aliases" : {
      "kangaroo" : { }
    }
  }
}

咱们能够向一个不存在的索引写入数据,这个操做会使用默认配置,若是索引名称命中template中的规则,就会使用template的配置建立索引。

这里咱们向kangaroo-201712写入数据,会命中以前建立的kangaroo_template。

curl -XPUT 'http://10.93.21.21:8049/kangaroo-201712/data/1' -d '{
    "name" : "yang",
    "id" : "1001",
    "weight" : "70 kg"
}'

经过head看一下,能够看到,索引别名已经创建,分片数=10,source禁用生效,name不分析。这就是咱们想要的结果。

多个template配置的合并

这个场景是这样的,一个索引命中了多个template配置,例如:有两个template配置分别为:a*, ab*,那么若是有一个索引名字是abc,就会命中了两个template,这时候会怎么样呢?

配置会merge,merge的法则能够参见官方文档,简单来讲,就是跟order值有关,较小order值的配置会先生效,较大order值的配置会继而覆盖。

相关文章
相关标签/搜索