项目实战:ES的增加数据和查询数据
    		       		warning:
    		            这篇文章距离上次修改已过436天,其中的内容可能已经有所变动。
    		        
        		                
                
from datetime import datetime
from elasticsearch import Elasticsearch
 
# 连接到Elasticsearch
es = Elasticsearch(hosts=["localhost:9200"])
 
# 创建一个新的项目数据
project_data = {
    "name": "Elasticsearch Tutorial",
    "description": "This is a tutorial on Elasticsearch",
    "created_at": datetime.now(),
    "tags": ["elasticsearch", "search", "tutorial"]
}
 
# 索引新的项目数据
index_response = es.index(index="projects", id=1, document=project_data)
print(f"Index response: {index_response}")
 
# 查询刚刚索引的项目数据
query_response = es.get(index="projects", id=1)
print(f"Query response: {query_response}")这段代码演示了如何使用Elasticsearch Python API连接到Elasticsearch服务器,并创建一个新的项目数据,然后将其索引到名为"projects"的索引中,并给它一个ID为1。接着,它展示了如何查询这个项目的数据。这个例子简单明了,并且使用了Elasticsearch的标准操作,对于初学者来说是非常有帮助的。
评论已关闭