ElasticSearch入门API

2019-06-25  本文已影响0人  青衣敖王侯

1.索引API:

创建索引:

curl -uelastic:changeme -XPUT http://172.2<wbr>1.30.11:1020<wbr>0/test_index

查看现有索引:

curl -uelastic:changeme -XGET http://172.21.30.11:10200/_cat/indices

删除索引:

curl -uelastic:changeme -XDELETE http://172.21.30.11:10200/test_index

2.文档API

2.1创建文档类API

指定id创建文档api,创建文档时,如果索引不存在,es会自动创建对应的index和type

curl -uelastic:changeme -XPUT http://172.2<wbr>1.30.11:1020<wbr>0/test_index<wbr>/doc/1 -d '{"username":"zzm","age":1}'

创建一个随机分配id的文档,和上面的PUT不一样,我们用的POST方法:

curl -uelastic:changeme -XPOST http://172.2<wbr>1.30.11:1020<wbr>0/test_index<wbr>/doc -d '{"username":"zzm","age":1}'

2.2查询文档类API

获取一个文档:

curl -uelastic:changeme -XGET http://172.2<wbr>1.30.11:1020<wbr>0/test_index<wbr>/doc/1?prett<wbr>y

查询所有文档,不带请求体:

curl -uelastic:changeme -XGET http://172.21.30.11:10200/test_index/doc/_search?pretty

带查询请求体的用法:

curl -uelastic:changeme -XGET http://172.2<wbr>1.30.11:1020<wbr>0/test_index<wbr>/doc/_search<wbr>?pretty -d '
{

"query":{          "term":{

         "_id":"1"

         }

     }

}'

2.3批量操作文档类API,endpoint为_bulk,action_type有index,update,create,delete4类,index和create都是创建文档,两者的区别是,如果文档已经存在了index会覆盖掉,而create会 报错。

curl -uelastic:changeme -XPOST http://172.2<wbr>1.30.11:1020<wbr>0/_bulk -d '
{"create":{"_index":"test_index","_type":"doc","_id":"3"}}
{"username":"zzmbulk","age":10}

{"delete":{"_index":"test_index","_type":"doc","_id":"4"}}

{"update":{"_id":"1","_index":"test_index","_type":"doc"}}

{"doc":{"age":"20"}}

'

这里特别说明一下,每一行都要换行,最后一行留空行,还有,_type一定要加不然会报错,报错的让人怀疑人生。

2.4批量查询文档API,这个比_search高端一些,它可以查出躲过_id

curl -uelastic:changeme -XGET http://172.21.30.11:10200/_mget?pretty -d '

{

"docs":[

    {

        "_index":"test_index",

        "_type":"doc",

        "_id":"1"

    },

    {

        "_index":"test_index",

        "_type":"doc",

        "_id":"2"

    }

]

}'

上一篇下一篇

猜你喜欢

热点阅读