1.倒排索引原理图
原理图:
2.指定分片数,分片副本数创建索引 (未指定情况下使用默认值)
# 指定分片数(完整信息存储分块数),副本数(保证高可用性,每块冗余节点数)
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 修改副本数
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 禁止写入
curl -H 'Content-Type:application/json' -XPUT http://localhost:9200/myweibo2/_settings?pretty -d'
{
"index":{
"blocks.write":true
}
}'
# 禁止操作元数据(连通过 _settings 查看元数据都被拦截了)
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)
# 再新增一个索引
curl -XPUT http://localhost:9200/weibo
# 一次性查看多个索引的配置
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"
}
}
}
}
}
# 插入数据
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 内置字段: _uid, _id, _type, _source, _all, _analyzer, _boost, _parent, _routing, _index, _size, _timestamp, _ttl
ES 数据类型: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 同时,指定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":{}
}
# 关闭 index
curl -XPOST http://localhost:9200/weibo/_close
#检查 索引关闭效果
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}}
# 清空多个索引缓存
curl -XPOST http://localhost:9200/weibo,weibo/_cache/clear
# 刷新指定索引数据
curl -XPOST http://localhost:9200/weibo/_refresh
{"_shards":{"total":10,"successful":5,"failed":0}}
# 刷新全部数据索引
curl -XPOST http://localhost:9200/_refresh
{"_shards":{"total":14,"successful":9,"failed":0}}
# 优化索引(合并碎片,再次不展开)
curl -XPOST http://localhost:9200/weibo/_optimize?max_num_segments=1
# 将内存中临时数据刷写到索引文件,然后清空内部操作日志 _flush
curl -XPOST http://localhost:9200/weibo/_flush
{"_shards":{"total":10,"successful":5,"failed":0}}
# 关闭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
}
]
}
# 测试 ik 最细粒度分析效果
curl -H 'Content-Type:application/json' -XPOST http://localhost:9200/_analyze?pretty -d'
{
"analyzer":"ik_max_word",
"text":"中华人民共和国中央人民政府主席"
}'
{
"tokens" : [
{
"token" : "中华人民共和国",
"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" : "人民共和国",
"start_offset" : 2,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 4
},
{
"token" : "人民",
"start_offset" : 2,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 5
},
{
"token" : "共和国",
"start_offset" : 4,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 6
},
{
"token" : "共和",
"start_offset" : 4,
"end_offset" : 6,
"type" : "CN_WORD",
"position" : 7
},
{
"token" : "国中",
"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":"中华人民共和国中央人民政府主席"
}'
{
"tokens" : [
{
"token" : "中华人民共和国",
"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
}
}
}
}
}
}
}
}
# 插入数据
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"
}
}
# 简化返回结果 _source=false 屏蔽具体消息,只返回是否存在
curl -XGET http://localhost:9200/weibo/example/1?_source=false
{
"_index":"weibo",
"_type":"example",
"_id":"1",
"_version":1,
"found":true
}
# _source=true 显示消息细节
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"}
}
# 抓取两个字段 _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 修改数值
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"
}
}
}
}
}'
# 插入数据
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"]
}'
# 同时更新连个字段
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 屏蔽无效信息
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 抓取多个文档的 多个字段
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
}
网友评论