目錄
0. 數(shù)據(jù)格式說(shuō)明
1. ES的基本操作
1.1?索引操作
1.1.1?建立索引
1.1.2? 刪除索引
1.1.3??查詢索引
1.2 映射操作
1.2.1 建立映射
1.2.2 查詢映射
1.3 基本操作-CRUD
1.3.1 新增和替換文檔
1.3.2 查詢文檔
0. 數(shù)據(jù)格式說(shuō)明
在實(shí)戰(zhàn)開(kāi)始之前,為了便于書(shū)寫(xiě)和溝通,本文先來(lái)約定一下如何在文章中表達(dá)請(qǐng)求和響應(yīng)的信息:
1. 假設(shè)通過(guò)Postman工具或者Kibana向服務(wù)器發(fā)送一個(gè)PUT類型的請(qǐng)求,地址是:http://{IP}:9200/test001/article/1
2. 請(qǐng)求的內(nèi)容是JSON格式的,內(nèi)容如下:
{
"settings": {
"number_of_shards": 5 //設(shè)置5個(gè)片區(qū)
, "number_of_replicas": 1 //設(shè)置1個(gè)備份
}
}
對(duì)于上面的請(qǐng)求,本文中就以如下格式描述:
PUT /user
{
"settings": {
"number_of_shards": 5 //設(shè)置5個(gè)片區(qū)
, "number_of_replicas": 1 //設(shè)置1個(gè)備份
}
}
1. ES的基本操作
1.1?索引操作
1.1.1?建立索引
語(yǔ)法:PUT /索引名
# 發(fā)送請(qǐng)求
# 在沒(méi)有特殊設(shè)置的情況下,默認(rèn)有5個(gè)分片,1個(gè)備份,也可以通過(guò)請(qǐng)求參數(shù)的方式來(lái)指定.。
PUT test_index
{}
# 返回值
#! Deprecation: the default number of shards will change from [5] to [1] in 7.0.0; if you wish to continue using the default of [5] shards, you must manage this on the create index request or with an index template
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "test_index"
}
結(jié)果返回創(chuàng)建成果,同時(shí)kinbana顯示警告,從ES7開(kāi)始默認(rèn)的分片數(shù)將從5修改為1,如果需要繼續(xù)指定分片數(shù),則需要采用index參數(shù)模板傳入?yún)?shù)。
1.1.2? 刪除索引
語(yǔ)法:DELETE /索引名
# 發(fā)送請(qǐng)求
DELETE test_03
{}
# 返回值
{
"acknowledged" : true
}
?刪除成功:?
1.1.3??查詢索引
語(yǔ)法:GET /索引名
# 發(fā)送請(qǐng)求
GET test_index
# 返回值
{
"test_index" : {
"aliases" : { },
"mappings" : { },
"settings" : {
"index" : {
"creation_date" : "1664806581608",
"number_of_shards" : "5",
"number_of_replicas" : "1",
"uuid" : "qcVTOE2VRpieJi3sZOjZwg",
"version" : {
"created" : "6050499"
},
"provided_name" : "test_index"
}
}
}
}
查詢結(jié)果中包括了別名(aliases)、映射(mappings)以及配置參數(shù)(mappings)等詳細(xì)信息。
1.2 映射操作
1.2.1 建立映射
語(yǔ)法:POST/索引/映射
# 發(fā)送請(qǐng)求
POST /test_index/student
{
"mappings": {
"info": {
"properties": {
"name": {
"type": "text"
},
"clazz":{
"type":"string"
},
"id":{
"type":"double"
}
}
}
}
}
# 返回值
{
"_index" : "test_index",
"_type" : "student",
"_id" : "bB1InoMBWJmEB7k-HcSI",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
字段類型 type:double / long / integer / text / keyword / date / binary
注意:text和keyword都是字符串類型,但是只有text類型的數(shù)據(jù)才能分詞,字段的配置一旦確定就不能更改 映射的配置項(xiàng)有很多,我們可以根據(jù)需要只配置用得上的屬性.
創(chuàng)建成果,返回值中除了執(zhí)行結(jié)果(result)之外,還有當(dāng)前映射的詳細(xì)信息。?
1.2.2 查詢映射
語(yǔ)法:GET /索引名/_mapping
# 發(fā)送請(qǐng)求
GET /test_index/_mapping
#返回值
{
"test_index" : {
"mappings" : {
"student" : {
"properties" : {
"mappings" : {
"properties" : {
"info" : {
"properties" : {
"properties" : {
"properties" : {
"clazz" : {
"properties" : {
"type" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
},
"id" : {
"properties" : {
"type" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
},
"name" : {
"properties" : {
"type" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
返回值包括type和field詳細(xì)的JSON信息。
1.3 基本操作-CRUD
1.3.1 新增和替換文檔
語(yǔ)法:PUT /索引名/類型名/文檔ID
# 發(fā)送請(qǐng)求
PUT /test_index/student/1
{
"id":1,
"name":"戰(zhàn)三",
"clazz":12
}
# 返回值
{
"_index" : "test_index",
"_type" : "student",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
注意:
- 新增和替換文檔時(shí)需要使用標(biāo)準(zhǔn)的JSON格式,不能使用簡(jiǎn)版的JSON 格式。
- POST 操作也可以對(duì)文檔進(jìn)行新增和刪除操作。他們的唯一區(qū)別就在于PUT 請(qǐng)求是硬著陸,會(huì)把文檔中的屬性全部強(qiáng)制更新(如:_id、_version等)。POST 請(qǐng)求 和 DEPETE 不會(huì)重置_verison,只會(huì)在其基礎(chǔ)上+1。
- 一般推薦使用的是PUT 進(jìn)行文檔的更新和替換,POST 用的比較少。POST操作可以不指明文檔的ID,但是我們存在ES中的數(shù)據(jù)一般都是轉(zhuǎn)存過(guò)來(lái)的,不會(huì)自動(dòng)生成,故不推薦使用POST。
- 當(dāng)索引/類型/映射不存在時(shí),會(huì)使用默認(rèn)設(shè)置自動(dòng)添加。 ES中的數(shù)據(jù)一般是從別的數(shù)據(jù)庫(kù)導(dǎo)入的,所以文檔的ID會(huì)沿用原數(shù)據(jù)庫(kù)中的ID 索引庫(kù)中沒(méi)有該ID對(duì)應(yīng)的文檔時(shí)則新增,擁有該ID對(duì)應(yīng)的文檔時(shí)則替換。
1.3.2 查詢文檔
語(yǔ)法:1. 查詢所有(基本查詢語(yǔ)句):?GET /索引名/類型名/_search
# 發(fā)送請(qǐng)求
GET /test_index/student/_search
# 返回值
{
"took" : 151,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 1.0,
"hits" : [
{
"_index" : "test_index",
"_type" : "student",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"id" : 1,
"name" : "戰(zhàn)三",
"clazz" : 12
}
},
{
"_index" : "test_index",
"_type" : "student",
"_id" : "bB1InoMBWJmEB7k-HcSI",
"_score" : 1.0,
"_source" : {
"mappings" : {
"info" : {
"properties" : {
"name" : {
"type" : "text"
},
"clazz" : {
"type" : "string"
},
"id" : {
"type" : "double"
}
}
}
}
}
}
]
}
}
?查詢所有結(jié)果中包含以下字段:
- took:耗時(shí)
- _shards.total:分片總數(shù)
- hits.total:查詢到的數(shù)量
- hits.max_score:最大匹配度
- hits.hits:查詢到的結(jié)果
- hits.hits._score:匹配度
語(yǔ)法:2. 根據(jù)ID查詢:?GET /索引名/類型名/文檔ID文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-672908.html
# 發(fā)送請(qǐng)求
GET /test_index/student/1
# 返回值
{
"_index" : "test_index",
"_type" : "student",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"id" : 1,
"name" : "戰(zhàn)三",
"clazz" : 12
}
}
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-672908.html
到了這里,關(guān)于Elasticsearch 實(shí)戰(zhàn)之三:ES 基本操作的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!