Elastic search 系统学习之四: 文档API

一、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、实时性

realtimeparameter: 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、删除索引所有文档

POST twitter/tweet/_delete_by_query?conflicts=proceed
{
  "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、多索引清空

POST twitter,blog/tweet,post/_delete_by_query
{
  "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、限定滚动数

POST twitter/_delete_by_query?scroll_size=5000
{
  "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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值