Elasticsearch --- (十三)索引管理

本文详细介绍了Elasticsearch的索引管理,包括创建、修改和删除索引,探讨了分词器的定制,深入解析了type底层数据结构,对mapping root object进行了深入剖析,讲解了如何定制dynamic mapping策略,以及如何利用scroll滚动搜索和alias实现零停机reindex。
摘要由CSDN通过智能技术生成

目录

1、创建、修改以及删除索引

(1)手动创建索引语法

(2)修改索引

(3)删除索引

2、 修改分词器以及定义自己的分词器

(1)默认分词器   standard

(2)修改分词器的设置

(3)定制化自己的分词器

3、深入探秘type底层数据结构

(1)理论知识

(2)案例实践

(3)最后总结

4、mapping root object深入剖析

(1)root object:某个type对应的mapping json

(2)properties:包含type、index、analyzer

(3)_source

(4)_all

(5)标识性metadata:_index、_type、_id

5、定制化自己的dynamic mapping策略

(1)定制dynamic策略

(2)定制dynamic mapping策略

a、date_detection

b、定制自己的动态映射模板dynamic mapping template(type level)

c、定制自己的default mapping template(index level)

6、基于scroll滚动搜索和alias别名实现zero downtime(零停机) reindex

(1)重建索引


1、创建、修改以及删除索引

(1)手动创建索引语法

PUT /my_index
{
  "settings": {... any settings...},
  "mappings": {
    "type_one":{... any mappings...},
    "type_two":{... any mappings...},
    ...
  }
}

PUT /my_index
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0
  },
  "mappings": {
    "my_type":{
      "properties": {
        "my_field":{
          "type":"text"
        }
      }
    }
  }
}

(2)修改索引

//---------修改replica 数量
PUT /my_index/_settings
{
  "number_of_replicas": 1
}

(3)删除索引

  • DELETE  /my_index
  • DELETE  /index_one,index_two
  • DELETE  /index_*
  • DELETE  /_all             -----删除所有索引
  • elasticsearch.yml 中配置action.destructive_requires_name:true,就不能用 DELETE /_all 这种方式删除全部索引

2、 修改分词器以及定义自己的分词器

(1)默认分词器   standard

  • standard  tokenizer:以单词边界进行切分
  • standard  token  filter:什么都不做
  • lowercase  token  filter:将所有字母转换为小写
  • stop token filter(默认被禁用):移除停用词,比如a    the    it 等等

(2)修改分词器的设置

启用english 停用词 token filter

PUT /my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "es_std":{
          "type":"standard",
          "stopwords":"_englist_"
        }
      }
    }
  }
}

修改后查看分词效果

//-----------用standard ,被分词成  a , dog , is , in , the , house
GET /my_index/_analyze
{
  "analyzer":"standard",
  "text":"a dog is in the house"
}


//----------用es_std, 被分词成   dog , house
GET /my_index/_analyze
{
  "analyzer": "es_std",
  "text": "a dog is in the house"
}

(3)定制化自己的分词器

PUT /my_index
{
  "settings": {
    "analysis": {
      "char_filter": {
        "&_to_and":{
          "type":"mapping",
          "mappings":["&=>and"]     //&转化成and
        }
      },
      "filter": {
        "my_stopwords":{
          "type":"stop",
          "stopwords":["the","a"]     //忽略the a
        }
      },
      "analyzer": {
        "my_analyzer":{
          "type":"custom",
          "char_filter":["html_strip","&_to_and"],    //忽略html标签,&转成and
          "tokenizer":"standard",
          "filter":["lowercase","my_stopwords"]
        }
      }
    }
  }
}



//--------------测试,分词结果:tomandjerry,are, friend,in, house,haha
GET /my_index/_analyze
{
  "analyzer": "my_analyzer",
  "text":"tom&jerry are a friend in the house,<a>,HAHA!!"
}

在type中应用自己的分词器

PUT /my_index/_mapping/my_type
{
  "properties": {
    "content":{
      "type": "text",
      "analyzer": "my_analyzer"
    }
  }
}

3、深入探秘type底层数据结构

(1)理论知识

  • type,是一个index中用来区分类似的数据的,类似的数据,但是可能有不同的fields,而且有不同的属性来控制索引建立、分词器
  • field的value,在底层的Lucene中建立索引的时候,全部是opaque(不透明)bytes类型,即:不区分类型的。
  • Lucene是没有type的概念的,在document中,实际上将type作为一个document的field来存储,即_type,es通过_type来进行type的过滤和筛选。
  • 一个index中的多个type,实际上是放在一起存储的,因此一个index下,不能有多个type重名,二类型或者其他设置不同的,因为那样是无法处理的。

(2)案例实践

(1)插入两条数据

PUT goods_index/electronic_goods/1
{
  "name": "geli kongtiao",
  "price": 1999.0,
  "service_period": "one year"
}

PUT goods_index/eat_goods/2
{
  "name": "aozhou dalongxia",
  "price": 199.0,
  "eat_period": "one week"
}

索引名称为goods_index,在该索引下面分别有两个type:electronic_goods和eat_goods

我们来看下索引对应的映射

(2)查看mapping

GET /goods_index/_mapping

{
  "goods_index": {
    "mappings": {
      "electronic_goods": {
        "properties": {
          "name": {
       
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值