本篇不涉及原理讲解。只记录一些索引的使用方法。
自动索引
使用DSL语言直接插入文档,ES会根据文档内容,自动生成索引。
注意:
1.我们不能为已经建立好的 index 动态修改 mapping。这是因为一旦修改,那么之前建立的索引就变成不能搜索的了。
一种办法是 reindex 从而重新建立我们的索引。如果在之前的 mapping 加入新的字段,那么我们可以不用重新建立索引。
2.映射类型:
字符串类型会映射为 text 同时生成一个keyword字段 支持精确匹配。查询时属性名称要加上keyword
DSL创建索引
例如:使用es 6.8版本
POST 地址/_doc
{
"username":"test",
"age":10
}
映射成:
{
"test_demo": {
"aliases": {},
"mappings": {
"_doc": {
"properties": {
"age": {
"type": "long"
},
"username": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
},
"settings": {
"index": {
"creation_date": "1614566470879",
"number_of_shards": "5",
"number_of_replicas": "1",
"uuid": "3tW38TBRQweLgulWATnERw",
"version": {
"created": "6080199"
},
"provided_name": "test_demo"
}
}
}
}
Java客户端创建索引
=======发送请求的代码=========
RestHighLevelClient client = //构造你的连接
IndexRequest indexRequest = new IndexRequest(索引名字, 文档类别);
//发送es请求
//同步方式获得结果
try {
indexRequest.source(Objects.requireNonNull(JacksonUtil.obj2String(ES文档对应的Java实体类)), XContentType.JSON);
IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
return response.status() == RestStatus.OK;
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
索引模板
假如一类索引有相同的结构,我们可以使用索引模板。
例如创建一个映射好时间字段的索引模板:
PUT _template/learn_template
{
"index_patterns":"learn*",
"order":1,
"settings": {
"number_of_shards": 4,
"number_of_replicas": 1
},
"mappings":{
"properties":{
"@timestamp":{
"type":"date",
"format":"yyyy-MM-dd HH:mm:ss"
}
}
}
}
Java客户端创建索引模板
RestHighLevelClient client = 获得连接
//索引模板的名字
PutIndexTemplateRequest request = new PutIndexTemplateRequest("模板名字");
//这个是创建别名用 所有的索引都可以通过这个别名进行查询到
// request.alias(new Alias("test_index"));
//设置模板的顺序
request.order(1);
//匹配哪些index。在创建index时会生效。
request.patterns(Collections.singletonList("匹配哪些索引*"));
request.settings(Settings.builder()
//数据插入后多久能查到,实时性要求高可以调低
// .put("index.refresh_interval", "10s")
//传输日志,对数据安全性要求高的 设置 request,默认值:request
// .put("index.translog.durability", "async")
// .put("index.translog.sync_interval", "120s")
//集群所有索引的副本最大值+1 <= 集群节点数量 <= 分片数量
//分片数量
.put("index.number_of_shards", "4")
//副本数量
.put("index.number_of_replicas", "2")
//单次最大查询数据的数量。默认10000。不要设置太高,如果有导出需求可以根据查询条件分批次查询。
.put("index.max_result_window", "100000"));
//使用官方提供的工具构建json。可以直接拼接一个json字符串,也可以使用map嵌套。
XContentBuilder jsonMapping = null;
try {
jsonMapping = XContentFactory.jsonBuilder();
jsonMapping.startObject().startObject("properties")
.startObject("@timestamp").field("type", "date").field("format", "yyyy-MM-dd HH:mm:ss||epoch_millis").endObject()
.endObject().endObject();
request.mapping(jsonMapping);
} catch (IOException e) {
e.printStackTrace();
}
//所有数据类型 看官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/7.4/mapping-types.html#_core_datatypes
//设置为true只强制创建,而不是更新索引模板。如果它已经存在,它将失败
request.create(false);
AcknowledgedResponse response = null;
try {
response = client.indices().putTemplate(request, RequestOptions.DEFAULT);
if (response.isAcknowledged()) {
log.info("创建模版成功!");
}
} catch (IOException e) {
e.printStackTrace();
log.error("创建模版失败!");
}