【ElasticSearch】索引(添加)
1、REST API
文档:https://www.elastic.co/guide/en/elasticsearch/reference/7.13/indices-create-index.html
ignore_above 默认值 256
settings 中的index可以去掉
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1
}
kibana
PUT /myindex
{
"settings": {
"index": {
"number_of_shards": 3,
"number_of_replicas": 3
}
},
"mappings": {
"properties": {
"name": {
"type": "keyword"
},
"age": {
"type": "integer"
},
"title": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
}
}
}
},
"aliases": {
"myindex_alias": {}
}
}
curl
curl -XPUT http://localhost:9200/myindex/_mapping -H 'Content-Type:application/json' -d '
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 3
},
"mappings": {
"properties": {
"name": {
"type": "keyword"
},
"age": {
"type": "integer"
},
"title": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
}
}
}
},
"aliases": {
"myindex_alias": {}
}
}
'
2、Java REST Client
文档:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.13/java-rest-high-create-index.html
CreateIndexRequest request = new CreateIndexRequest("myindex");
// 设置
Settings.Builder settings = Settings.builder()
.put("index.number_of_shards", 3)
.put("index.number_of_replicas", 3);
request.settings(elasticsearchIndexRequest.getSettings());
// 别名
request.alias(new Alias("myindex_alias"));
// 映射
Map<String, Object> name = new HashMap<>();
name.put("type", "keyword");
Map<String, Object> age = new HashMap<>();
age.put("type", "integer");
Map<String, Object> raw = new HashMap<>();
raw.put("type", "keyword");
Map<String, Object> fields = new HashMap<>();
fields.put("raw", raw);
Map<String, Object> title = new HashMap<>();
title.put("type", "text");
title.put("fields", "fields");
Map<String, Object> properties = new HashMap<>();
properties.put("name", name);
properties.put("age", age);
properties.put("title", title);
Map<String, Object> mapping = new HashMap<>();
mapping.put("properties", properties);
request.mapping(mapping);
// 超时时间
request.setTimeout(TimeValue.timeValueMinutes(2));
request.setMasterTimeout(TimeValue.timeValueMinutes(1));
// 请求
CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
if (createIndexResponse.isShardsAcknowledged()) {
return true;
} else {
return false;
}
分类:
03_ElasticSearch
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性