如何显示定义一个Mapping
PUT movies
{
"mappings": {
//define your mappings here
}
}
自定义Mapping的一些建议
-
可以参考API手册,纯手写
-
为了减少输入的工作量,减少出错概率,可以依照以下步骤
-
创建一个临时的index,写入一些样本数据
-
通过访问Mapping API获得该临时文件的动态Mapping定义
-
修改后用该配置创建你的索引
-
删除临时索引
-
控制当前字段是否被索引
- Index - 控制当前字段是否被索引. 默认为true. 如果设置成false,该字段不可被索引
PUT users
{
"mappings" : {
"properties" : {
"firstName" : {
"type" : "text"
},
"lastName" : {
"type" : "text"
},
"mobile" : {
"type" : "text",
"index": false
}
}
}
}
Index Option
-
四种不同级别的Index Option配置,可以控制倒排索引记录的内容
-
docs - 记录doc id
-
freqs - 记录doc id 和 term frequencies
-
positions - 记录doc id / term frequencies / term position
-
offsets - doc id / term frequencies / term position/ character offects
-
-
Text类型默认记录positions,其他默认为docs
-
记录内容越多,占用存储空间越大
null_value
GET users/_search?q=mobile:NULL
PUT users
{
"mappings" : {
"properties" : {
"firstName" : {
"type" : "text"
},
"lastName" : {
"type" : "text"
},
"mobile" : {
"type" : "keyword",
"null_value": "NULL"
}
}
}
}
-
需要对Null值实现搜索
-
只有Keyword类型支持设定Null_value
copy_to设置
PUT users
{
"mappings": {
"properties": {
"firstName":{
"type": "text",
"copy_to": "fullName"
},
"lastName":{
"type": "text",
"copy_to": "fullName"
}
}
}
}
GET users/_search?q=fullName:(Ruan Yiming)
-
_all在7中被copy_to所替代
-
满足一些特定的搜索需求
-
copy_to将字段的数值拷贝到目标字段,实现类似_all的作用
-
copy_to的目标字段不出现在_source中
数组类型
- ElasticSearch中不提供专门的数组类型.但是任何字段,都可以包含多个相同类型的数值
PUT users/_doc/1
{
"name":"onebird",
"interests":"reading"
}
PUT users/_doc/1
{
"name":"twobirds",
"interests":["reading","music"]
}
课程Demos
#设置 index 为 false
DELETE users
PUT users
{
"mappings" : {
"properties" : {
"firstName" : {
"type" : "text"
},
"lastName" : {
"type" : "text"
},
"mobile" : {
"type" : "text",
"index": false
}
}
}
}
PUT users/_doc/1
{
"firstName":"Ruan",
"lastName": "Yiming",
"mobile": "12345678"
}
POST /users/_search
{
"query": {
"match": {
"mobile":"12345678"
}
}
}
#设定Null_value
DELETE users
PUT users
{
"mappings" : {
"properties" : {
"firstName" : {
"type" : "text"
},
"lastName" : {
"type" : "text"
},
"mobile" : {
"type" : "keyword",
"null_value": "NULL"
}
}
}
}
PUT users/_doc/1
{
"firstName":"Ruan",
"lastName": "Yiming",
"mobile": null
}
PUT users/_doc/2
{
"firstName":"Ruan2",
"lastName": "Yiming2"
}
GET users/_search
{
"query": {
"match": {
"mobile":"NULL"
}
}
}
#设置 Copy to
DELETE users
PUT users
{
"mappings": {
"properties": {
"firstName":{
"type": "text",
"copy_to": "fullName"
},
"lastName":{
"type": "text",
"copy_to": "fullName"
}
}
}
}
PUT users/_doc/1
{
"firstName":"Ruan",
"lastName": "Yiming"
}
GET users/_search?q=fullName:(Ruan Yiming)
POST users/_search
{
"query": {
"match": {
"fullName":{
"query": "Ruan Yiming",
"operator": "and"
}
}
}
}
#数组类型
PUT users/_doc/1
{
"name":"onebird",
"interests":"reading"
}
PUT users/_doc/1
{
"name":"twobirds",
"interests":["reading","music"]
}
POST users/_search
{
"query": {
"match_all": {}
}
}
GET users/_mapping
补充阅读
网友评论