3.文檔索引極其管理

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

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末唇礁,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子惨篱,更是在濱河造成了極大的恐慌盏筐,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,386評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件砸讳,死亡現(xiàn)場(chǎng)離奇詭異琢融,居然都是意外死亡界牡,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,142評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門(mén)漾抬,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)宿亡,“玉大人,你說(shuō)我怎么就攤上這事奋蔚∷欤” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,704評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵泊碑,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我毯欣,道長(zhǎng)馒过,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,702評(píng)論 1 294
  • 正文 為了忘掉前任酗钞,我火速辦了婚禮腹忽,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘砚作。我一直安慰自己窘奏,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,716評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布葫录。 她就那樣靜靜地躺著着裹,像睡著了一般。 火紅的嫁衣襯著肌膚如雪米同。 梳的紋絲不亂的頭發(fā)上骇扇,一...
    開(kāi)封第一講書(shū)人閱讀 51,573評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音面粮,去河邊找鬼少孝。 笑死,一個(gè)胖子當(dāng)著我的面吹牛熬苍,可吹牛的內(nèi)容都是我干的稍走。 我是一名探鬼主播,決...
    沈念sama閱讀 40,314評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼柴底,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼婿脸!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起似枕,我...
    開(kāi)封第一講書(shū)人閱讀 39,230評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤盖淡,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后凿歼,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體褪迟,經(jīng)...
    沈念sama閱讀 45,680評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡冗恨,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,873評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了味赃。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片掀抹。...
    茶點(diǎn)故事閱讀 39,991評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖心俗,靈堂內(nèi)的尸體忽然破棺而出傲武,到底是詐尸還是另有隱情,我是刑警寧澤城榛,帶...
    沈念sama閱讀 35,706評(píng)論 5 346
  • 正文 年R本政府宣布揪利,位于F島的核電站,受9級(jí)特大地震影響狠持,放射性物質(zhì)發(fā)生泄漏疟位。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,329評(píng)論 3 330
  • 文/蒙蒙 一喘垂、第九天 我趴在偏房一處隱蔽的房頂上張望甜刻。 院中可真熱鬧,春花似錦正勒、人聲如沸得院。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,910評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)祥绞。三九已至,卻和暖如春阱驾,著一層夾襖步出監(jiān)牢的瞬間就谜,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,038評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工里覆, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留丧荐,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,158評(píng)論 3 370
  • 正文 我出身青樓喧枷,卻偏偏與公主長(zhǎng)得像虹统,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子隧甚,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,941評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容