天天看点

ES 设置 mapping

ES 设置 mapping

接着上片说的创建索引 , 接下来需要做的是设置 mapping . 就相当于数据库添加了表之后 , 需要设计各个字段的数据格式

创建 index 之后 , ES 会默认创建 mapping .

查看 mapping

GET /roc_test_index/_mapping
           

返回结果

{
  "roc_test_index": {
    "mappings": {
      "roc_test_type": {
        "properties": {
          "id": {
            "type": "long"
          },
          "name": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              }
        	}
        }
      }
    }
  }
}
           

一般的数据格式 , 像字符串(text) , 数字格式(long) , 数组格式 . 创建索引或者往索引添加数据的时候 , 都会默认加上,但是如果时间格式 , 复杂的对象格式 , 就需要提前配置 mapping , 如果没有配置 , 直接添加数据的话 , mapping 就会默认为 text.这样的话是没法修改 mapping,只能是重建索引,或者重新用另一个字段

设置时间格式 mapping

PUT /roc_test_index/_mapping/roc_test_type
{
	"properties": {
		"createTime": {
			"type": "date",
			"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd"
		}
	}
}
           

设置复杂对象格式 mapping

PUT /roc_test_index/_mapping/roc_test_type
{
	"properties": {
		"comments": {
			"type": "nested",
			"properties": {
				"name": {
					"type": "text",
					"fields": {
						"keyword": {
							"type": "keyword",
							"ignore_above": 256
						}
					}
				},
				"age": {
					"type": "long"
				},
				"tags": {
					"type": "text",
					"fields": {
						"keyword": {
							"type": "keyword",
							"ignore_above": 256
						}
					}
				}
			}
		}
	}
}
           

查看 mapping

{
  "roc_test_index": {
    "mappings": {
      "roc_test_type": {
        "properties": {
          "comments": {
            "type": "nested",
            "properties": {
              "name": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "age": {
                "type": "long"
              },
              "tags": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              }
            }
          },
          "name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "id": {
            "type": "long"
          },
          "createTime": {
			"type": "date",
			"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd"
		  }
        }
      }
    }
  }
}
           
es

继续阅读