目录
(1)root object:某个type对应的mapping json
(2)properties:包含type、index、analyzer
(5)标识性metadata:_index、_type、_id
b、定制自己的动态映射模板dynamic mapping template(type level)
c、定制自己的default mapping template(index level)
6、基于scroll滚动搜索和alias别名实现zero downtime(零停机) reindex
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": {