一、Index API
1、插入文档
curl -XPUT 'localhost:9200/twitter/tweet/1?pretty' -H 'Content-Type: application/json' -d'
{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}
'
或
http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/docs-index_/1.json
2、自动创建索引
索引操作自动创建索引
put操作可以手动创建新类型
动态创建索引禁止:
action.auto_create_index
: false 或 the cluster update settings API
动态映射类型禁止:
index.mapper.dynamic: true
动态创建索引模式:
action.auto_create_index:
aaa*,-bbb*,+ccc*,-*
+表示允许,-表示不允许
3、版本控制
curl -XPUT 'localhost:9200/twitter/tweet/1?version=2&pretty' -H 'Content-Type: application/json' -d'
{
"message" : "elasticsearch now has versioning support, double cool!"
}
'
或
PUT twitter/tweet/1?version=2
{
"message" : "elasticsearch now has versioning support, double cool!"
}
4、版本控制类型
internal,external or external_gt, external_gte
5、操作类型
op_type: create 存在就会创建失败
PUT twitter/tweet/1?op_type=create
{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}
或
curl -XPUT 'localhost:9200/twitter/tweet/1?op_type=create&pretty' -H 'Content-Type: application/json' -d'
{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}
'
6、自动产生id
POST twitter/tweet/
{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}
或
curl -XPOST 'localhost:9200/twitter/tweet/?pretty' -H 'Content-Type: application/json' -d'
{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}
'
7、路由
POST /twitter/tweet?routing=kimchy&pretty
{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}
或
curl -XPOST 'localhost:9200/twitter/tweet?routing=kimchy&pretty' -H 'Content-Type: application/json' -d'
{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}
'
文档根据routing值将文档路由到相应的分片
8、分布式
分片有主次之分
9、等待分片激活
index.write.wait_for_active_shards: 2 写操作会等待2个分片备份恢复才会返回或者超时
及时设置为all,
也不一定能保证一定写成功
判断是否激活是在各个备份写入前判断的
10、刷新
写入后进行刷新
11、等待更新noop update
12、超时
PUT twitter/tweet/1?timeout=5m
{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}
或
curl -XPUT 'localhost:9200/twitter/tweet/1?timeout=5m&pretty' -H 'Content-Type: application/json' -d'
{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}
'
二、获取
1、查询文档
GET twitter/tweet/1
或
curl -XGET 'localhost:9200/twitter/tweet/0?pretty'
文档存在查询
HEAD /twitter/tweet/1
或
curl -XHEAD 'localhost:9200/twitter/tweet/0?pretty'
2、实时性
realtime
parameter: false 关闭实时性
默认,都是实时的
文档更新后没有refresh,查询会导致refresh,从而使文档可见
3、源过滤 官方教程有毛病,不配套
关闭源检索?
GET twitter/tweet/0?_source=false
或
curl -XGET 'localhost:9200/twitter/tweet/0?_source=false&pretty'
获取部分源信息
GET twitter/tweet/1?_source=user&_source_exclude=messag*
4、存储域信息
设置counter域为非存储域
PUT test{
"mappings": {
"tweet": {
"properties": {
"counter": {
"type": "integer",
"store": false
},
"tags": {
"type": "keyword",
"store": true
}
}
}
}
}
或
curl -XPUT 'localhost:9200/test?pretty' -H 'Content-Type: application/json' -d'
{
"mappings": {
"tweet": {
"properties": {
"counter": {
"type": "integer",
"store": false
},
"tags": {
"type": "keyword",
"store": true
}
}
}
}
}
'
PUT test/tweet/1
{
"counter" : 1,
"tags" : ["red"]
}
或
curl -XPUT 'localhost:9200/test/tweet/1?pretty' -H 'Content-Type: application/json' -d'
{
"counter" : 1,
"tags" : ["red"]
}
'
GET /test/tweet/1?stored_fields=tags,counter
或
curl -XGET 'localhost:9200/test/tweet/1?stored_fields=tags,counter&pretty'
5、直接获取_source
获取文档source信息
GET twitter/tweet/1/_source
或
curl -XGET 'localhost:9200/twitter/tweet/1/_source?pretty'
过滤source信息
GET /twitter/tweet/1/_source?_source_include=user&_source_exclude=message'
或
curl -XGET 'localhost:9200/twitter/tweet/1/_source?_source_include=*.id&_source_exclude=entities'&pretty'
测试文档是否存在
HEAD twitter/tweet/1/_source
或
curl -XHEAD 'localhost:9200/twitter/tweet/1/_source?pretty'
6、路由
GET twitter/tweet/2?routing=user1
7、GET偏向
默认各个备份随机提供服务
_primary, _local
8、刷新
refresh:true
9、分布式
10、版本控制
三、删除API
1、操作
GET twitter/tweet/1?routing=user1
2、版本控制
3、路由
DELETE /twitter/tweet/1?routing=kimchy
4、自动创建索引
如果删除的文件的索引不存在,delete操作会自动创建索引
5、等待激活的分片
6、刷新
7、超时
DELETE /twitter/tweet/1?timeout=5m
或
curl -XDELETE 'localhost:9200/twitter/tweet/1?timeout=5m&pretty'
四、通过query删除文档API
1、通过query删除文档
POST twitter/_delete_by_query{
"query": {
"match": {
"message": "trying"
}
}
}
或
curl -XPOST 'localhost:9200/twitter/_delete_by_query?pretty' -H 'Content-Type: application/json' -d'
{
"query": {
"match": {
"message": "some message"
}
}
}
'
2、过程
获得索引快照--------->删除匹配文档 两个步骤中间发生了文档更新,删除会有版本冲突
3、删除索引所有文档
{
"query": {
"match_all": {}
}
}
或
curl -XPOST 'localhost:9200/twitter/tweet/_delete_by_query?conflicts=proceed&pretty' -H 'Content-Type: application/json' -d'
{
"query": {
"match_all": {}
}
}
'
4、多索引清空
{
"query": {
"match_all": {}
}
}
或
curl -XPOST 'localhost:9200/twitter,blog/tweet,post/_delete_by_query?pretty' -H 'Content-Type: application/json' -d'
{
"query": {
"match_all": {}
}
}
'
5、路由限定处理的分片
POST twitter/_delete_by_query?routing=1
{
"query": {
"range" : {
"age" : {
"gte" : 10
}
}
}
}
或
curl -XPOST 'localhost:9200/twitter/_delete_by_query?routing=1&pretty' -H 'Content-Type: application/json' -d'
{
"query": {
"range" : {
"age" : {
"gte" : 10
}
}
}
}
'
6、限定滚动数
{
"query": {
"term": {
"user": "kimchy"
}
}
}
或
curl -XPOST 'localhost:9200/twitter/_delete_by_query?scroll_size=5000&pretty' -H 'Content-Type: application/json' -d'
{
"query": {
"term": {
"user": "kimchy"
}
}
}
'
7、URL参数
待补充
https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete-by-query.html
8、任务接口
获取delete-by-query任务的状态
GET _tasks?detailed=true&actions=*/delete/byquery
或
curl -XGET 'localhost:9200/_tasks?detailed=true&actions=*/delete/byquery&pretty'
根据任务id直接查看任务
GET /_tasks/taskId:1
或
curl -XGET 'localhost:9200/_tasks/taskId:1?pretty'
9、取消delete-by-query任务
POST _tasks/task_id:1/_cancel
或
curl -XPOST 'localhost:9200/_tasks/task_id:1/_cancel?pretty'
10、rethrottling
POST _delete_by_query/task_id:1/_rethrottle?requests_per_second=-1
或
curl -XPOST 'localhost:9200/_delete_by_query/task_id:1/_rethrottle?requests_per_second=-1&pretty'
11、人工分片
PO