Elasticsearch 之 API调用

一、Elasticsearch API 参考网址

https://www.elastic.co/guide/en/elasticsearch/reference/6.0/docs.html

Elasticsearch  之  API调用

二、基于curl创建信息

1、回顾elasticsearch的类型信息
Elasticsearch  之  API调用

2、创建如下信息
Elasticsearch  之  API调用

3、数据陈列

PUT twitter/tweet/1
{
    "user" : "kimchy",
    "post_date" : "2009-11-15T14:12:12",
    "message" : "trying out Elasticsearch"
}

4、curl 请求方法

curl -X PUT "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"
}
'

5、创建结果
Elasticsearch  之  API调用
Elasticsearch  之  API调用

三、基于python的API调用

1、创建索引

from elasticsearch import Elasticsearch
es = Elasticsearch(['10.3.153.200:9200'])
#创建索引test-index
ret = es.indices.create(index='test-index')
#打印返回结果
print(ret)

2、删除索引

from elasticsearch import Elasticsearch
es = Elasticsearch(['10.3.153.200:9200'])
#删除索引test-index
ret = es.indices.delete(index='test-index')
#打印返回结果
print(ret)

3、创建文档数据

from elasticsearch import Elasticsearch
es = Elasticsearch(['10.3.153.200:9200'])
#创建索引kang,类型book,id3,主体(title:elasticsearch从入门到精通)
ret = es.create(index='kang',doc_type='book',id=3,body={"title":"elasticsearch从入门到精通"})
#打印返回结果
print(ret)

4、获取文档数据

from elasticsearch import Elasticsearch
es = Elasticsearch(['10.3.153.200:9200'])
#查询索引kang,类型book,id=1的数据
ret = es.get(index='kang',doc_type='book',id=1)
print(ret)

5、更新文档数据

from elasticsearch import Elasticsearch
es = Elasticsearch(['10.3.153.200:9200'])
#更新索引KANG,文档类型book,id=1,doc更新内容,title变更为Ansible自动化运维
ret = es.update(index='kang',doc_type='book',id=1,body={"doc":{"title":'Ansible自动化运维'}})
print(ret)
上一篇:elasticSearch基本使用


下一篇:ElasticSearch