Search in sources :

Example 6 with PutIndexTemplateRequest

use of org.elasticsearch.client.indices.PutIndexTemplateRequest in project kms by mahonelau.

the class KmEsMgntServiceImpl method initKmDocTemplate.

@Override
public Result<?> initKmDocTemplate() throws IOException {
    PutIndexTemplateRequest request = new PutIndexTemplateRequest(KMConstant.DocIndexName);
    // 别名,所有根据该模版创建的index 都会添加这个别名。查询时可查询别名,就可以同时查询多个名称不同的index,根据此方法可实现index每天或每月生成等逻辑。
    request.alias(new Alias(KMConstant.DocIndexAliasName));
    // 匹配哪些index。在创建index时会生效。
    request.patterns(CollUtil.newArrayList(KMConstant.DocIndexName + "*"));
    request.order(3);
    request.settings(Settings.builder().put("index.refresh_interval", "10s").put("index.translog.durability", "async").put("index.translog.sync_interval", "120s").put("index.number_of_shards", "2").put("index.number_of_replicas", "0").put("index.max_result_window", "1000"));
    // 使用官方提供的工具构建json。可以直接拼接一个json字符串,也可以使用map嵌套。
    XContentBuilder jsonMapping = XContentFactory.jsonBuilder();
    // 所有数据类型 看官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/7.4/mapping-types.html#_core_datatypes
    // keyword类型不会分词存储
    jsonMapping.startObject().startObject("properties").startObject("id").field("type", "keyword").endObject().startObject("releaseFlag").field("type", "integer").endObject().startObject("publicFlag").field("type", "integer").endObject().startObject("category").field("type", "integer").endObject().startObject("fileNo").field("type", "keyword").endObject().startObject("source").field("type", "integer").endObject().startObject("pubTime").field("type", "integer").endObject().startObject("title").field("type", "text").field("analyzer", "ik_max_word").endObject().startObject("content").field("type", "text").field("analyzer", "ik_max_word").endObject().startObject("versions").field("type", "integer").endObject().startObject("businessTypes").field("type", "integer").endObject().startObject("keywords").field("type", "keyword").endObject().startObject("topicCodes").field("type", "keyword").endObject().endObject().endObject();
    request.mapping(jsonMapping);
    // 设置为true只强制创建,而不是更新索引模板。如果它已经存在,它将失败
    request.create(false);
    AcknowledgedResponse response = restHighLevelClient.indices().putTemplate(request, RequestOptions.DEFAULT);
    if (response.isAcknowledged()) {
        return Result.OK("创建模版成功!");
    } else {
        return Result.error("创建模版失败!");
    }
}
Also used : Alias(org.elasticsearch.action.admin.indices.alias.Alias) AcknowledgedResponse(org.elasticsearch.action.support.master.AcknowledgedResponse) PutIndexTemplateRequest(org.elasticsearch.client.indices.PutIndexTemplateRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 7 with PutIndexTemplateRequest

use of org.elasticsearch.client.indices.PutIndexTemplateRequest in project spring-data-elasticsearch by spring-projects.

the class ReactiveIndexTemplate method putTemplate.

// endregion
// region templates
@Override
public Mono<Boolean> putTemplate(PutTemplateRequest putTemplateRequest) {
    Assert.notNull(putTemplateRequest, "putTemplateRequest must not be null");
    PutIndexTemplateRequest putIndexTemplateRequest = requestFactory.putIndexTemplateRequest(putTemplateRequest);
    return Mono.from(operations.executeWithIndicesClient(client -> client.putTemplate(putIndexTemplateRequest)));
}
Also used : AnnotatedElementUtils(org.springframework.core.annotation.AnnotatedElementUtils) GetMappingsRequest(org.elasticsearch.client.indices.GetMappingsRequest) AliasActions(org.springframework.data.elasticsearch.core.index.AliasActions) Settings(org.springframework.data.elasticsearch.core.index.Settings) PutIndexTemplateRequest(org.elasticsearch.client.indices.PutIndexTemplateRequest) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) ElasticsearchPersistentEntity(org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity) Mapping(org.springframework.data.elasticsearch.annotations.Mapping) ElasticsearchConverter(org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter) GetAliasesResponse(org.elasticsearch.client.GetAliasesResponse) Map(java.util.Map) Nullable(org.springframework.lang.Nullable) Requests(org.elasticsearch.client.Requests) AliasData(org.springframework.data.elasticsearch.core.index.AliasData) ReactiveMappingBuilder(org.springframework.data.elasticsearch.core.index.ReactiveMappingBuilder) TemplateData(org.springframework.data.elasticsearch.core.index.TemplateData) IndexTemplatesExistRequest(org.elasticsearch.client.indices.IndexTemplatesExistRequest) DeleteTemplateRequest(org.springframework.data.elasticsearch.core.index.DeleteTemplateRequest) ExistsTemplateRequest(org.springframework.data.elasticsearch.core.index.ExistsTemplateRequest) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) CreateIndexRequest(org.elasticsearch.client.indices.CreateIndexRequest) IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates) GetAliasesRequest(org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest) GetIndexTemplatesRequest(org.elasticsearch.client.indices.GetIndexTemplatesRequest) Set(java.util.Set) IndicesAliasesRequest(org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest) Mono(reactor.core.publisher.Mono) PutTemplateRequest(org.springframework.data.elasticsearch.core.index.PutTemplateRequest) DeleteIndexTemplateRequest(org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest) Flux(reactor.core.publisher.Flux) GetTemplateRequest(org.springframework.data.elasticsearch.core.index.GetTemplateRequest) NoSuchIndexException(org.springframework.data.elasticsearch.NoSuchIndexException) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) GetSettingsRequest(org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest) GetIndexRequest(org.elasticsearch.client.indices.GetIndexRequest) Document(org.springframework.data.elasticsearch.core.document.Document) StringUtils(org.springframework.util.StringUtils) Assert(org.springframework.util.Assert) PutIndexTemplateRequest(org.elasticsearch.client.indices.PutIndexTemplateRequest)

Example 8 with PutIndexTemplateRequest

use of org.elasticsearch.client.indices.PutIndexTemplateRequest in project spring-data-elasticsearch by spring-projects.

the class RestIndexTemplate method putTemplate.

@Override
public boolean putTemplate(PutTemplateRequest putTemplateRequest) {
    Assert.notNull(putTemplateRequest, "putTemplateRequest must not be null");
    PutIndexTemplateRequest putIndexTemplateRequest = requestFactory.putIndexTemplateRequest(putTemplateRequest);
    return restTemplate.execute(client -> client.indices().putTemplate(putIndexTemplateRequest, RequestOptions.DEFAULT).isAcknowledged());
}
Also used : PutIndexTemplateRequest(org.elasticsearch.client.indices.PutIndexTemplateRequest)

Example 9 with PutIndexTemplateRequest

use of org.elasticsearch.client.indices.PutIndexTemplateRequest in project kms by mahonelau.

the class KmEsMgntServiceImpl method initKmDocVisitTemplate.

@Override
public Result<?> initKmDocVisitTemplate() throws IOException {
    PutIndexTemplateRequest request = new PutIndexTemplateRequest(KMConstant.DocVisitIndexName);
    request.alias(new Alias(KMConstant.DocVisitIndexAliasName));
    request.patterns(CollUtil.newArrayList(KMConstant.DocVisitIndexName + "*"));
    request.order(1);
    request.settings(Settings.builder().put("index.refresh_interval", "60s").put("index.translog.durability", "async").put("index.translog.sync_interval", "600s").put("index.number_of_shards", "2").put("index.number_of_replicas", "0").put("index.max_result_window", "10000"));
    XContentBuilder jsonMapping = XContentFactory.jsonBuilder();
    jsonMapping.startObject().startObject("properties").startObject("id").field("type", "keyword").endObject().startObject("visitType").field("type", "integer").endObject().startObject("createBy").field("type", "keyword").endObject().startObject("keywords").field("type", "text").field("analyzer", "ik_smart").endObject().startObject("keywordsMax").field("type", "keyword").endObject().startObject("createTime").field("type", "date").field("format", "yyyy-MM-dd HH:mm:ss").endObject().startObject("sourceIp").field("type", "ip").endObject().endObject().endObject();
    request.mapping(jsonMapping);
    request.create(false);
    AcknowledgedResponse response = restHighLevelClient.indices().putTemplate(request, RequestOptions.DEFAULT);
    if (response.isAcknowledged()) {
        return Result.OK("创建模版成功!");
    } else {
        return Result.error("创建模版失败!");
    }
}
Also used : Alias(org.elasticsearch.action.admin.indices.alias.Alias) AcknowledgedResponse(org.elasticsearch.action.support.master.AcknowledgedResponse) PutIndexTemplateRequest(org.elasticsearch.client.indices.PutIndexTemplateRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 10 with PutIndexTemplateRequest

use of org.elasticsearch.client.indices.PutIndexTemplateRequest in project kms by mahonelau.

the class KmEsMgntServiceImpl method initKmSearchRecordTemplate.

@Override
public Result<?> initKmSearchRecordTemplate() throws IOException {
    PutIndexTemplateRequest request = new PutIndexTemplateRequest(KMConstant.KMSearchRecordIndexName);
    request.alias(new Alias(KMConstant.KMSearchRecordIndexAliasName));
    request.patterns(CollUtil.newArrayList(KMConstant.KMSearchRecordIndexName + "*"));
    request.settings(Settings.builder().put("index.refresh_interval", "60s").put("index.translog.durability", "async").put("index.translog.sync_interval", "600s").put("index.number_of_shards", "2").put("index.number_of_replicas", "0").put("index.max_result_window", "10000"));
    XContentBuilder jsonMapping = XContentFactory.jsonBuilder();
    jsonMapping.startObject().startObject("properties").startObject("title").field("type", "text").field("analyzer", "ik_smart").endObject().startObject("content").field("type", "text").field("analyzer", "ik_smart").endObject().startObject("keywords").field("type", "text").field("analyzer", "ik_smart").endObject().startObject("keywordsMax").field("type", "keyword").endObject().startObject("topicCodes").field("type", "keyword").endObject().startObject("createBy").field("type", "keyword").endObject().startObject("createTime").field("type", "date").field("format", "yyyy-MM-dd HH:mm:ss").endObject().startObject("sourceIp").field("type", "ip").endObject().endObject().endObject();
    request.mapping(jsonMapping);
    request.create(false);
    AcknowledgedResponse response = restHighLevelClient.indices().putTemplate(request, RequestOptions.DEFAULT);
    if (response.isAcknowledged()) {
        return Result.OK("创建模版成功!");
    } else {
        return Result.error("创建模版失败!");
    }
}
Also used : Alias(org.elasticsearch.action.admin.indices.alias.Alias) AcknowledgedResponse(org.elasticsearch.action.support.master.AcknowledgedResponse) PutIndexTemplateRequest(org.elasticsearch.client.indices.PutIndexTemplateRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Aggregations

PutIndexTemplateRequest (org.elasticsearch.client.indices.PutIndexTemplateRequest)8 Alias (org.elasticsearch.action.admin.indices.alias.Alias)4 AcknowledgedResponse (org.elasticsearch.action.support.master.AcknowledgedResponse)3 AliasActions (org.springframework.data.elasticsearch.core.index.AliasActions)3 IndicesAliasesRequest (org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest)2 GetAliasesRequest (org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest)2 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)2 GetSettingsRequest (org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest)2 DeleteIndexTemplateRequest (org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest)2 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)2 PutIndexTemplateRequest (org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.PutIndexTemplateRequest)2 Map (java.util.Map)1 Set (java.util.Set)1 Log (org.apache.commons.logging.Log)1 LogFactory (org.apache.commons.logging.LogFactory)1 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)1 ClusterHealthRequest (org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest)1 DeleteStoredScriptRequest (org.elasticsearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest)1 GetStoredScriptRequest (org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptRequest)1 PutStoredScriptRequest (org.elasticsearch.action.admin.cluster.storedscripts.PutStoredScriptRequest)1