ES-索引新數據
0.通過mapping映射新建索引
CURL -XPOST 'localhost:9200/test/index?pretty' -d '{ "mappings": { "docs": { "_source": { "excludes": [ "query_content" ] }, "properties": { "legalbasis": { "enabled": false }, "query_content": { "doc_values": false, "search_analyzer": "ik_smart", "type": "text", "analyzer": "ik_smart" }, "updatetime": { "enabled": false }, "openlaw_seq": { "enabled": false }, "url": { "enabled": false }, "doctype": { "enabled": false }, "modify_time": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" }, "fact": { "enabled": false } } } }, "settings": { "index": { "number_of_replicas": "0", "number_of_shards": "6", "refresh_interval": "10s", "translog": { "durability": "async", "flush_threshold_size": "1g" } } } }'
1.通過curl索引一篇文檔
curl是一個命令行工具,通過HTTP協議傳送數據。使用curl命令發送HTTP請求。
索引一篇文檔
FengZhendeMacBook-Pro:bin FengZhen$ curl -XPOST 'http://localhost:9200/get-together/group/1?pretty' -d '{ > "name":"ES Test", > "organizer":"Feng" > }' { "_index" : "get-together", "_type" : "group", "_id" : "1", "_version" : 1, "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "created" : true }
ES自動添加了get-together索引,並且為group類型創建了一個新的映射。映射包含字符串字段的定義。默認情況下,ES處理所有這些,無需任何事先的配置,就可以開始索引。若有需要,可改變這一配置
2.創建索引和映射類型
(1). 手動創建索引
FengZhendeMacBook-Pro:bin FengZhen$ curl -XPUT ‘http://localhost:9200/music’ {“acknowledged”:true}
創建索引本身比創建一篇文檔要花費更多時間,提前創建索引的另一個理由是想指定和ES默認不同的配置,例如:確定分片的數量。
(2). 獲取映射
映射是隨着新文檔而自動創建的,而且ES自動的將name和organizers字段識別為字符串。如果添加新文檔的同事添加另一個新的字段,ES也會猜測它的類型,並將其附加到映射。
為了查看當前的映射,發送一個HTTP GET請求到該索引的_mapping端點。這將展示所有類型的映射,但是可以通過在_mapping端點后指定類型的名字來獲得某個具體的映射。
FengZhendeMacBook-Pro:bin FengZhen$ curl 'localhost:9200/get-together/_mapping/group?pretty' { "get-together" : { "mappings" : { "group" : { "properties" : { "name" : { "type" : "string" }, "organizer" : { "type" : "string" } } } } } }
返回的結果包括如下相關數據
索引名稱:get-together
類型名稱:group
屬性列表:name和organizer
屬性選項:兩個屬性的type都是string