# 导入Elasticsearch库
from elasticsearch import Elasticsearch
# 创建Elasticsearch客户端实例
es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
# 定义索引名称和文档类型
index_name = 'my_index'
doc_type = 'my_type'
# 插入文档示例
doc = {
'author': 'John Doe',
'text': 'Elasticsearch is a powerful search engine.'
}
res = es.index(index=index_name, doc_type=doc_type, id=1, body=doc)
print('插入结果:', res['result'])
# 查询文档示例
res = es.get(index=index_name, doc_type=doc_type, id=1)
print('查询结果:', res['_source'])
# 搜索文档示例
search_param = {
'query': {
'match': {
'text': 'powerful'
}
}
}
res = es.search(index=index_name, doc_type=doc_type, body=search_param)
print('搜索结果:', res['hits']['hits'])
elasticsearch
库,可以通过pip install elasticsearch
来安装。index
方法向指定的索引中插入一条文档,并打印插入结果。get
方法根据ID获取指定文档,并打印查询结果。search
方法根据查询条件搜索文档,并打印搜索结果。上一篇:python中if
下一篇:python文件读取
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站