創(chuàng)建索引庫
使用postman或curl這樣的工具創(chuàng)建
put http://localhost:9200/索引庫名稱
參數(shù):
{
"settings": {
"index": {
"number_of_shards": 1,
"number_of_replicas": 0
}
}
}
number_of_shards:設(shè)置分片的數(shù)量,在集群中通常設(shè)置多個(gè)分片,表示一個(gè)索引庫將拆分成多片分別存儲(chǔ)不同的結(jié)點(diǎn),提高了ES的處理能力和高可用性,入門程序使用單機(jī)環(huán)境,這里設(shè)置為1。
number_of_replicas:設(shè)置副本的數(shù)量,設(shè)置副本是為了提高ES的高可靠性,單機(jī)環(huán)境設(shè)置為0.
如下是創(chuàng)建的例子,創(chuàng)建course索引庫,共1個(gè)分片,0個(gè)副本:
結(jié)果:
創(chuàng)建映射
創(chuàng)建映射就是向索引庫中創(chuàng)建field的過程,下邊是document和field與關(guān)系數(shù)據(jù)庫的概念的類比:
文檔(Document)----------------Row記錄
字段(Field)-------------------Columns 列
如果數(shù)據(jù)庫就表示一個(gè)索引庫可以創(chuàng)建很多不同類型的文檔,這在ES中也是允許的。
如果表就表示一個(gè)索引庫只能存儲(chǔ)相同類型的文檔,ES官方建議 在一個(gè)索引庫中只存儲(chǔ)相同類型的文檔。
put http://localhost:9200/索引庫名稱 /類型名稱/_mapping
這里類型名稱注意查看head顯示的內(nèi)容
使用postman請(qǐng)求
put http://localhost:9200/course/_doc/_mapping
{
"properties": {
"name": {
"type": "text"
},
"description": {
"type": "text"
},
"studymodel": {
"type": "keyword"
}
}
}
索引庫+映射
也可以同時(shí)創(chuàng)建索引庫和映射,這里創(chuàng)建一個(gè)新的索引庫xc_course
put http://localhost:9200/xc_course
{
"settings":{
"number_of_shards":3,
"number_of_replicas":1
},
"mappings":{
"properties":{
"name":{
"type":"text"
},
"country":{
"type":"keyword"
},
"age":{
"type":"integer"
}
}
}
創(chuàng)建文檔
ES中的文檔相當(dāng)于MySQL數(shù)據(jù)庫表中的記錄。
發(fā)送:put 或Post http://localhost:9200/xc_course/類型名稱/id值
(如果不指定id值ES會(huì)自動(dòng)生成ID)
post http://localhost:9200/xc_course/_doc/1
{
"name":"李明",
"country":"中國",
"age":"14"
}
通過head查看數(shù)據(jù)文章來源:http://www.zghlxwxcb.cn/news/detail-442247.html
查詢文檔
get http://localhost:9200/xc_course/_doc/1
文章來源地址http://www.zghlxwxcb.cn/news/detail-442247.html
到了這里,關(guān)于elasticSearch創(chuàng)建索引庫、映射、文檔的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!