1腔呜、建立索引
curl -XPUT 'http://localhost:9200/mytest' -d '{
"settings":{
"index":{
"number_of_shards":5,
"number_of_replicas":1
}
}
}'
使用_settings修改索引文件
curl -XPUT 'http://localhost:9200/mytest/_settings' -d '{
"index":{
"number_of_replicas":7
}
}'
number_of_shards:設(shè)置分片數(shù)量
number_of_replicas:設(shè)置當(dāng)前索引副本數(shù)量
block.read_only:若設(shè)置為true熬尺,當(dāng)前索引只允許讀,不允許寫(xiě)或者更新
block.read:若設(shè)置為true,禁止讀取
block.write:若設(shè)置為true田炭,禁止寫(xiě)
block.metadata:若設(shè)置為true,禁止對(duì)metadata進(jìn)行操作
獲取索引較為詳細(xì)的配置信息
curl -XGET 'http://localhost:9200/mytest/_settings'
獲得多個(gè)索引的配置信息
curl -XGET 'http://localhost:9200/mytest,dept/_settings'
獲得全部索引的配置信息
curl -XGET 'http://localhost:9200/_all/_settings'
獲取指定索引文件的狀態(tài)信息
curl -XGET 'http://localhost:9200/mytest/_status'
doc:被索引文檔的信息,count描述索引中文檔的數(shù)量
stores:索引的大小
indexing:索引操作信息
get:實(shí)時(shí)獲取操作信息
search:搜索操作信息
2鼻忠、通過(guò)mapping映像配置索引
elasticsearch在使用時(shí),不指定映像杈绸,會(huì)自動(dòng)根據(jù)數(shù)據(jù)格式定義類(lèi)型帖蔓,但是如果需要對(duì)某些字段添加特殊屬性(分詞,存儲(chǔ)等)就必須指定映像瞳脓。
新建索引
curl -XPUT 'http://localhost:9200/mytest1' -d '{
"settings":{
"number_of_shards":5,
"number_of_replicas":1
},
"mapping":{
"wb":{}
}
}'
管理配置映像
curl -XPUT 'http://localhost:9200/mytest1/wb/_mapping' -d '{
"wb":{
"properties":{
"mymessage":{
"type":"string",
"store":true
}
}
}
}'
獲取映像信息
curl -XGET 'http://localhost:9200/mytest1/_mapping/wb'
curl -XGET 'http://localhost:9200/_all/_mapping/'
curl -XGET 'http://localhost:9200/_mapping/'
curl -XGET 'http://localhost:9200/mytest1/_mapping/wb,pages'
curl -XGET 'http://localhost:9200/mytest1/_mapping/wb/field/mymessage' //查看指定field的mapping
刪除映像
curl -XDELETE 'http://localhost:9200/mytest1/_mapping/wb'
curl -XDELETE 'http://localhost:9200/mytest1/wb'
curl -XDELETE 'http://localhost:9200/mytest1/wb/_mapping'
管理索引文件
一個(gè)關(guān)閉的索引將禁止寫(xiě)入或者讀取對(duì)其中數(shù)據(jù)的操作塑娇,而打來(lái)索引可以允許用戶(hù)對(duì)其中的數(shù)據(jù)文件進(jìn)行操作
curl -XPOST 'http://localhost:9200/mytest1/_open'
curl -XPOST 'http://localhost:9200/mytest1/_close'
檢測(cè)文件狀態(tài),返回200為存在劫侧,404為不存在
curl -XHEAD 'http://localhost:9200/mytest1/' -v
刪除索引
curl -XDELETE 'http://localhost:9200/mytest1/'
清空索引緩存
curl -XPOST 'http://localhost:9200/mytest1/_cache/clear'
curl -XPOST 'http://localhost:9200/mytest1,mytest/_cache/clear'
刷新索引數(shù)據(jù)
curl -XPOST 'http://localhost:9200/mytest1,mytest/_refresh'
curl -XPOST 'http://localhost:9200/mytest1/_refresh'
curl -XPOST 'http://localhost:9200/_refresh'
優(yōu)化索引數(shù)據(jù)
curl -XPOST 'http://localhost:9200/mytest1/_optimize'
將暫存于內(nèi)存中的臨時(shí)數(shù)據(jù)發(fā)送至索引文件埋酬,并清空內(nèi)部操作日志
curl -XPOST 'http://localhost:9200/mytest1/_flush'
3、中文分詞器
設(shè)置分詞器需要先關(guān)閉當(dāng)前索引烧栋,更改分詞器之后再打開(kāi)写妥。
curl -XPOST 'http://localhost:9200/mytest1/_close'
curl -XPUT 'http://localhost:9200/mytest1/_settings' -d '{
"analysis":{
"analyzer":{
"type":"custom",
"tokenizer":"standard"
}
}
}'
curl -XPOST 'http://localhost:9200/mytest1/_open'
對(duì)指定的文件進(jìn)行分詞
curl -XGET 'http://localhost:9200/_analyze?analyzer=standard&pretty' -d 'this is a test'
4、對(duì)文檔的其他操作
獲取指定文檔的信息
curl -XGET 'http://localhost:9200/myfirstindex/share/1'
curl -XGET 'http://localhost:9200/myfirstindex/share/1?pretty&_source=false'
curl -XGET 'http://localhost:9200/myfirstindex/share/1?pretty&fields=location'
pretty表示返回的結(jié)果中顯示縮進(jìn)劲弦,以方便閱讀
_source=false表示不顯示source的內(nèi)容
fields=location只顯示location的內(nèi)容
刪除文檔中的信息
curl -XDELETE 'http://localhost:9200/myfirstindex/share/1'
數(shù)據(jù)更新
處理過(guò)程:先取出文檔耳标,運(yùn)行指定腳本,然后再更新文檔
curl -XPUT 'http://localhost:9200/myfirstindex/share/2' -d '{
"url":"xtwang.github.io",
"date":"2015-12-3",
"location":"beijing"
}'
curl -XPOST 'http://localhost:9200/myfirstindex/share/2/_update' -d '{
"script":"ctx._source.location += text",
"params":{
"text":"test"
}
}'
創(chuàng)建文檔并且加入新的字段
curl -XPOST 'http://localhost:9200/myfirstindex/share/3/_update' -d '{
"script":"ctx._source.location += text",
"params":{
"text":"test"
},
"upsert":{
"counter":1
}
}'
當(dāng)存在文檔3時(shí)邑跪,使用script增加location字段的值次坡,如果不存在,使用upsert增加文檔画畅,并且添加字段counter砸琅。
批量獲取文檔
curl -XPOST 'http://localhost:9200/myfirstindex/share/_mget?' -d '{
"docs":[
{
"_location":"myfirstindex",
"_type":"share",
"_id":"1"
},
{
"_location":"myfirstindex",
"_type":"share",
"_id":"2"
}
]
}'
可以在?后面添加preety參數(shù)轴踱,獲取其中一個(gè)文檔的信息
刪除部分文檔
curl -XDELETE 'http://localhost:9200/myfirstindex/share/_query?q=location:beijing'