# 創建索引
1.創建索引
ES的API組成結構:使用RESTful API風格來命名API
```
API基本格式:http://<ip>:<port>/<索引>/<類型>/<文檔id> 常用HTTP動詞
```
>常用HTTP動詞:GET/PUT/POST/DELETE
>>PUT 執行創建或修改,如確定document的ID時
>>POST一般用于改變對象或查詢,如不確定document的ID,可以直接POST, ES可以 自己生成不會發生碰撞的UUID
>>GET一般用于查詢
>>DELETE用于刪除
>>HEAD請求獲取對象的基礎信息,如檢查文檔是否存在
我們現在有一個業務需求就是存儲訂單數據,需要創建訂單文檔,如下操作:
>一個文檔必須的三個元數據元素如下
_index文檔在哪存放
_type文檔表示的對象類別
_id文檔唯一標識,id可以自己指定,也可以自動生成
#####假設構建了的表如下:
是否主鍵|字段名|字段描述|數據類型|分詞
-|-|-|
是|orderId|訂單ID|long|否
否|userId|用戶ID|long|否
否|orderNo|訂單號|string|否
否|userName|用戶名|string|否
否|totalPrice|價格|float|否
否|address|收貨地址|string|是
否|createTime|創建時間|data|否
如果不創建mapping,ES會根據文檔的字段數據自動識別類型
這里需要創建自己定義的mapping
#####創建自定義mapping
PUT或者POST
```
PUT /order_index/_mapping/order_type/
{
"order_type": {
"properties": {
"orderId": {
"type": "long"
},
"userId": {
"type": "long"
},
"userName": {
"type": "text"
},
"totalPrice": {
"type": "float"
},
"address": {
"type": "text",
"analyzer": "ik_smart",
"search_analyzer": "ik_smart"
},
"createTime": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
}
}
```


1.建立order_index索引
2.在order_index下建立order_type類型
3.每個訂單數據是一個文檔
簡單的創建索引方式,只能使用PUT創建
```\`
PUT /order_index/
```
簡單的創建類型方式,只能使用POST,除非指定document的ID
```
POST /order_index/order_type/
```

以上創建索引和類型可以通過一條命令完成
```
PUT /order_index/order_type/1
{
"orderId":10001,
"userId":10001,
"orderNo":"a10001",
"userName":"John",
"totalPrice":50.00,
"address":"江蘇省南京市江寧區XXX",
"createTime":"2018-05-16 14:08:00"
}
```

- 目錄
- 前言
- ElasticSearch基礎
- 基礎概念
- 生產環境配置
- ElasticSearch插件
- ElasticSearch-head插件
- 中文分詞
- ElasticSearch安全插件x-pack
- ElasticSearch查詢
- ElasticSearch語法
- 創建索引
- 新增文檔
- 修改文檔
- 查詢文檔
- 簡單查詢
- 基礎查詢
- 聚合查詢
- 刪除文檔
- ElasticSearch高級查詢
- filter語法
- 關聯查詢
- SpringBoot集成ES的操作
- java操作ES
- Spring-data-elasticsearch操作ES
- SpringBoot性能優化
- ElasticSearch的優化
- ElasticSearch系統優化
- ElasticSearch數據的備份與恢復
- ElasticSearch性能調優
- ElasticSearch集群監控
- ElasticSearch問題匯總
- ElasticSearch問題
- ElasticSearch學習網站