服务热线
135-6963-3175
内置REST接口
/index/_search 搜索指定索引下的数据
/_aliases 获取或操作索引的别名
/index 查询或者新增索引
/index/type/ 创建或操作类型
/index/_mapping 创建或操作mapping
/index/_setting 创建或操作设置(number_of_shards是不可更改的)
/index/_open 打开指定被关闭的索引
/index/_close 关闭指定索引
/index/_refresh 刷新索引(使新增加内容对搜索可见,不保证数据被写入磁盘)
/index/flush 刷新索引(会触发lucene提交)
curl -XGET 'http://127.0.0.1:9200'
curl
-X指定请求方法,HEAD GEP POST PUT等
-d参数
-H header请求头信息
1、创建索引库
curl -XPUT 'http://localhost:9200/bigdata'
http://172.30.254.157:9200/devplatform_index?pretty
pretty用来美化格式
2、在索引库下创建索引类型product并插入数据
curl -H 'content-type:application/json' -XPOST 'http://localhost:9200/bigdata/product/1?pretty' -d
'{
"name":"hadoop",
"author":"Doug Cutting",
"core":["hdsfs","mr","yarn"],
"latest_version":3.0
}'
一般用PUT更新、POST新增
curl -XPUT 'http://192.168.80.200:9200/zhouls/emp/1?pretty' -d'{"name":"tom","age":25}'
{"_index":"zhouls","_type":"emp","_id":"1","_version":1,"_shards":{"total":2,"successful":1,"failed":0},"created":true}
即,"_index":"zhouls",是索引库是zhouls
"_type":"emp",是类型是emp
"_id":"1",是id是1
"_version":1,是版本是1
"_shards":{"total":2,"successful":1,"failed":0},是
"created":true,是创建索引提示成功!
3、全局更新
使用post将索引标识为1索引信息更新为:“name”:"HADOOP","author":"郭富城"
curl -H 'Content-Type:application/json' -XPOST 'http://localhost:9200/bigdata/product/1?pretty' -d '{"name":"HADOOP","author":"郭富城"}'
使用put,将索引标识为1索引信息更新为:"name":"hadoop","author":"道哥.卡廷"
curl -H ‘Content-Type:application/json’ -XPUT 'http://localhost:9200/bigdata/product/1?pretty' -d '{"author":"道哥.卡廷"}'
说明:这些操作都是全局更新,先将旧的删除,然后再重新创建一个新的。id相同的document
4.局部更新
用_update ,更新的是source中的doc内容
使用post将索引标识为1索引信息更新为:“author”:"小鱼儿"
curl -H 'Content-Type:application/json' -XPOST 'http://localhost:9200/bigdata/product/1/_update?pretty' -d '{"doc":{"author":"小鱼儿"}}'
5.根据id查询
curl -XGET http://localhost:9200/bigdata/product/1?pretty
curl -X DELETE "localhost:9200/customer/_doc/2?pretty"
查询显示特定的字段内容
curl -XGET http://localhost:9200/bigdata/product/1/source=name,author&pretty
获取source的数据
curl -XGET http://localhostL9200/bigdata/product/1/sourcec?pretty
查询所有
curl -XGET http://localhost:9200/bigdata/product/_search?pretty
根据条件进行查询
curl -XGET http://localhost:9200/bigdata/product_search?q=name:hbase&pretty
批量创建
curl -X POST "localhost:9200/customer/_doc/_bulk?pretty" -H 'Content-Type: application/json' -d'
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
批量操作
curl -X POST "localhost:9200/customer/_doc/_bulk?pretty" -H 'Content-Type: application/json' -d'
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}
加载简单数据集(注意文件名没有前面的@)
curl -H "Content-Type: application/json" -XPOST "localhost:9200/bank/_doc/_bulk?pretty&refresh" --data-binary "@accounts.json"
搜索api 默认饭回10条
curl -X GET "localhost:9200/bank/_search?q=*&sort=account_number:asc&pretty"'
curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'
{
"query": { "match_all": {} },
"sort": [
{ "account_number": "asc" }
{ "balance": { "order": "desc" } }
]
}'
指定返回结果的fields
curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'
{
"query": { "match_all": {} },
"_source": ["account_number", "balance"]
}'
聚合(Aggregations)
es设置
设置es日志级别
curl -X PUT "localhost:9200/_cluster/settings" -H 'Content-Type: application/json' -d'
{
"transient": {
"logger.org.elasticsearch.transport": "trace"
}
}'
es监控
curl -X PUT "localhost:9200/_cluster/settings" -H 'Content-Type: application/json' -d'
{
"persistent": {
"xpack.monitoring.collection.enabled": true
}
}'
公共选项
pretty=true (json格式展开)
human=false (以人方便阅读的方式展示)
filter_path=took,-hits.hits._id (-代表不展示该字段)
flat_settings=true (以平面格式展示)
error_trace=true (显示错误堆栈)
index API
自动创建index
禁止自动创建index:action.auto_create_index = false(默认为true)
可以指定pattern:action.auto_create_index = +aaa,-bbb,+ccc,- (+ 允许、-禁止)
禁止自动创建mapping:index.mapper.dynamic = false
version
创建文档时不指定默认为1,后续修改一次+1
当然可以在创建文档的时候指定version(version>=0)
Operation Type
方式一:
curl -X PUT "localhost:9200/twitter/_doc/1?op_type=create" -H 'Content-Type: application/json' -d'
{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}'
方式二:
curl -X PUT "localhost:9200/twitter/_doc/1/_create" -H 'Content-Type: application/json' -d'
{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}'
op_type=create or _create强制指定为创建模式,如果存在该文档就会报错