ELASTICSEARCH的mapping设置
目录
6.将elasticsearch的文件夹名称修改成analysis-ik
1.plugins目录下的ik分词器中config文件夹中找到IKAnalyzer.cfg.xml
10.通过mapping设置将ik_max_word作为字符串类型的分词器使用
3.在index05中添加一个article类型的自定义mapping结构
4.index05的article类型中新增几个document
5.验证是否使用了ik分词器将title中java编程思想分词计算为{java}{编程}{思想}
一.mapping映射结构
1.1动态映射
在新增任何数据之前的空索引中,动态mapping没有配置,空的内容
1.添加一个新的index03
2.查询mapping 状态
- curl -XGET http://10.42.60.249:9200/index03/_mapping
返回结果
{"index03":{"mappings":{}}}
- 一旦添加任何数据到索引中,mapping结构会根据你的数据类型
- {
- "index03": {
- "mappings": {
- "article": { //对article类型定义的映射配置
- "properties": { //mapping中的具体属性
- "content": { //content域的设置
- "type": "text", //字符串类型,并且根据text定义分词计算
- "fields": { //对一个域的属性做扩展
- "keyword": { //查询可用域
- "type": "keyword", //keyword类型相当于lucene底层使用的StringField类型, 整体也可以作为一词项使用
- "ignore_above": 256//作为整体使用时,字符串超过256字节,keyword类型就不生效了
- }
- }
- },
- "id": {
- "type": "text",
- "fields": {
- "keyword": {
- "type": "keyword",
- "ignore_above": 256
- }
- }
- },
- "title": {
- "type": "text",
- "fields": {
- "keyword": {
- "type": "keyword",
- "ignore_above": 256
- }
- }
- }
- }
- }
- }
- }
- }
- 在index03中添加一个新的域,给定一个值不是字符串是整数"product_id":1
- {
- "index03": {
- "mappings": {
- "article": {
- "properties": {
- "content": {
- "type": "text",
- "fields": {
- "keyword": {
- "type": "keyword",
- "ignore_above": 256
- }
- }
- },
- "id": {
- "type": "text",
- "fields": {
- "keyword": {
- "type": "keyword",
- "ignore_above": 256
- }
- }
- },
- "product_id": {
- "type": "long"
- },
- "title": {
- "type": "text",
- "fields": {
- "keyword": {
- "type": "keyword",
- "ignore_above": 256
- }
- }
- }
- }
- }
- }
- }
- }
对于某个索引,中某个类型中的某个域属性,一旦动态映射完成,无法修改;添加数据之前提前将mapping做好
2.静态手动映射
新增一个index04,新增数据之前,将其对应artilce类型中的content只定义为text,id定义为"integer"/"int",title定义为"text",imgUrl定义为"keyword"
- #curl -XPUT http://10.9.151.60:9200/index04
新建完自定义的mapping重新获取index04的内容
- {
- "index04": {
- "mappings": {
- "book": {
- "properties": {
- "content": {
- "type": "text"
- },
- "id": {
- "type": "integer"
- },
- "imgUrl": {
- "type": "keyword"
- },
- "title": {
- "type": "text"
- }
- }
- }
- }
- }
- }
3.对比数据库
mapping结构设置,与数据库中定义一个表格的结构(scheming),是一样的意义
4.对象与json的转化
对象和json的对应关系:
1.一个普通类的对象
class User {
private String id;
private Integer age;
}
user对象对应json字符串
{"id":"uuid1","age":18}
{
"id": "uuid1",
"age": 18
}
2.稍微复杂一些的对象
class Order{
private String order_id;
private OrderShipping shipping;
private List<OrderItem> orderItems;
}
class OrderShipping{
private String receiver;
private String address;
}
order对象对应的json
{"order_id":"uuid","shipping":{"receiver":"张三","address":"beijing"}}
{
"order_id": "uuid",
"shipping": {
"receiver": "张三",
"address": "北京"
},
"orderItems":[{"":"","":""},{"":"","":""},{"":"","":""}]
}
3.之前JSON数据转换为Java对象
{
"index04": {
"mappings": {
"book": {
"properties": {
"content": {
"type": "text"
},
"id": {
"type": "integer"
},
"imgUrl": {
"type": "keyword"
},
"title": {
"type": "text"
}
}
}
}
}
}
转换Java对象
第一层
class Object1{
private Object2 index04;
}
- {"index04":{Object2的json}}
第二层
class Object2{
private Object3 mappings;
}
- {"mappings":{object3的json}}
第三层
class Object3{
private Object4 book;
}
- {"book":{object4的json}}
第四层
class Object4 {
private Object5 properties;
}
- {"properties":{object5的json}}
第五层
class Object5{
private Object6 content;
private Object6 title;l
private Object6 id;
private Object6 imgUrl;
}
- {"content":{object6的json},"title":{object6的json},"id":{object6的json},"imgUrl":{object6的json}}
第六层
class Object6{
private String type;
}
- {"type":"text/integer"}
二.ik分词器插件引入到ELASTICSEARCH
1.文件拷贝
2.安装依赖(已有不要安装)
3.解压
4.删除安装包
将安装包一定要删除,es的启动会加载plugins文件夹下内容,一旦发现不认识的zip包,启动报错
5.进入到Config文件夹
6.将elasticsearch的文件夹名称修改成analysis-ik
7.重新启动es加载plugins中的ik分词器
我的之前报错,内存不足,直接关闭云主机,不惯毛病,
具体错误,可以CSDN 不过好多博客写全是废话,屁都不着边,看也白看
8.访问es提供的接口,实现不同的分词器使用
http://10.42.60.249:9200/index01/_analyze?analyzer=ik_max_word&text=中华人民共和国
- index01:一个存在的索引
- _analyze:对分词的测试
参数:analyzer=ik_max_word,就是ik分词器的名称
text=中华人民共和国,计算分词的测试字符串
9.配置扩展和停用词典
1.plugins目录下的ik分词器中config文件夹中找到IKAnalyzer.cfg.xml
2.没找到,去上传一份
3.找错文件夹 上一步可以不用做
4.配置扩展和停用
5.生成对应的词典们
- ext.dic:华人民
- stopword.dic:人民
6.输出结果
{
"tokens": [{
"token": "中华人民共和国",
"start_offset": 0,
"end_offset": 7,
"type": "CN_WORD",
"position": 0
}, {
"token": "中华人民",
"start_offset": 0,
"end_offset": 4,
"type": "CN_WORD",
"position": 1
}, {
"token": "中华",
"start_offset": 0,
"end_offset": 2,
"type": "CN_WORD",
"position": 2
}, {
"token": "华人民",
"start_offset": 1,
"end_offset": 4,
"type": "CN_WORD",
"position": 3
}, {
"token": "华人",
"start_offset": 1,
"end_offset": 3,
"type": "CN_WORD",
"position": 4
}, {
"token": "人民共和国",
"start_offset": 2,
"end_offset": 7,
"type": "CN_WORD",
"position": 5
}, {
"token": "共和国",
"start_offset": 4,
"end_offset": 7,
"type": "CN_WORD",
"position": 6
}, {
"token": "共和",
"start_offset": 4,
"end_offset": 6,
"type": "CN_WORD",
"position": 7
}, {
"token": "国",
"start_offset": 6,
"end_offset": 7,
"type": "CN_CHAR",
"position": 8
}]
}
10.通过mapping设置将ik_max_word作为字符串类型的分词器使用
1.index05中,定义article
- {
- "properties": {
- "content": {
- "type": "text",
- "analyzer":"ik_max_word"
- },
- "title": {
- "type": "text",
- "analyzer":"ik_max_word"
- },
- "id": {
- "type": "integer"
- },
- "imgUrl": {
- "type": "keyword"
- }
- }
- }
2.新建一个index05
3.在index05中添加一个article类型的自定义mapping结构
curl -XPUT http://10.42.60.249:9200/index05/_mapping/article -d '{"properties":{"content":{"type":"text","analyzer":"ik_max_word"},"title":{"type":"text","analyzer":"ik_max_word"},"id":{"type":"integer"},"imgUrl":{"type":"keyword"}}}'
4.index05的article类型中新增几个document
curl -XPUT -d '{"id":1,"title":"java编程思想","content":"中华人民共和国"}' http://10.42.60.249:9200/index05/article/1
5.验证是否使用了ik分词器将title中java编程思想分词计算为{java}{编程}{思想}
curl -XGET http://10.42.60.249:9200/index05/_search -d '{"query":{"term":{"title":"编程"}}}'
IK分词器安装成功