ES的索引库操作
我们要向es中存储数据,必须先创建“库”和“表”。
- 
type:字段数据类型,常见的简单类型有: - 
字符串:text(可分词的文本)、keyword(精确值,例如:品牌、国家、ip地址) 
- 
数值:long、integer、short、byte、double、float、 
- 
布尔:boolean 
- 
日期:date 
- 
对象:object 
 
- 
- 
index:是否创建索引,默认为true 
- 
analyzer:使用哪种分词器 
- 
properties:该字段的子字段 
例如下面的json文档:
{ "age": 21, "weight": 52.1, "isMarried": false, "info": "黑马程序员Java讲师", "email": "zy@itcast.cn", "score": [99.1, 99.5, 98.9], "name": { "firstName": "云", "lastName": "赵" } }
对应的每个字段映射(mapping):
- 
- 
weight:类型为float;参与搜索,因此需要index为true;无需分词器 
- 
isMarried:类型为boolean;参与搜索,因此需要index为true;无需分词器 
- 
info:类型为字符串,需要分词,因此是text;参与搜索,因此需要index为true;分词器可以用ik_smart 
- 
email:类型为字符串,但是不需要分词,因此是keyword;不参与搜索,因此需要index为false;无需分词器 
- 
score:虽然是数组,但是我们只看元素的类型,类型为float;参与搜索,因此需要index为true;无需分词器 
- 
name:类型为object,需要定义多个子属性 - 
name.firstName;类型为字符串,但是不需要分词,因此是keyword;参与搜索,因此需要index为true;无需分词器 
- 
name.lastName;类型为字符串,但是不需要分词,因此是keyword;参与搜索,因此需要index为true;无需分词器 
 
- 
- 
请求方式:PUT 
- 
请求路径:/索引库名,可以自定义 
- 
请求参数:mapping映射 
格式:
PUT /索引库名称 { "mappings": { "properties": { "字段名":{ "type": "text", "analyzer": "ik_smart" }, "字段名2":{ "type": "keyword", "index": "false" }, "字段名3":{ "properties": { "子字段": { "type": "keyword" } } }, // ...略 } } }
PUT /heima { "mappings": { "properties": { "info":{ "type": "text", "analyzer": "ik_smart" }, "email":{ "type": "keyword", "index": "falsae" }, "name":{ "properties": { "firstName": { "type": "keyword" } } }, // ... 略 } } }
- 
请求方式:GET 
- 
请求路径:/索引库名 
- 
请求参数:无 
格式: GET /索引库名
虽然无法修改mapping中已有的字段,但是却允许添加新的字段到mapping中,因为不会对倒排索引产生影响。
PUT /索引库名/_mapping { "properties": { "新字段名":{ "type": "integer" } } }

- 
请求方式:DELETE 
- 
请求路径:/索引库名 
- 
请求参数:无 
- 
创建索引库:PUT /索引库名 
- 
查询索引库:GET /索引库名 
- 
删除索引库:DELETE /索引库名 
- 
添加字段:PUT /索引库名/_mapping 
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号