Elasticsearch---索引管理、基于scroll+bulk的索引重建

创建索引的语法

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"
        }
      }
    }
  }
}

添加索引(索引一旦建立,不能修改)

PUT /my_index/_settings
{
  "number_of_replicas": 1
}
PUT /my_index/_mapping/my_type
{
  "properties": {
    "my_field":{
      "type": "string"
    }
  }
}

删除索引

DELETE /my_index
DELETE /index_one,index_two
DELETE /index_*
DELETE /_all    //要想这样删除,需要修改config/elasticsearch.yml 中action.destructive_requires_name: false

分词器的修改与定制

  • 修改分词器设置

默认分词器是standard

  • standard tokenizer:已单词边界进行切分
  • standard token filter:什么都不做
  • lowercase token filter:将所有字符转换为小写
  • stop token filter(默认禁用):移除停用词
PUT /my_index
{
 "settings": {
   "analysis": {
     "analyzer": {
       "my_analyzer_std":{
         "type":"standard",
         "stopwords":"_english_"
       }
     }
   }
 }
}
//测试
GET /my_index/_analyze
{
 "analyzer": "my_analyzer_std",
 "text": "hello,you are the good boy"    // are the会被去掉
}
  • 定制自己的分词器
PUT /my_index
{
  "settings": {
    "analysis": {
      "char_filter":{
        "&_to_and":{
          "type":"mapping",
          "mappings":["&=> and"]
        }
      },
      "filter": {
        "my_stopwords":{
          "type":"stop",
          "stopwords":["the","are"]
        }
      },
      "analyzer": {
        "my_analyzer":{
          "type":"custom",
          "char_filter":["html_strip","&_to_and"],
          "tokenizer":"standard",
          "filter":["lowercase","my_stopwords"]
        }
      }
    }
  }
}
//测试
GET /my_index/_analyze
{
  "analyzer": "my_analyzer",
  "text":"tom & jery,they are the good friend,<a>CLICK ME</a>"
}

type底层的数据结构

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

//设置_mapping
PUT /ecommerce
{
 "mappings": {
   "elactronic_goods":{
     "properties": {
       "name":{
         "type": "string"
       },
       "price":{
         "type":"double"
       },
       "service_period":{
         "type":"string"
       }
     }
   },
   "fresh_goods":{
     "properties": {
       "name":{
         "type": "string"
       },
       "price":{
         "type": "double"
       },
       "eat_period":{
         "type":"string"
       }
     }
   }
 }
}
//查询_mapping
GET /ecommerce/_mapping
//存入document
PUT /ecommerce/elactronic_goods/1
{
 "name":"geli kongtiao",
 "price":3999,
 "service_period":"one year"
}
PUT /ecommerce/fresh_goods/1
{
 "name":"da xia",
 "price":99,
 "eat_period":"one week"
}

底层数据结构是这样的

{
   "ecommerce": {
      "mappings": {
        "_type": {
          "type": "string",
          "index": "not_analyzed"
        },
        "name": {
          "type": "string"
        }
        "price": {
          "type": "double"
        }
        "service_period": {
          "type": "string"
        }
        "eat_period": {
          "type": "string"
        }
      }
   }
}
//放入的document是这样的
{
  "_type": "elactronic_goods",
  "name": "geli kongtiao",
  "price": 1999.0,
  "service_period": "one year",
  "eat_period": ""
}
{
  "_type": "fresh_goods",
  "name": "aozhou dalongxia",
  "price": 199.0,
  "service_period": "",
  "eat_period": "one week"
}

所以应该把类似结构的type放在一个index下,这些type应该有多个field是相同的,如果一个index的多个type的field完全不同,那个每条数据会有一大部分的field在底层lucene中是空值,会有严重的性能问题

_mapping root object深入剖析

  • root object
    就是某个type对应的mapping json,包括了properties,metadata(_id,_source,_type),setting(analyzer),其他setting(比如include_in_all)
PUT /my_index
{
  "mappings": {
    "my_type":{
      "properties": {}
    }
  }
}
  • properties
    包含有type,index,analyzer
PUT /my_index/_mapping/my_type
{
  "properties": {
    "title":{
      "type": "text"
    }
  }
}
  • _source
  • 查询的时候,直接可以拿到完整的document,不需要先拿document id,再发送一次请求拿document
  • partial update基于_source实现
  • reindex时,直接基于_source实现,不需要从数据库(或者其他外部存储)查询数据再修改
  • 可以基于_source定制返回field
  • debug query更容易,因为可以直接看到_source

如果不需要上述好处,可以禁用_source

PUT /my_index/_mapping/my_type2
{
  "_source": {"enabled": false}
}
  • _all
    将所有field打包在一起,作为一个_all field,建立索引。没指定任何field进行搜索时,就是使用_all field在搜索。
PUT /my_index/_mapping/my_type3
{
  "_all": {"enabled": false}
}

也可以在field级别设置include_in_all field,设置是否要将field的值包含在_all field中

PUT /my_index/_mapping/my_type4
{
  "properties": {
    "my_field": {
      "type": "text",
      "include_in_all": false
    }
  }
}

dynamic mapping策略

  • 定制策略
  • true:遇到陌生字段,就进行dynamic mapping
  • false:遇到陌生字段,就忽略
  • strict:遇到陌生字段,就报错
PUT /my_index
{
 "mappings": {
   "my_type":{
     "dynamic":"strict",
     "properties": {
       "title":{
         "type": "text"
       },
       "address":{
         "type": "object",
         "dynamic":"true"
       }
     }
   }
 }
}
PUT /my_index/my_type/1
{
 "content":"uuuu",
 "title":"hello world",
 "address":{
   "country":"china",
   "provice":"beiing"
 }
}
//结果
{
 "error": {
   "root_cause": [
     {
       "type": "strict_dynamic_mapping_exception",
       "reason": "mapping set to strict, dynamic introduction of [content] within [my_type] is not allowed"
     }
   ],
   "type": "strict_dynamic_mapping_exception",
   "reason": "mapping set to strict, dynamic introduction of [content] within [my_type] is not allowed"
 },
 "status": 400
}
  • date detection
    默认会按照一定格式识别date,比如yyyy-MM-dd。但是如果某个field先过来一个2017-01-01的值,就会被自动dynamic mapping成date,后面如果再来一个"hello world"之类的值,就会报错。可以手动关闭某个type的date_detection,如果有需要,自己手动指定某个field为date类型。
PUT /my_index/_mapping/my_type
{
  "date_detection": false
}
  • 定制自己的dynamic mapping template(type level)
PUT /my_index
{
  "mappings": {
    "my_type":{
      "dynamic_templates":[
        {
          "en":{
            "match":"*_en",
            "match_mapping_type":"string",
            "mapping":{
              "type":"string",
              "analyzer":"english"
            }
          }
        }
      ]
    }
  }
}
//插入数据
PUT /my_index/my_type/1
{
  "title":"this is my first article"
}
PUT /my_index/my_type/2
{
  "title_en":"this is my first article"
}
//查询
//-------------------------------第一个
//没有匹配到任何的dynamic模板
//默认就是standard分词器,不会过滤停用词,is会进入倒排索引,用is来搜索是可以搜索到的
GET /my_index/my_type/_search
{
  "query": {
    "match": {
      "title": "is"
    }
  }
}
//-------------------------------第二个
//匹配到了dynamic模板,就是english分词器,会过滤停用词,is这种停用词就会被过滤掉,用is来搜索就搜索不到了
GET /my_index/my_type/_search
{
  "query": {
    "match": {
      "title_en": "is"
    }
  }
}
  • 定制自己的default mapping template(index level)
PUT /my_index
{
  "mappings": {
    "_default_":{
      "_all":{
        "enabled":false
      }
    },
   "blog":{
      "_all":{
        "enabled":true
      }
    }
  }
}

基于scroll+bulk的索引重建

一个field的设置是不能被修改的,如果要修改一个field,那么应该重新按照新的mapping,建立一个index,然后将数据批量查询出来,重新用bulk api写入index中
批量查询的时候,建议才用scroll api,并且才用多线程并发的方式来reindex数据,每次scroll就查询指定日期的一段数据,交个一个线程即可

  • 插入模拟数据,但是不小心有些数据时2017-01-01这种日期格式,所以title这种field就被自动映射为date类型,实际上他应该是string类型
PUT /my_index/my_type/1
{
  "title":"2017-01-01"
}
PUT /my_index/my_type/2
{
  "title":"2017-01-02"
}
PUT /my_index/my_type/3
{
  "title":"2017-01-03"
}
  • 然后向索引中加入string类型的title值得时候,会报错
PUT /my_index/my_type/4
{
  "title":"my first article"
}
{
  "error": {
    "root_cause": [
      {
        "type": "mapper_parsing_exception",
        "reason": "failed to parse [title]"
      }
    ],
    "type": "mapper_parsing_exception",
    "reason": "failed to parse [title]",
    "caused_by": {
      "type": "illegal_argument_exception",
      "reason": "Invalid format: \"my first article\""
    }
  },
  "status": 400
}
//查看其mapping
GET /my_index/_mapping/my_type
{
  "my_index": {
    "mappings": {
      "my_type": {
        "properties": {
          "title": {
            "type": "date"
          }
        }
      }
    }
  }
}
  • 此时尝试修改title的类型
PUT /my_index/_mapping/my_type
{
  "properties": {
    "title":{
      "type": "string"
    }
  }
}
//返回结果
{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "mapper [title] of different type, current_type [date], merged_type [text]"
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "mapper [title] of different type, current_type [date], merged_type [text]"
  },
  "status": 400
}
  • 此时,唯一的办法就是进行reindex(重建索引),将旧索引的数据查询出来,再导入新索引
  • 给旧索引起一个别名,这个别名指向旧的索引,如果应用程序在使用,可以用这个别名索引
PUT /my_index/_alias/goods_index
  • 新建一个index,调整其title类型为string
PUT /my_index_new
{
  "mappings": {
    "my_type":{
      "properties": {
        "title":{
          "type": "string"
        }
      }
    }
  }
}
  • 使用scroll api将数据批量查出来
GET /my_index/_search?scroll=1m
{
  "query": {
    "match_all": {}
  },
  "sort":["_doc"],
  "size":1
}
  • 采用bulk api将scroll查询出来的一批数据,批量写入新的索引中
POST /_bulk
{"index":{"_index":"my_index_new","_type":"my_type","_id":"2"}}
{"title":"2017-01-02"}
  • 重复上面的两个步骤,把所有的数据都写入新的索引
GET /_search/scroll
{
  "scroll":"1m",
  "scroll_id":"DnF1ZXJ5VGhlbkZldGNoBQAAAAAAAC6bFlhIb1FOME82U3llb202bER1Zm95VkEAAAAAAAAumBZYSG9RTjBPNlN5ZW9tNmxEdWZveVZBAAAAAAAALpwWWEhvUU4wTzZTeWVvbTZsRHVmb3lWQQAAAAAAAC6ZFlhIb1FOME82U3llb202bER1Zm95VkEAAAAAAAAumhZYSG9RTjBPNlN5ZW9tNmxEdWZveVZB"
}
POST /_bulk
{"index":{"_index":"my_index_new","_type":"my_type","_id":"...."}}
{"title":"..."}
  • 将goods_index alias切换到my_index_new上去,
POST /_aliases
{
  "actions": [
    {
      "remove": {"index":"my_index","alias": "goods_index"}
    },
    {
      "add":{"index":"my_index_new","alias": "goods_index"}
    }
  ]
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,099评论 18 139
  • Neil Zhu,简书ID Not_GOD,University AI 创始人 & Chief Scientist...
    朱小虎XiaohuZhu阅读 13,172评论 0 5
  • 删除索引 用以下的请求来 删除索引: 对一些人来说,能够用单个命令来删除所有数据可能会导致可怕的后果。如果你想要避...
    techhow阅读 1,041评论 0 1
  • 本节讨论在将应用迁移到 elasticsearch 2.0 时需要注意的一些变化。 在 0.90 版本前创建的索引...
    朱小虎XiaohuZhu阅读 1,672评论 2 3
  • 一天,一个博士坐船欣赏风景。 在船上,博士问渔夫:“你会生物吗?”渔夫说不会,博士就说:“那你的生...
    无欲则刚66阅读 243评论 0 0