Python可以使用elasticsearch-py库来对elasticsearch进行数据查询和添加。

首先需要安装elasticsearch-py库:

pip install elasticsearch

然后可以通过以下代码来连接elasticsearch:

from elasticsearch import Elasticsearch

es = Elasticsearch([{'host': 'localhost', 'port': 9200}])

其中,host和port分别为elasticsearch的IP地址和端口号。

接下来可以使用es对象来进行数据查询和添加。

查询数据:

res = es.search(index="my-index", body={"query": {"match_all": {}}})
print(res)

其中,index为要查询的索引名,body为查询语句。

添加数据:

doc = {
    'author': 'kimchy',
    'text': 'Elasticsearch: cool. bonsai cool.',
    'timestamp': datetime.now(),
}
res = es.index(index="my-index", id=1, body=doc)
print(res['result'])

其中,index为要添加的索引名,id为文档ID,body为文档内容。

以上就是Python对elasticsearch数据查询和添加的简单介绍

python 对elstaticsearch数据查询和添加

原文地址: https://www.cveoy.top/t/topic/hutY 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录