Elastic Search Mapping
- 查看Mapping
GET /gb/_mapping/tweet
{
"gb": {
"mappings": {
"tweet": {
"properties": {
"date": {
"type": "date",
"format": "strict_date_optional_time||epoch_millis"
},
"name": {
"type": "string"
},
"tweet": {
"type": "string"
},
"user_id": {
"type": "long"
}
}
}
}
}
}
- 新建Index并指定tweet字段使用english分析器
PUT /gb
{
"mappings": {
"tweet" : {
"properties" : {
"tweet" : {
"type" : "string",
"analyzer": "english"
},
"date" : {
"type" : "date"
},
"name" : {
"type" : "string"
},
"user_id" : {
"type" : "long"
}
}
}
}
}
- 添加一个新字段,指定为not_analyzed
PUT /gb/_mapping/tweet
{
"properties" : {
"tag" : {
"type" : "string",
"index": "not_analyzed"
}
}
}
- 测试mapping
GET /gb/_analyze?field=tweet
{
"text": "Black-cats"
}
GET /gb/_analyze?field=tag
{
"text": "Black-cats"
}
- String类型的index属性的值
index属性控制string如何被index
属性值 | 意义 |
---|---|
analyzed | 先分析然后再index,即把为字段的值做全文索引 |
not_analyzed | 仅仅索引而不做分析 |
no | 不索引这个字段,即不可以被搜索 |
- 多级对象
{
"tweet": "Elasticsearch is very flexible",
"user": {
"id": "@johnsmith",
"gender": "male",
"age": 26,
"name": {
"full": "John Smith",
"first": "John",
"last": "Smith"
}
}
}
Elasticsearch会动态检测object field并且把它的类型map成object:
{
"gb": {
"tweet": { // root object
"properties": {
"tweet": { "type": "string" },
"user": { // inner object
"type": "object",
"properties": {
"id": { "type": "string" },
"gender": { "type": "string" },
"age": { "type": "long" },
"name": { // inner object
"type": "object",
"properties": {
"full": { "type": "string" },
"first": { "type": "string" },
"last": { "type": "string" }
}
}
}
}
}
}
}
}
Lucene 并不理解内部对象,它会把上面的结果转换成如下的结构
{
"tweet": [elasticsearch, flexible, very],
"user.id": [@johnsmith],
"user.gender": [male],
"user.age": [26],
"user.name.full": [john, smith],
"user.name.first": [john],
"user.name.last": [smith]
}
- 内部对象数组
{
"followers": [
{ "age": 35, "name": "Mary White"},
{ "age": 26, "name": "Alex Jones"},
{ "age": 19, "name": "Lisa Smith"}
]
}
会被转换为
{
"followers.age": [19, 26, 35],
"followers.name": [alex, jones, lisa, smith, mary, white]
}