ElasticSearch 实战: ES 索引 API

本文详细介绍了Elasticsearch中关于索引管理的操作,包括创建、查看、更新设置、删除索引以及使用Java客户端创建索引的过程,以及相关的配置和示例代码。
摘要由CSDN通过智能技术生成

Elasticsearch 提供了丰富的 RESTful API 用于管理和操作索引,包括创建、查询、更新、删除等操作。以下是对 Elasticsearch 索引 API 的实战指南,涵盖了关键操作的示例和说明:

创建索引

创建一个新的索引时,需要指定索引名,并可以提供索引的配置(如分片数、副本数)、映射(字段类型、分析器等)以及其它设置。

请求格式:

PUT /<index_name>
{
  "settings": {
    "number_of_shards": <shard_count>,
    "number_of_replicas": <replica_count>,
    ...其它设置...
  },
  "mappings": {
    "<doc_type>" : {
      "properties": {
        "<field_name>": {
          "type": "<field_type>",
          ...其它字段属性...
        },
        ...其它字段...
      }
    }
  },
  ...可能包含的其它部分(如 aliases、warmers)...
}
  • <index_name>:要创建的索引名称。
  • settings:索引级别的配置,如分片数(number_of_shards)、副本数(number_of_replicas)等。
  • mappings:定义索引中文档类型(doc_type)及其字段(field_name)的数据类型(field_type)和分析器等属性。从 Elasticsearch 7.x 版本开始,单个索引内不再支持多个文档类型,所以通常只定义一个默认类型(可以省略类型名)。
  • <shard_count><replica_count>:分别表示主分片数量和副本分片数量,根据集群规模和数据分布需求设置。

示例:

PUT /my_index
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 2
  },
  "mappings": {
    "properties": {
      "title": {
        "type": "text"
      },
      "content": {
        "type": "text",
        "analyzer": "english"
      },
      "published_date": {
        "type": "date"
      }
    }
  }
}

查看索引信息

获取指定索引的基本信息,包括设置、映射、状态等。

请求格式:

GET /<index_name>

示例:

GET /my_index

更新索引设置

修改已存在索引的部分或全部设置。

请求格式:

PUT /<index_name>/_settings
{
  "<setting_key>": "<new_value>",
  ...其它设置项...
}

示例:更改副本数:

PUT /my_index/_settings
{
  "number_of_replicas": 1
}

删除索引

删除指定的索引及其所有数据。

请求格式:

DELETE /<index_name>

示例:

DELETE /my_index

索引模板

除了直接操作单个索引,Elasticsearch 还支持创建索引模板,用于自动应用预定义的设置和映射到符合特定模式的新建索引上。

创建模板:

PUT _template/<template_name>
{
  "index_patterns": ["<pattern>"],
  "settings": { ... },
  "mappings": { ... },
  ...其它部分...
}

示例:

PUT _template/my_template
{
  "index_patterns": ["logs-*"],
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0
  },
  "mappings": {
    "properties": {
      "@timestamp": {
        "type": "date"
      },
      "message": {
        "type": "text"
      }
    }
  }
}

使用 Java Client 创建索引

对于 Java 开发者,可以利用官方提供的 Java High Level REST Client 或者更现代的 Java Low Level REST Client 来创建索引。以下是一个使用 High Level REST Client 的简例:

import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;

public class CreateIndexExample {

    public static void main(String[] args) throws Exception {
        RestHighLevelClient client = // 初始化客户端实例...

        XContentBuilder mappingBuilder = XContentFactory.jsonBuilder()
                .startObject()
                    .startObject("properties")
                        .startObject("title")
                            .field("type", "text")
                        .endObject()
                        .startObject("content")
                            .field("type", "text")
                            .field("analyzer", "english")
                        .endObject()
                        .startObject("published_date")
                            .field("type", "date")
                        .endObject()
                    .endObject()
                .endObject();

        CreateIndexRequest request = new CreateIndexRequest("my_index");
        request.settings(Settings.builder()
                .put("number_of_shards", 3)
                .put("number_of_replicas", 2));
        request.mapping(mappingBuilder);

        CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);

        System.out.println("Index created: " + response.isAcknowledged());
    }
}

以上实战指南覆盖了 ElasticSearch 索引 API 的核心操作,包括使用 HTTP 请求直接操作和通过 Java 客户端编程接口进行索引管理。实际使用中,应结合具体业务需求和 Elasticsearch 版本特性进行相应的调整。

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值