elasticsearch Mapping的创建

elasticsearch Mapping的创建

 

一、Mapping介绍

 Elasticsearch  中, Mapping  是什么?

mapping   Elasticsearch  中的作用就是约束。

1.数据类型声明

它类似于静态语言中的数据类型声明,比如声明一个字段为String, 以后这个变量都只能存储String类型的数据。同样的, 一个number类型的 mapping  字段只能存储number类型的数据。

2.Mapping它定义了 Type 的属性。

 
  1. "_ttl": {"enabled": false

表示 ttl关闭,其实ttl默认就是关闭。

3.指定分词器。

 
  1. "id": {
  2. "index": "not_analyzed",
  3. "type": "string"
  4. }

指定字段 id不分词,并且类型为 string

4.…………

二、创建Mapping

1.下面介绍一下HTTP的创建方式。我一般用Java 创建方式。

PUT http://123.123.123.123:9200/index/type/
{
  "settings": {
     //设置10个分片,理解为类似数据库中的表分区中一个个分区的概念,不知道是否妥当
     "number_of_shards": 10
  }, 
  "mappings": {
    "trades": {
      "_id": {
        "path": "id"
      },
      "properties": {
        "id": {
         "type": "integer",
        //id:自增数字
        //要求:查询
         "store" : true
        },
        "name": { //名称
         "type": "string"
        },
        "brand": { //品牌: PG,P&G,宝洁集团,宝洁股份,联想集团,联想电脑等 
          "type": "string"
        },
        "orderNo": { //订单号 :如ATTS000928732
          "type": "string",
          "index":  "not_analyzed"
        },
        "description": {
              //描述: 2015款玫瑰香型强生婴儿沐浴露,550ml,包邮
              "type": "string""sort": true
        },
        "date": {
          "type": "date"
        },
        "city": {
          "type": "string"
        },
        "qty": {// index分词无效
            "type": "float"
        },
        "price": {
              //价格: float index无效
             "type": "float"
        }
      }
    }
  }
}

上面是从其他地方抄过来的。因为我不用这种方式。

2.Java方式创建。

构建 Mapping  

package com.sojson.core.elasticsearch.mapping;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import java.io.IOException;

import org.elasticsearch.common.xcontent.XContentBuilder;

public class ZhidaoMapping {

    public static XContentBuilder getMapping(){
        XContentBuilder mapping = null;
        try {
            mapping = jsonBuilder()
            .startObject()
            //开启倒计时功能
            .startObject("_ttl")
                .field("enabled",false)
                .endObject()
                    .startObject("properties")
                    .startObject("title")
                        .field("type","string")
                    .endObject()
                    .startObject("question")
                        .field("type","string")
                        .field("index","not_analyzed")
                    .endObject()
                    .startObject("answer")
                        .field("type","string")
                        .field("index","not_analyzed")
                    .endObject()
                    .startObject("category")
                        .field("type","string")
                        .field("index","not_analyzed")
                    .endObject()
                    .startObject("author")
                        .field("type","string")
                        .field("index","not_analyzed")
                    .endObject()
                    .startObject("date")
                        .field("type","string")
                        .field("index","not_analyzed")
                    .endObject()
                    .startObject("answer_author")
                        .field("type","string")
                        .field("index","not_analyzed")
                    .endObject()
                    .startObject("answer_date")
                        .field("type","string")
                        .field("index","not_analyzed")
                    .endObject()
                    .startObject("description")
                        .field("type","string")
                        .field("index","not_analyzed")
                    .endObject()
                    .startObject("keywords")
                        .field("type","string")
                        .field("index","not_analyzed")
                    .endObject()
                    .startObject("read_count")
                        .field("type","integer")
                        .field("index","not_analyzed")
                    .endObject()
                    //关联数据
                    .startObject("list").field("type","object").endObject()
                .endObject()
            .endObject();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        return mapping;
    }
}

创建 Mapping  

public static void createBangMapping(){
    PutMappingRequest mapping = Requests.putMappingRequest(INDEX).type(TYPE).source(ZhidaoMapping.getMapping());
    ESTools.client.admin().indices().putMapping(mapping).actionGet();
    
}

创建的时候,需要 index已经创建才行,要不然会报错。

//构建一个Index(索引)
CreateIndexRequest request = new CreateIndexRequest(INDEX);
ESTools.client.admin().indices().create(request);

创建完毕在 Head  插件里查看或者Get请求。

http://123.123.123.123:9200/index/type/_mapping

得到的结果:

{
    "zhidao_index": {
        "mappings": {
            "zhidao_type": {
                "_ttl": {
                    "enabled": false
                },
                "properties": {
                    "answer": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "answerAuthor": {
                        "type": "string"
                    },
                    "answerDate": {
                        "type": "date",
                        "format": "strict_date_optional_time||epoch_millis"//这里出现了复合类型
                    },
                    "answer_author": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "answer_date": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "author": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "category": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "date": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "description": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "id": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "keywords": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "list": {
                        "type": "object"
                    },
                    "question": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "readCount": {
                        "type": "long"
                    },
                    "read_count": {
                        "type": "integer"
                    },
                    "title": {
                        "type": "string"
                    }
                }
            }
        }
    }
}

Head插件查看

其实 Mapping  ,你接触 Elasticsearch  久一点也就那么回事。我们虽然知道 Elasticsearch  有根据数据识别创建 Mapping  ,但是最好是创建,并且指定分词与否。这样高效一点。

 

麦田实战:针对_head插件页面的操作

//创建主索引

http://localhost:19201/es_test5_1/

 
   

PUT
{
"settings": {
"index": {
"creation_date": "1533786340419",
"analysis": {
"analyzer": {
"ngram_analyzer": {
"tokenizer": "ngram_tokenizer"
}
},
"tokenizer": {
"ngram_tokenizer": {
"token_chars": [
"digit",
"letter",
"symbol",
"punctuation"
],
"min_gram": "1",
"type": "ngram",
"max_gram": "1000"
}
}
},
"number_of_shards": "5",
"number_of_replicas": "1"
}
}
}



//
新增type及对应mapping http://localhost:19201/es_test4/_mapping/product/ PUT { "properties": { "id": { "type": "string" }, "title": { "type": "string" }, "address": { "type": "string", "index": "not_analyzed" }, "publish_data": { "type": "date", "index": "not_analyzed" }, "price": { "type": "double" }, "number": { "type": "integer" } } } 查询index对应的mapping http://localhost:19201/es_test4/_mapping GET type中新增字段以及指定mapping http://localhost:19201/es_test4/_mapping/product/ PUT { "properties": { "xinzengziduan": { "type": "integer" } } }

 麦田测试环境mapping映射(主要应用了ngram_analyzer分词器)

{
"state": "open",
"settings": {
"index": {
"creation_date": "1525160674349",
"analysis": {
"analyzer": {
"ngram_analyzer": {
"tokenizer": "ngram_tokenizer"
}
},
"tokenizer": {
"ngram_tokenizer": {
"token_chars": [
"digit"
,
"letter"
,
"symbol"
,
"punctuation"
],
"min_gram": "1",
"type": "ngram",
"max_gram": "1000"
}
}
},
"number_of_shards": "5",
"number_of_replicas": "1",
"uuid": "Fl8ZB4LmTBmm4ED_8O1DFA",
"version": {
"created": "2040099"
}
}
},
"mappings": {
"sale_house_type": {
"properties": {
"hisFollowCount": {
"type": "integer"
},
"firstPic": {
"type": "string"
},
"buildAreaLable": {
"type": "string"
},
"oldHouseId": {
"type": "string"
},
"defaultSortTime": {
"type": "long"
},
"defaultSort": {
"type": "string"
},
"unitPriceLable": {
"type": "string"
},
"houFiveYearsFlag": {
"type": "integer"
},
"houLocation": {
"index": "not_analyzed",
"type": "string"
},
"quote": {
"type": "double"
},
"roomNum": {
"type": "double"
},
"seeTotal30": {
"type": "integer"
},
"seeTotal30Lable": {
"type": "string"
},
"roomNumId": {
"type": "string"
},
"houTwoYearsFlag": {
"type": "integer"
},
"isAttack": {
"type": "integer"
},
"id": {
"index": "not_analyzed",
"type": "string"
},
"isTodaySee": {
"type": "integer"
},
"tag": {
"type": "string"
},
"foor": {
"type": "string"
},
"houPropertyName": {
"index": "not_analyzed",
"type": "string"
},
"isMtOnline": {
"type": "integer"
},
"roomType": {
"type": "string"
},
"direction": {
"type": "string"
},
"followerAgentId": {
"type": "string"
},
"unitPrice": {
"type": "double"
},
"houPropertyNameQuery": {
"analyzer": "ngram_analyzer",
"type": "string"
},
"unitName": {
"index": "not_analyzed",
"type": "string"
},
"bussinesSts": {
"type": "integer"
},
"isKey": {
"type": "integer"
},
"addTbTime": {
"type": "long"
},
"buildArea": {
"type": "double"
},
"roomName": {
"index": "not_analyzed",
"type": "string"
},
"buildingName": {
"index": "not_analyzed",
"type": "string"
},
"businessManageName": {
"analyzer": "ngram_analyzer",
"type": "string"
},
"areaId": {
"type": "string"
},
"isManagerRecommend": {
"type": "integer"
},
"ownerUniqFlag": {
"type": "integer"
},
"isBigManagerRecommend": {
"type": "integer"
},
"realHouseFlag": {
"type": "integer"
},
"quoteLable": {
"type": "string"
},
"propertyPeriodId": {
"type": "string"
},
"location": {
"type": "geo_point"
},
"businessManageId": {
"type": "string"
},
"houseBaseId": {
"index": "not_analyzed",
"type": "string"
}
}
}
},
"aliases": [ ]
}
"digit" ---数字
,
"letter"---字母
,
"symbol"---符号
,
"punctuation"---标点符号
"min_gram": "1",---最小分割长度
"type": "ngram",
"max_gram": "1000"---最大分割长度
 

获取index为library,type为books的映射

GET /libraryyry/_mapping/books

获取集群内所有的映射信息

GET /_all/_mapping/

获取这个集群内某两个或多个type映射信息(books和bank_account映射信息)

GET /_all/_mapping/books,bank_account

删除映射

DELETE /libraryry/books

DELETE /libraryry/books/_mapping                #删除books的映射

DELETE /libraryry/_mapping/books,bank_acount  #删除多个映射

无法修改已经存在的mapping映射

复制代码
1.如果要推到现有的映射,你得重新建立一个索引.然后重新定义映射
2.然后把之前索引里的数据导入到新的索引里
-------具体方法------
1.给现有的索引定义一个别名,并且把现有的索引指向这个别名,运行步骤2
2.运行: PUT /现有索引/_alias/别名A
3.新创建一个索引,定义好最新的映射
4.将别名指向新的索引.并且取消之前索引的执行,运行步骤5
5.运行: POST /_aliases
        {
            "actions":[
                {"remove"    :    {    "index":    "现有索引名".    "alias":"别名A"    }}.
                {"add"        :    {    "index":    "新建索引名",    "alias":"别名A"    }}
            ]
        }
注意:通过这几个步骤就实现了索引的平滑过渡,并且是零停机
复制代码

 

转载于:https://www.cnblogs.com/pypua/articles/9417892.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值