使用es必须要知道的一些知识点:索引篇_es创建索引
warning:
这篇文章距离上次修改已过182天,其中的内容可能已经有所变动。
创建索引的主要方式是使用Elasticsearch的REST API。以下是一个使用Python和requests
库创建索引的示例代码:
import requests
# 设置Elasticsearch服务的地址
es_url = 'http://localhost:9200/'
# 索引名称
index_name = 'my_index'
# 创建索引的配置
index_config = {
"settings": {
"number_of_shards": 3,
"number_of_replicas": 2
},
"mappings": {
"properties": {
"title": {
"type": "text"
},
"content": {
"type": "text"
},
"date": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
}
}
}
}
# 创建索引
response = requests.put(es_url + index_name, json=index_config)
# 输出结果
print(response.json())
在这个例子中,我们首先设置了Elasticsearch服务的URL和我们想要创建的索引名称。然后,我们定义了索引的配置,包括分片数和副本数,以及字段的映射。最后,我们使用requests
库的put
方法发送一个PUT请求到Elasticsearch,以创建索引。创建成功后,Elasticsearch会返回一个JSON响应。
评论已关闭