elasticsearch入门介绍
    		       		warning:
    		            这篇文章距离上次修改已过449天,其中的内容可能已经有所变动。
    		        
        		                
                Elasticsearch 是一个基于 Apache Lucene 的开源搜索和分析引擎,设计用于云计算中,能够快速地处理大量数据。
以下是一个简单的 Python 示例,演示如何使用 Elasticsearch 的官方 Python 客户端 elasticsearch 进行基本操作:
首先,安装 Elasticsearch 客户端:
pip install elasticsearch然后,使用以下 Python 代码与 Elasticsearch 集群交互:
from elasticsearch import Elasticsearch
 
# 连接到本地Elasticsearch实例
es = Elasticsearch("http://localhost:9200")
 
# 创建一个新的索引
res = es.indices.create(index='my_index', ignore=400)
print(res['acknowledged'])
 
# 在索引中添加一个文档
doc = {
    'name': 'John Doe',
    'age': 30,
    'about': 'I love to go rock climbing'
}
res = es.index(index='my_index', id=1, document=doc)
print(res['result'])
 
# 搜索索引中的文档
res = es.search(index='my_index', query={'match': {'about': 'climbing'}})
print(res['hits']['hits'])
 
# 删除索引
res = es.indices.delete(index='my_index', ignore=[400, 404])
print(res['acknowledged'])这段代码展示了如何连接到 Elasticsearch 实例、创建一个新的索引、向索引中添加一个文档、执行搜索以及删除索引。这是 Elasticsearch 的基本操作,对于初学者来说是一个很好的起点。
评论已关闭