当使用Elasticsearch API时,我们可以使用各种HTTP方法(如GET、POST、PUT、DELETE)来与Elasticsearch进行交互。以下是一些常用的Elasticsearch API示例(python):
1. 创建索引:# pythonfrom elasticsearch import Elasticsearches = Elasticsearch()index_name = "my_index"# 创建索引es.indices.create(index=index_name)2. 索引文档:from elasticsearch import Elasticsearches = Elasticsearch()index_name = "my_index"document = {"title": "Example Document","content": "This is an example document."}# 索引文档es.index(index=index_name, body=document)3. 搜索文档:from elasticsearch import Elasticsearches = Elasticsearch()index_name = "my_index"query = { "query": { "match": { "content": "example" } } }# 搜索文档response = es.search(index=index_name, body=query)4. 删除索引:from elasticsearch import Elasticsearches = Elasticsearch()index_name = "my_index"# 删除索引es.indices.delete(index=index_name)