1.倒排索引原理圖
? ? 原理圖:
2.指定分片數(shù)肝匆,分片副本數(shù)創(chuàng)建索引 (未指定情況下使用默認(rèn)值)
# 指定分片數(shù)(完整信息存儲(chǔ)分塊數(shù))空繁,副本數(shù)(保證高可用性惭聂,每塊冗余節(jié)點(diǎn)數(shù))
curl -H 'Content-Type:application/json' -XPUT http://localhost:9200/myweibo2?pretty -d'
{
? ? "settings":{
? ? ? ? "index":{
? ? ? ? ? ? "number_of_shards":1,
? ? ? ? ? ? "number_of_replicas":1
? ? ? ? }
? ? }? ?
}'
{
? "acknowledged" : true,
? "shards_acknowledged" : true,
? "index" : "myweibo2"
}
# 使用 _settings api 修改副本數(shù)
curl -H 'Content-Type:application/json' -XPUT http://localhost:9200/myweibo2/_settings?pretty -d'
{
? ? "index":{
? ? ? ? "number_of_replicas":3
? ? }
}'
{
? "acknowledged" : true
}
# "blocks.read_only":true 只讀
curl -H 'Content-Type:application/json' -XPUT http://localhost:9200/myweibo2/_settings?pretty -d'
{
? ? "index":{
? ? ? ? "blocks.read_only":true
? ? }
}'
# 禁止操作 api
# "blocks.read":true 禁止讀取
curl -H 'Content-Type:application/json' -XPUT http://localhost:9200/myweibo2/_settings?pretty -d'
{
? ? "index":{
? ? ? ? "blocks.read":true
? ? }
}'
# "blocks.write":true 禁止寫(xiě)入
curl -H 'Content-Type:application/json' -XPUT http://localhost:9200/myweibo2/_settings?pretty -d'
{
? ? "index":{
? ? ? ? "blocks.write":true
? ? }
}'
# 禁止操作元數(shù)據(jù)(連通過(guò) _settings 查看元數(shù)據(jù)都被攔截了)
curl -H 'Content-Type:application/json' -XPUT http://localhost:9200/myweibo2/_settings?pretty -d'
{
? ? "index":{
? ? ? ? "blocks.metadata":true
? ? }
}'
# 查看全部索引的配置
curl -XGET http://localhost:9200/_all/_settings?pretty
# 查看配置
curl -XGET http://localhost:9200/myweibo2/_settings?pretty
{
? "myweibo2" : {
? ? ? ? "settings" : {
? ? ? ? ? ? "index" : {
? ? ? ? ? ? ? ? "number_of_shards" : "1",
? ? ? ? ? ? ? ? "blocks" : {
? ? ? ? ? ? ? ? ? ? ? "metadata" : "false",
? ? ? ? ? ? ? ? ? ? ? "read" : "true",
? ? ? ? ? ? ? ? ? ? ? "read_only" : "false",
? ? ? ? ? ? ? ? ? ? ? "write" : "true"
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? "provided_name" : "myweibo2",
? ? ? ? ? ? ? ? "creation_date" : "1547051554736",
? ? ? ? ? ? ? ? "number_of_replicas" : "3",
? ? ? ? ? ? ? ? "uuid" : "jI15jtCwRQmtLR4xPbvtQg",
? ? ? ? ? ? ? ? "version" : {
? ? ? ? ? ? ? ? ? ? ? "created" : "6050499"
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? }
}
# 新建索引(方案1)
# 再新增一個(gè)索引
curl -XPUT http://localhost:9200/weibo
# 一次性查看多個(gè)索引的配置
curl -XGET http://localhost:9200/weibo,myweibo2/_settings?pretty
# 使用通配查找 匹配索引的 配置
curl -XGET http://localhost:9200/*weibo*/_settings?pretty
{
? "weibo" : {
? ? ? ? "settings" : {
? ? ? ? ? ? "index" : {
? ? ? ? ? ? ? ? "creation_date" : "1547052857943",
? ? ? ? ? ? ? ? "number_of_shards" : "5",
? ? ? ? ? ? ? ? "number_of_replicas" : "1",
? ? ? ? ? ? ? ? "uuid" : "piglaiA_RZWYLOamR1bj3w",
? ? ? ? ? ? ? ? "version" : {
? ? ? ? ? ? ? ? ? ? ? "created" : "6050499"
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? "provided_name" : "weibo"
? ? ? ? ? ? }
? ? ? ? }
? },
? "myweibo2" : {
? ? ? ? "settings" : {
? ? ? ? ? ? "index" : {
? ? ? ? ? ? ? ? "number_of_shards" : "1",
? ? ? ? ? ? ? ? "blocks" : {
? ? ? ? ? ? ? ? ? ? ? "metadata" : "false",
? ? ? ? ? ? ? ? ? ? ? "read" : "true",
? ? ? ? ? ? ? ? ? ? ? "read_only" : "false",
? ? ? ? ? ? ? ? ? ? ? "write" : "true"
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? "provided_name" : "myweibo2",
? ? ? ? ? ? ? ? "creation_date" : "1547051554736",
? ? ? ? ? ? ? ? "number_of_replicas" : "3",
? ? ? ? ? ? ? ? "uuid" : "jI15jtCwRQmtLR4xPbvtQg",
? ? ? ? ? ? ? ? "version" : {
? ? ? ? ? ? ? ? ? ? ? "created" : "6050499"
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? }
}
# 插入數(shù)據(jù)
curl -H 'Content-Type:application/json' -XPOST http://localhost:9200/weibo/example?pretty -d'
{
? ? "user":"Alan",
? ? "post_date":"2014-11-26T08:00:00",
? ? "mymessage":"this is an example on operation type on create"
}'
{
? "_index" : "weibo",
? "_type" : "example",
? "_id" : "xlQdNGgB6vx3VsC9w6XJ",? <<< 自增id
? "_version" : 1,
? "result" : "created",
? "_shards" : {
? ? ? "total" : 2,
? ? ? "successful" : 1,
? ? ? "failed" : 0
? },
? "_seq_no" : 0,
? "_primary_term" : 1
}
ES 內(nèi)置字段: _uid, _id, _type, _source, _all, _analyzer, _boost, _parent, _routing, _index, _size, _timestamp, _ttl
ES 數(shù)據(jù)類(lèi)型:String, Integer/long,Float/double,Boolean,Null,Date蒲肋。
#
curl -H 'Content-Type:application/json' -XPUT http://localhost:9200/weibo/example/3?pretty -d'
{
? ? "user":"LiMing",
? ? "post_date":"2014-11-24T14:12:12",
? ? "message":"Hello Tom"
}'
{
? ? "_index":"weibo",
? ? "_type":"example",
? ? "_id":"3",? <<<< 指定id
? ? "_version":1,
? ? "result":"created",
? ? "_shards":{"total":2,"successful":1,"failed":0},
? ? "_seq_no":0,
? ? "_primary_term":1
}
# 直接基于id 查詢
curl -XGET http://localhost:9200/weibo/example/3?pretty
{
? "_index" : "weibo",
? "_type" : "example",
? "_id" : "3",
? "_version" : 1,
? "found" : true,
? "_source" : {
? ? ? "user" : "LiMing",
? ? ? "post_date" : "2014-11-24T14:12:12",
? ? ? "message" : "Hello Tom"
? }
}
# 新建索引(方案2)
# 添加 index 同時(shí)乒省,指定mapping
curl -H 'Content-Type:application/json' -XPUT http://localhost:9200/weibo1 -d'
{
? ? "settings":{
? ? ? ? "number_of_shards":1,
? ? ? ? "number_of_replicas":1
? ? },
? ? "mappings":{
? ? ? ? "news":{
? ? ? ? ? ? "properties":{
? ? ? ? ? ? ? ? "user":{
? ? ? ? ? ? ? ? ? ? "type":"keyword"
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
}'
# 查看自定 index mapping
curl http://localhost:9200/weibo1/_mapping?pretty
{
? "weibo1" : {
? ? "mappings" : {
? ? ? ? "news" : {
? ? ? ? ? "properties" : {
? ? ? ? ? ? "user" : {
? ? ? ? ? ? ? ? "type" : "keyword"
? ? ? ? ? ? }
? ? ? ? ? }
? ? ? ? }
? ? }
? }
}
# 查看指定 index 下 type 的 mapping
curl http://localhost:9200/weibo1/_mapping/news?pretty
# 查看指定 field 的 mapping
curl http://localhost:9200/weibo1/_mapping/news/field/user?pretty
{
? "weibo1" : {
? ? "mappings" : {
? ? ? ? "news" : {
? ? ? ? ? "user" : {
? ? ? ? ? ? "full_name" : "user",
? ? ? ? ? ? "mapping" : {
? ? ? ? ? ? ? ? "user" : {
? ? ? ? ? ? ? ? ? "type" : "keyword"
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? }
? ? ? ? }
? ? }
? }
}
# 刪除索引
curl -XDELETE http://localhost:9200/weibo1
{"acknowledged":true}
# 查詢
curl -H 'Content-Type:application/json' -XPOST http://localhost:9200/weibo/_search -d'
{
? ? "query":{
? ? ? ? "match_all":{}
}
# 關(guān)閉 index
curl -XPOST http://localhost:9200/weibo/_close
#檢查 索引關(guān)閉效果
curl -H 'Content-Type:application/json' -XPOST http://localhost:9200/weibo/_search?pretty -d'
{
? ? "query":{
? ? ? ? "match_all":{}
? ? }
}'
{
? "error" : {
? ? "root_cause" : [
? ? ? ? {
? ? ? ? ? "type" : "index_closed_exception",
? ? ? ? ? "reason" : "closed",
? ? ? ? ? "index_uuid" : "piglaiA_RZWYLOamR1bj3w",
? ? ? ? ? "index" : "weibo"
? ? ? ? }
? ? ],
? ? "type" : "index_closed_exception",
? ? "reason" : "closed",
? ? "index_uuid" : "piglaiA_RZWYLOamR1bj3w",
? ? "index" : "weibo"
? },
? "status" : 400
}
# 重新啟用 _open
curl -XPOST http://localhost:9200/weibo/_open
{"acknowledged":true,"shards_acknowledged":true}
# 檢查 _open 效果
curl -H 'Content-Type:application/json' -XPOST http://localhost:9200/weibo/_search?pretty -d'
{
? ? "query":{
? ? ? ? "match_all":{}
? ? }
}'
{
? "took" : 20,
? "timed_out" : false,
? "_shards" : {
? ? "total" : 5,
? ? "successful" : 5,
? ? "skipped" : 0,
? ? "failed" : 0
? },
? "hits" : {
? ? "total" : 2,
? ? "max_score" : 1.0,
? ? "hits" : [
? ? ? ? {
? ? ? ? ? "_index" : "weibo",
? ? ? ? ? "_type" : "example",
? ? ? ? ? "_id" : "xlQdNGgB6vx3VsC9w6XJ",
? ? ? ? ? "_score" : 1.0,
? ? ? ? ? "_source" : {
? ? ? ? ? ? "user" : "Alan",
? ? ? ? ? ? "post_date" : "2014-11-26T08:00:00",
? ? ? ? ? ? "mymessage" : "this is an example on operation type on create"
? ? ? ? ? }
? ? ? ? },
? ? ? ? {
? ? ? ? ? "_index" : "weibo",
? ? ? ? ? "_type" : "example",
? ? ? ? ? "_id" : "3",
? ? ? ? ? "_score" : 1.0,
? ? ? ? ? "_source" : {
? ? ? ? ? ? "user" : "LiMing",
? ? ? ? ? ? "post_date" : "2014-11-24T14:12:12",
? ? ? ? ? ? "message" : "Hello Tom"
? ? ? ? ? }
? ? ? ? }
? ? ]
? }
}
# 查看 _open 效果
curl -XHEAD http://localhost:9200/weibo -v
* About to connect() to localhost port 9200 (#0)
*? ? Trying ::1...
* Connected to localhost (::1) port 9200 (#0)
> HEAD /weibo HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost:9200
> Accept: */*
>
< HTTP/1.1 200 OK
< content-type: application/json; charset=UTF-8
< content-length: 537
<
# 基于通配刪除索引
curl -XDELETE http://localhost:9200/idx*
{"acknowledged":true}
# 清空索引緩存
curl -XPOST http://localhost:9200/weibo/_cache/clear
{"_shards":{"total":10,"successful":5,"failed":0}}
# 清空多個(gè)索引緩存
curl -XPOST http://localhost:9200/weibo,weibo/_cache/clear
# 刷新指定索引數(shù)據(jù)
curl -XPOST http://localhost:9200/weibo/_refresh
{"_shards":{"total":10,"successful":5,"failed":0}}
# 刷新全部數(shù)據(jù)索引
curl -XPOST http://localhost:9200/_refresh
{"_shards":{"total":14,"successful":9,"failed":0}}
# 優(yōu)化索引(合并碎片巧颈,再次不展開(kāi))
curl -XPOST http://localhost:9200/weibo/_optimize?max_num_segments=1
# 將內(nèi)存中臨時(shí)數(shù)據(jù)刷寫(xiě)到索引文件,然后清空內(nèi)部操作日志 _flush
curl -XPOST http://localhost:9200/weibo/_flush
{"_shards":{"total":10,"successful":5,"failed":0}}
# 關(guān)閉index -> 修改指定索引使用的分詞策略 -> 重啟index
curl -XPOST http://localhost:9200/weibo/_close
{"acknowledged":true}
curl -H 'Content-Type:application/json' -XPUT http://localhost:9200/weibo/_settings?pretty -d'
{
? ? "analysis":{
? ? ? ? "analyzer":{
? ? ? ? ? ? "content":{
? ? ? ? ? ? ? ? "type":"custom",
? ? ? ? ? ? ? ? "tokenizer":"standard"
? ? ? ? ? ? }
? ? ? ? }
? ? }
}'
{
? "acknowledged" : true
}
curl -XPOST http://localhost:9200/weibo/_open
{"acknowledged":true,"shards_acknowledged":true}
curl -XGET http://localhost:9200/_analyze?analyzer=standard&text='this is a test'&pretty
curl -XGET http://localhost:9200/_analyze?&text='this is a test'
curl -H 'Content-Type:application/json' -XPOST http://localhost:9200/_analyze?pretty -d'
{
? ? "analyzer":"standard",
? ? "text":"this is a test"
}'
{
? "tokens" : [
? ? {
? ? ? ? "token" : "this",
? ? ? ? "start_offset" : 0,
? ? ? ? "end_offset" : 4,
? ? ? ? "type" : "<ALPHANUM>",
? ? ? ? "position" : 0
? ? },
? ? {
? ? ? ? "token" : "is",
? ? ? ? "start_offset" : 5,
? ? ? ? "end_offset" : 7,
? ? ? ? "type" : "<ALPHANUM>",
? ? ? ? "position" : 1
? ? },
? ? {
? ? ? ? "token" : "a",
? ? ? ? "start_offset" : 8,
? ? ? ? "end_offset" : 9,
? ? ? ? "type" : "<ALPHANUM>",
? ? ? ? "position" : 2
? ? },
? ? {
? ? ? ? "token" : "test",
? ? ? ? "start_offset" : 10,
? ? ? ? "end_offset" : 14,
? ? ? ? "type" : "<ALPHANUM>",
? ? ? ? "position" : 3
? ? }
? ]
}
# 測(cè)試 ik 最細(xì)粒度分析效果
curl -H 'Content-Type:application/json' -XPOST http://localhost:9200/_analyze?pretty -d'
{
? ? "analyzer":"ik_max_word",
? ? "text":"中華人民共和國(guó)中央人民政府主席"
}'
{
? "tokens" : [
? ? {
? ? ? ? "token" : "中華人民共和國(guó)",
? ? ? ? "start_offset" : 0,
? ? ? ? "end_offset" : 7,
? ? ? ? "type" : "CN_WORD",
? ? ? ? "position" : 0
? ? },
? ? {
? ? ? ? "token" : "中華人民",
? ? ? ? "start_offset" : 0,
? ? ? ? "end_offset" : 4,
? ? ? ? "type" : "CN_WORD",
? ? ? ? "position" : 1
? ? },
? ? {
? ? ? ? "token" : "中華",
? ? ? ? "start_offset" : 0,
? ? ? ? "end_offset" : 2,
? ? ? ? "type" : "CN_WORD",
? ? ? ? "position" : 2
? ? },
? ? {
? ? ? ? "token" : "華人",
? ? ? ? "start_offset" : 1,
? ? ? ? "end_offset" : 3,
? ? ? ? "type" : "CN_WORD",
? ? ? ? "position" : 3
? ? },
? ? {
? ? ? ? "token" : "人民共和國(guó)",
? ? ? ? "start_offset" : 2,
? ? ? ? "end_offset" : 7,
? ? ? ? "type" : "CN_WORD",
? ? ? ? "position" : 4
? ? },
? ? {
? ? ? ? "token" : "人民",
? ? ? ? "start_offset" : 2,
? ? ? ? "end_offset" : 4,
? ? ? ? "type" : "CN_WORD",
? ? ? ? "position" : 5
? ? },
? ? {
? ? ? ? "token" : "共和國(guó)",
? ? ? ? "start_offset" : 4,
? ? ? ? "end_offset" : 7,
? ? ? ? "type" : "CN_WORD",
? ? ? ? "position" : 6
? ? },
? ? {
? ? ? ? "token" : "共和",
? ? ? ? "start_offset" : 4,
? ? ? ? "end_offset" : 6,
? ? ? ? "type" : "CN_WORD",
? ? ? ? "position" : 7
? ? },
? ? {
? ? ? ? "token" : "國(guó)中",
? ? ? ? "start_offset" : 6,
? ? ? ? "end_offset" : 8,
? ? ? ? "type" : "CN_WORD",
? ? ? ? "position" : 8
? ? },
? ? {
? ? ? ? "token" : "中央人民政府",
? ? ? ? "start_offset" : 7,
? ? ? ? "end_offset" : 13,
? ? ? ? "type" : "CN_WORD",
? ? ? ? "position" : 9
? ? },
? ? {
? ? ? ? "token" : "中央",
? ? ? ? "start_offset" : 7,
? ? ? ? "end_offset" : 9,
? ? ? ? "type" : "CN_WORD",
? ? ? ? "position" : 10
? ? },
? ? {
? ? ? ? "token" : "人民政府",
? ? ? ? "start_offset" : 9,
? ? ? ? "end_offset" : 13,
? ? ? ? "type" : "CN_WORD",
? ? ? ? "position" : 11
? ? },
? ? {
? ? ? ? "token" : "人民",
? ? ? ? "start_offset" : 9,
? ? ? ? "end_offset" : 11,
? ? ? ? "type" : "CN_WORD",
? ? ? ? "position" : 12
? ? },
? ? {
? ? ? ? "token" : "民政",
? ? ? ? "start_offset" : 10,
? ? ? ? "end_offset" : 12,
? ? ? ? "type" : "CN_WORD",
? ? ? ? "position" : 13
? ? },
? ? {
? ? ? ? "token" : "政府",
? ? ? ? "start_offset" : 11,
? ? ? ? "end_offset" : 13,
? ? ? ? "type" : "CN_WORD",
? ? ? ? "position" : 14
? ? },
? ? {
? ? ? ? "token" : "主席",
? ? ? ? "start_offset" : 13,
? ? ? ? "end_offset" : 15,
? ? ? ? "type" : "CN_WORD",
? ? ? ? "position" : 15
? ? }
? ]
}
# 智能中文分詞
curl -H 'Content-Type:application/json' -XPOST http://localhost:9200/_analyze?pretty -d'
{
? ? "analyzer":"ik_smart",
? ? "text":"中華人民共和國(guó)中央人民政府主席"
}'
{
? "tokens" : [
? ? {
? ? ? ? "token" : "中華人民共和國(guó)",
? ? ? ? "start_offset" : 0,
? ? ? ? "end_offset" : 7,
? ? ? ? "type" : "CN_WORD",
? ? ? ? "position" : 0
? ? },
? ? {
? ? ? ? "token" : "中央人民政府",
? ? ? ? "start_offset" : 7,
? ? ? ? "end_offset" : 13,
? ? ? ? "type" : "CN_WORD",
? ? ? ? "position" : 1
? ? },
? ? {
? ? ? ? "token" : "主席",
? ? ? ? "start_offset" : 13,
? ? ? ? "end_offset" : 15,
? ? ? ? "type" : "CN_WORD",
? ? ? ? "position" : 2
? ? }
? ]
}
# 查看指定索引的 映射
curl -XGET http://localhost:9200/weibo/_mappings/example?pretty
{
? "weibo" : {
? ? "mappings" : {
? ? ? ? "example" : {
? ? ? ? ? "properties" : {
? ? ? ? ? ? "message" : {
? ? ? ? ? ? ? ? "type" : "text",
? ? ? ? ? ? ? ? "fields" : {
? ? ? ? ? ? ? ? ? "keyword" : {
? ? ? ? ? ? ? ? ? ? "type" : "keyword",
? ? ? ? ? ? ? ? ? ? "ignore_above" : 256
? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? },
? ? ? ? ? ? "mymessage" : {
? ? ? ? ? ? ? ? "type" : "text",
? ? ? ? ? ? ? ? "fields" : {
? ? ? ? ? ? ? ? ? "keyword" : {
? ? ? ? ? ? ? ? ? ? "type" : "keyword",
? ? ? ? ? ? ? ? ? ? "ignore_above" : 256
? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? },
? ? ? ? ? ? "post_date" : {
? ? ? ? ? ? ? ? "type" : "date"
? ? ? ? ? ? },
? ? ? ? ? ? "user" : {
? ? ? ? ? ? ? ? "type" : "text",
? ? ? ? ? ? ? ? "fields" : {
? ? ? ? ? ? ? ? ? "keyword" : {
? ? ? ? ? ? ? ? ? ? "type" : "keyword",
? ? ? ? ? ? ? ? ? ? "ignore_above" : 256
? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? }
? ? ? ? }
? ? }
? }
}
# 插入數(shù)據(jù)
curl -H 'Content-Type:application/json' -XPOST http://localhost:9200/weibo/example/1 -d'
{
? ? "user":"LiMing",
? ? "post_date":"2014-10-15T14:12:12",
? ? "message":"Hello Tom"
}'
# 指定id 查詢
curl -XGET http://localhost:9200/weibo/example/1?pretty
{
? "_index" : "weibo",
? "_type" : "example",
? "_id" : "1",
? "_version" : 1,
? "found" : true,
? "_source" : {
? ? "user" : "LiMing",
? ? "post_date" : "2014-10-15T14:12:12",
? ? "message" : "Hello Tom"
? }
}
# 簡(jiǎn)化返回結(jié)果 _source=false 屏蔽具體消息袖扛,只返回是否存在
curl -XGET http://localhost:9200/weibo/example/1?_source=false
{
? ? "_index":"weibo",
? ? "_type":"example",
? ? "_id":"1",
? ? "_version":1,
? ? "found":true
}
# _source=true 顯示消息細(xì)節(jié)
curl -XGET http://localhost:9200/weibo/example/1?_source=true
{
? ? "_index":"weibo",
? ? "_type":"example",
? ? "_id":"1",
? ? "_version":1,
? ? "found":true,
? ? "_source":
? ? ? ? {
? ? ? ? ? ? "user":"LiMing",
? ? ? ? ? ? "post_date":"2014-10-15T14:12:12",
? ? ? ? ? ? "message":"Hello Tom"
? ? ? ? }
}
# 抓取指定字段_source=f1,f2
curl -XGET http://localhost:9200/weibo/example/1?_source=user
{
? ? "_index":"weibo",
? ? "_type":"example",
? ? "_id":"1",
? ? "_version":1,
? ? "found":true,
? ? "_source":{"user":"LiMing"}
}
# 抓取兩個(gè)字段 _source=user,post_date
curl -XGET http://localhost:9200/weibo/example/1?_source=user,post_date
{
? ? "_index":"weibo",
? ? "_type":"example",
? ? "_id":"1",
? ? "_version":1,
? ? "found":true,
? ? "_source":{
? ? ? ? "post_date":"2014-10-15T14:12:12",
? ? ? ? "user":"LiMing"
? ? }
}
# 指定 id 刪除
curl -XDELETE http://localhost:9200/weibo/example/2?pretty
{
? "_index" : "weibo",
? "_type" : "example",
? "_id" : "2",
? "_version" : 2,
? "result" : "not_found",? <<? 未找到
? "_shards" : {
? ? "total" : 2,
? ? "successful" : 1,
? ? "failed" : 0
? },
? "_seq_no" : 3,
? "_primary_term" : 3
}
# 指定 id 刪除
curl http://localhost:9200/weibo/example/1?pretty
{
? "_index" : "weibo",
? "_type" : "example",
? "_id" : "1",
? "_version" : 1,
? "found" : true,? << 找到了
? "_source" : {
? ? "user" : "LiMing",
? ? "post_date" : "2014-10-15T14:12:12",
? ? "message" : "Hello Tom"
? }
}
# 更新砸泛,添加新字段 like
curl -H 'Content-Type:application/json' -XPOST http://localhost:9200/weibo/example/1 -d'
{
? ? "user":"LiMing",
? ? "post_date":"2014-10-15T14:12:12",
? ? "message":"Hello Tom",
? ? "like":3
}'
{
? ? "_index":"weibo",
? ? "_type":"example",
? ? "_id":"1",
? ? "_version":2,
? ? "result":"updated",? <<< 更新
? ? "_shards":{"total":2,"successful":1,"failed":0},
? ? "_seq_no":2,
? ? "_primary_term":3
}
# 傳參修改 like
curl -H 'Content-Type:application/json' -XPOST http://localhost:9200/weibo/example/1 -d '
{
? ? "script":"ctx._source.like += count",
? ? "params":{
? ? ? ? "count":4
? ? }
}'
{
? ? "_index":"weibo",
? ? "_type":"example",
? ? "_id":"1",
? ? "_version":3,
? ? "result":"updated",
? ? "_shards":{"total":2,"successful":1,"failed":0},
? ? "_seq_no":3,
? ? "_primary_term":3
}
curl -H 'Content-Type:application/json' -XPUT http://localhost:9200/weibo -d'{
"settings":{
? "number_of_shards":1,
? "number_of_replicas":1
},
"mappings":{
? ? "news":{
? ? ? "properties":{
? ? ? ? "user":{
? ? ? ? ? ? "type":"text",
? ? ? ? ? ? "analyzer":"ik_smart",
? ? ? ? ? ? "search_analyzer":"ik_smart"
? ? ? ? ? },
? ? ? ? ? "post_date":{
? ? ? ? ? ? "type":"date",
? ? ? ? ? ? "format":"yyyy/mm/dd HH:MM:SS"
? ? ? ? ? },
? ? ? ? ? "message":{
? ? ? ? ? ? ? "type":"text",
? ? ? ? ? ? ? "analyzer":"ik_max_word",
? ? ? ? ? ? ? "search_analyzer":"ik_max_word"
? ? ? ? ? }
? ? ? ? }
? ? ? }
? }
}'
# 刪除 索引
curl -XDELETE http://localhost:9200/weibo?pretty
# 重建索引,并自定義 mapping
curl -H 'Content-Type:application/json' -XPUT http://localhost:9200/weibo -d'{
"settings":{
? "number_of_shards":1,
? "number_of_replicas":1
},
"mappings":{
? ? "news":{
? ? ? "properties":{
? ? ? ? "user":{
? ? ? ? ? ? "type":"text",
? ? ? ? ? ? "analyzer":"ik_smart",
? ? ? ? ? ? "search_analyzer":"ik_smart"
? ? ? ? ? },
? ? ? ? ? "post_date":{
? ? ? ? ? ? "type":"date",
? ? ? ? ? ? "format":"yyyy/mm/dd HH:MM:SS"
? ? ? ? ? },
? ? ? ? ? "message":{
? ? ? ? ? ? ? "type":"text",
? ? ? ? ? ? ? "analyzer":"ik_max_word",
? ? ? ? ? ? ? "search_analyzer":"ik_max_word"
? ? ? ? ? }
? ? ? ? }
? ? ? }
? }
}'
{"acknowledged":true,"shards_acknowledged":true,"index":"weibo"}
# 新建文檔
curl -H 'Content-Type:application/json' -XPOST http://localhost:9200/weibo/news/1?pretty -d'
{
? ? "user":"LiMing",
? ? "post_date":"2014/10/15 14:12:12",
? ? "message":"Hello Tom"
}'
{
? "_index" : "weibo",
? "_type" : "news",
? "_id" : "1",
? "_version" : 1,
? "result" : "created",
? "_shards" : {
? ? "total" : 2,
? ? "successful" : 1,
? ? "failed" : 0
? },
? "_seq_no" : 0,
? "_primary_term" : 1
}
# 添加字段
curl -H 'Content-Type:application/json' -XPOST http://localhost:9200/weibo/news/1?pretty -d'
{
? ? "like":3
}'
{
? "_index" : "weibo",
? "_type" : "news",
? "_id" : "1",
? "_version" : 2,
? "result" : "updated",
? "_shards" : {
? ? "total" : 2,
? ? "successful" : 1,
? ? "failed" : 0
? },
? "_seq_no" : 1,
? "_primary_term" : 1
}
curl -H 'Content-Type:application/json' -XPOST http://localhost:9200/weibo/news/1/_update?pretty -d'
{
? ? "script":"ctx._source.like.add(params.count)",
? ? "params":{
? ? ? ? "count":4
? ? }
}'
# 使用 _update api 修改數(shù)值
curl -H 'Content-Type:application/json' -XPOST http://localhost:9200/weibo/news/1/_update?pretty -d'
{
? ? "script":"ctx._source.like += 4"
}'
{
? ? "_index":"weibo",
? ? "_type":"news",
? ? "_id":"1",
? ? "_version":3,
? ? "result":"updated",
? ? "_shards":{"total":2,"successful":1,"failed":0},
? ? "_seq_no":2,
? ? "_primary_term":1
}
# 查看 修改效果
curl http://localhost:9200/weibo/news/1?pretty
{
? "_index" : "weibo",
? "_type" : "news",
? "_id" : "1",
? "_version" : 3,
? "found" : true,
? "_source" : {
? ? "like" : 7
? }
}
# 傳參更新
curl -H 'Content-Type:application/json' -XPOST http://localhost:9200/weibo/news/1/_update?pretty -d'
{
? ? "script":{
? ? ? ? "inline":"ctx._source.like += params.count",
? ? ? ? "params":{
? ? ? ? ? ? "count":4
? ? ? ? }
? ? }
}'
# 查看更新效果
curl http://localhost:9200/weibo/news/1
{
? "_index" : "weibo",
? "_type" : "news",
? "_id" : "1",
? "_version" : 5,
? "found" : true,
? "_source" : {
? ? "like" : 11
? }
}
# 原想添加字段蛆封,變成了整體替換了
curl -H 'Content-Type:application/json' -XPOST http://localhost:9200/weibo/news/1?pretty -d'
{
? ? "tags":["Hello"]
}'
# 重新插入
curl -H 'Content-Type:application/json' -XPOST http://localhost:9200/weibo/news/1 -d'
{? ?
? ? "user":"LiMing",
? ? "post_date":"2014/10/15 14:12:12",
? ? "message":"Hello Tom",
? ? "like":3,
? ? "tags":["Hello"]
}'
{
? ? "_index":"weibo",
? ? "_type":"news",
? ? "_id":"1",
? ? "_version":9,
? ? "result":"updated",
? ? "_shards":{"total":2,"successful":1,"failed":0},
? ? "_seq_no":8,
? ? "_primary_term":1
}
# tags 集合字段添加元素
curl -H 'Content-Type:application/json' -XPOST http://localhost:9200/weibo/news/1/_update -d'{
? ? "script":"ctx._source.tags.add(params.tag)",
? ? "params":{
? ? ? ? "tag":"This is a nice day!"
? ? }
}'
# 查看集合添加字段效果
curl http://localhost:9200/weibo/news/1?pretty
{
? "_index" : "weibo",
? "_type" : "news",
? "_id" : "1",
? "_version" : 10,
? "found" : true,
? "_source" : {
? ? "user" : "LiMing",
? ? "post_date" : "2014/10/15 14:12:12",
? ? "message" : "Hello Tom",
? ? "like" : 3,
? ? "tags" : [
? ? ? "Hello",
? ? ? "Today is a nice day!"
? ? ]
? }
}
# 刪除索引
curl -XDELETE http://localhost:9200/weibo
# 重建索引
curl -H 'Content-Type:application/json' -XPUT http://localhost:9200/weibo?pretty -d'{
? ? "settings":{
? ? ? ? "number_of_shards":1,
? ? ? ? "number_of_replicas":1
? ? },
? ? "mappings":{
? ? ? ? "news":{
? ? ? ? ? ? "properties":{
? ? ? ? ? ? ? ? "user":{
? ? ? ? ? ? ? ? ? ? "type":"text",
? ? ? ? ? ? ? ? ? ? "analyzer":"ik_max_word",
? ? ? ? ? ? ? ? ? ? "search_analyzer":"ik_max_word"
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? "post_date":{
? ? ? ? ? ? ? ? ? ? "type":"date",
? ? ? ? ? ? ? ? ? ? "format":"yyyy/mm/dd HH:MM:SS"
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? "message":{
? ? ? ? ? ? ? ? ? ? "type":"text",
? ? ? ? ? ? ? ? ? ? "analyzer":"ik_max_word",
? ? ? ? ? ? ? ? ? ? "search_analyzer":"ik_max_word"
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
}'
# 插入數(shù)據(jù)
curl -H 'Content-Type:application/json' -XPOST http://localhost:9200/weibo/news/1?pretty -d'{
? ? "user":"LiMing",
? ? "post_date":"2014/10/15 14:12:12",
? ? "message":"Hello Tom",
? ? "like":3,
? ? "tags":["Hello"]
}'
# 同時(shí)更新連個(gè)字段
curl -H 'Content-Type:application/json' -XPOST http://localhost:9200/weibo/news/1/_update?pretty -d'{
? ? "script":{
? ? ? ? "inline":"ctx._source.like += params.count;ctx._source.tags.add(params.tag)",
? ? ? ? "params":{
? ? ? ? ? ? "count":4,
? ? ? ? ? ? "tag":"This is a nice day!"
? ? ? ? }
? ? }
}'
# 添加新字段
curl -H 'Content-Type:application/json' -XPOST http://localhost:9200/weibo/news/1/_update -d'{
? ? "script":{
? ? ? ? "inline":"ctx._source.flag=\"red\""
? ? }
}'
{
? ? "_index":"weibo",
? ? "_type":"news",
? ? "_id":"1",
? ? "_version":5,
? ? "result":"updated",
? ? "_shards":{"total":2,"successful":1,"failed":0},
? ? "_seq_no":4,
? ? "_primary_term":1
}
# 查看增加字段效果
curl http://localhost:9200/weibo/news/1
{
? "_index" : "weibo",
? "_type" : "news",
? "_id" : "1",
? "_version" : 5,
? "found" : true,
? "_source" : {
? ? "user" : "LiMing",
? ? "post_date" : "2014/10/15 14:12:12",
? ? "message" : "Hello Tom",
? ? "like" : 7,
? ? "tags" : [
? ? ? "Hello",
? ? ? "This is a nice day!"
? ? ],
? ? "flag" : "red"
? }
}
# 傳參更新
curl -H 'Content-Type:application/json' -XPOST http://localhost:9200/weibo/news/1/_update?pretty -d'{
? ? "script":{
? ? ? ? "inline":"ctx._source.like += params.count",
? ? ? ? "params":{
? ? ? ? ? ? "count":4
? ? ? ? }
? ? },
? ? "upsert":{
? ? ? ? "counter":1
? ? }
}'
# 再插入一條記錄
curl -H 'Content-Type:application/json' -XPOST http://localhost:9200/weibo/news/2?pretty -d'{
? ? "user":"Andrew Json",
? ? "post_date":"2014/10/15 15:12:12",
? ? "message":"Hello world",
? ? "like":3,
? ? "tags":["Good","Boy"],
? ? "flag":"green"
}'
# _mget api 批量抓取
curl -H 'Content-Type:application/json' -XGET http://localhost:9200/weibo/news/_mget?pretty -d'
{
"docs":[
{
"_index":"weibo",
"_type":"news",
"_id":1
},
{
"_index":"weibo",
"_type":"news",
"_id":2
}
]
}'
{
? "docs" : [
? ? {
? ? ? "_index" : "weibo",
? ? ? "_type" : "news",
? ? ? "_id" : "1",
? ? ? "_version" : 6,
? ? ? "found" : true,
? ? ? "_source" : {
? ? ? ? "user" : "LiMing",
? ? ? ? "post_date" : "2014/10/15 14:12:12",
? ? ? ? "message" : "Hello Tom",
? ? ? ? "like" : 11,
? ? ? ? "tags" : [
? ? ? ? ? "Hello",
? ? ? ? ? "This is a nice day!"
? ? ? ? ],
? ? ? ? "flag" : "red"
? ? ? }
? ? },
? ? {
? ? ? "_index" : "weibo",
? ? ? "_type" : "news",
? ? ? "_id" : "2",
? ? ? "_version" : 1,
? ? ? "found" : true,
? ? ? "_source" : {
? ? ? ? "user" : "Andrew Json",
? ? ? ? "post_date" : "2014/10/15 15:12:12",
? ? ? ? "message" : "Hello world",
? ? ? ? "like" : 3,
? ? ? ? "tags" : [
? ? ? ? ? "Good",
? ? ? ? ? "Boy"
? ? ? ? ],
? ? ? ? "flag" : "green"
? ? ? }
? ? }
? ]
}
# _mget api 屏蔽無(wú)效信息
curl -H 'Content-Type:application/json' -XGET http://localhost:9200/weibo/news/_mget?pretty -d'
{
"docs":[
{
"_index":"weibo",
"_type":"news",
"_id":1,
"_source":false
},
{
"_index":"weibo",
"_type":"news",
"_id":2,
"_source":false
}
]
}'
{
? "docs" : [
? ? {
? ? ? "_index" : "weibo",
? ? ? "_type" : "news",
? ? ? "_id" : "1",
? ? ? "_version" : 6,
? ? ? "found" : true
? ? },
? ? {
? ? ? "_index" : "weibo",
? ? ? "_type" : "news",
? ? ? "_id" : "2",
? ? ? "_version" : 1,
? ? ? "found" : true
? ? }
? ]
}
# _mget api 抓取多個(gè)文檔的 多個(gè)字段
curl -H 'Content-Type:application/json' -XGET http://localhost:9200/weibo/news/_mget?pretty -d'
{
"docs":[
{
"_index":"weibo",
"_type":"news",
"_id":1,
"_source":["user","post_date"]
},
{
"_index":"weibo",
"_type":"news",
"_id":2,
"_source":["user","post_date"]
}
]
}'
{
? "docs" : [
? ? {
? ? ? "_index" : "weibo",
? ? ? "_type" : "news",
? ? ? "_id" : "1",
? ? ? "_version" : 6,
? ? ? "found" : true,
? ? ? "_source" : {
? ? ? ? "post_date" : "2014/10/15 14:12:12",
? ? ? ? "user" : "LiMing"
? ? ? }
? ? },
? ? {
? ? ? "_index" : "weibo",
? ? ? "_type" : "news",
? ? ? "_id" : "2",
? ? ? "_version" : 1,
? ? ? "found" : true,
? ? ? "_source" : {
? ? ? ? "post_date" : "2014/10/15 15:12:12",
? ? ? ? "user" : "Andrew Json"
? ? ? }
? ? }
? ]
}
curl -XGET http://localhost:9200/weibo/news/_query?pretty&q=user:LiMing
# 指定查詢條件下線 _search q=
curl -XGET http://localhost:9200/weibo/news/_search?pretty&q=user:LiMing
{
? "took" : 1,
? "timed_out" : false,
? "_shards" : {
? ? "total" : 1,
? ? "successful" : 1,
? ? "skipped" : 0,
? ? "failed" : 0
? },
? "hits" : {
? ? "total" : 2,
? ? "max_score" : 1.0,
? ? "hits" : [
? ? ? {
? ? ? ? "_index" : "weibo",
? ? ? ? "_type" : "news",
? ? ? ? "_id" : "1",
? ? ? ? "_score" : 1.0,
? ? ? ? "_source" : {
? ? ? ? ? "user" : "LiMing",
? ? ? ? ? "post_date" : "2014/10/15 14:12:12",
? ? ? ? ? "message" : "Hello Tom",
? ? ? ? ? "like" : 11,
? ? ? ? ? "tags" : [
? ? ? ? ? ? "Hello",
? ? ? ? ? ? "This is a nice day!"
? ? ? ? ? ],
? ? ? ? ? "flag" : "red"
? ? ? ? }
? ? ? },
? ? ? {
? ? ? ? "_index" : "weibo",
? ? ? ? "_type" : "news",
? ? ? ? "_id" : "2",
? ? ? ? "_score" : 1.0,
? ? ? ? "_source" : {
? ? ? ? ? "user" : "Andrew Json",
? ? ? ? ? "post_date" : "2014/10/15 15:12:12",
? ? ? ? ? "message" : "Hello world",
? ? ? ? ? "like" : 3,
? ? ? ? ? "tags" : [
? ? ? ? ? ? "Good",
? ? ? ? ? ? "Boy"
? ? ? ? ? ],
? ? ? ? ? "flag" : "green"
? ? ? ? }
? ? ? }
? ? ]
? }
}
# 查詢匹配刪除 _delete_by_query q=
curl -XPOST http://localhost:9200/weibo/news/_delete_by_query?q=user:LiMing
{
"took":177,
"timed_out":false,
"total":1,
"deleted":1,
"batches":1,
"version_conflicts":0,
"noops":0,
"retries":{"bulk":0,"search":0},
"throttled_millis":0,
"requests_per_second":-1.0,
"throttled_until_millis":0,
"failures":[]
}
# 查看刪除效果
curl http://localhost:9200/weibo/news/1
{
"_index":"weibo",
"_type":"news",
"_id":"1",
"found":false
}