天天看點

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

繼續閱讀