ElasticSearch RestHighLevelClient 教程(二) 操作index

前言

官方推荐的RestHighLevelClient在网络上相关文档较少,因此很多坑都需要通过自己看官方文档,源代码解决。因此这系列文章就是为了解决这些文档提及不全的部分,如果有文章没提到的部分,欢迎一起交流。

这篇就关于使用RestClient操作索引,内容就是解决问题的实例。同样,一切版本以5.6.0为准。其他升级版本新的api不属此列。

正文

问题根源

​ 由于使用RestHighLevelClient(后称为rhlClient)时,进行Index操作,所有IndexRequest都会校验Index,type,source,contentType不为空。

 // 校验源码
if (type == null) {
            validationException = addValidationError("type is missing", validationException);
        }
        if (source == null) {
            validationException = addValidationError("source is missing", validationException);
        }
        if (contentType == null) {
            validationException = addValidationError("content type is missing", validationException);
        }

​ 所以,如果只创建索引时一定会遇到校验抛出异常,而是否有其他方法绕过校验不得而知,而本人的方法是使用老版本RestClient直接操作。虽然在封装上会比rhlClient简陋,但是同时具备了更高的灵活性。

操作实例
  1. 创建索引

    首先给出rest api 操作的格式:

    因为整合了ik中文分词器,所以“analyzer”: “ik_max_word”此处参考安装配置ik分词器

    /PUT {{host}}:{{port}}/demo
    {
    "mappings":{
        "doc":{
            "properties":{
                "title":{
                    "type":"text",
                     "analyzer": "ik_max_word"
                },
                "content":{
                    "type":"text",
                     "analyzer": "ik_max_word"
                },
                "uniqueId":{
                    "type":"keyword",
                    "index":"not_analyzed"
                },
                "created":  {
                    "type":   "date", 
                    "format": "strict_date_optional_time||epoch_millis"
                }
            }
        }
    },
    "settings":{
            "number_of_shards":3,
            "number_of_replicas":1
    }
    }
    @Test
    public void indexTest() {
        try {
                // 借助indexRequest的json拼接工具
            IndexRequest indexRequest = new IndexRequest();
            XContentBuilder builder = JsonXContent.contentBuilder()
                    .startObject()
                        .startObject("mappings")
                            .startObject("doc")
                                .startObject("properties")
                                    .startObject("title")
                                        .field("type","text")
                                        .field("analyzer","ik_max_word")
                                    .endObject()
                                    .startObject("content")
                                        .field("type","text")
                                        .field("index","analyzed")
                                        .field("analyzer","ik_max_word")
                                    .endObject()
                                    .startObject("uniqueId")
                                        .field("type","keyword")
                                        .field("index","not_analyzed")
                                    .endObject()
                                    .startObject("created")
                                        .field("type","date")
                                        .field("format","strict_date_optional_time||epoch_millis")
                                    .endObject()
                                .endObject()
                            .endObject()
                        .endObject()
                        .startObject("settings")
                            .field("number_of_shards",3)
                            .field("number_of_replicas",1)
                        .endObject()
                    .endObject();
            indexRequest.source(builder);
                // 生成json字符串
            String source = indexRequest.source().utf8ToString();
            HttpEntity entity = new NStringEntity(source, ContentType.APPLICATION_JSON);
           // 使用RestClient进行操作 而非rhlClient
                Response response = client.performRequest("put", "/demo", Collections.<String, String> emptyMap(),
                    entity);
            System.out.println(response);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    
    }
    // 结果
    /GET {{host}}:{{port}}/demo
    {
       "demo": {
           "aliases": {},
           "mappings": {
               "doc": {
                   "properties": {
                       "content": {
                           "type": "text",
                           "analyzer": "ik_max_word"
                       },
                       "created": {
                           "type": "date"
                       },
                       "title": {
                           "type": "text",
                           "analyzer": "ik_max_word"
                       },
                       "uniqueId": {
                           "type": "keyword"
                       }
                   }
               }
           },
           "settings": {
               "index": {
                   "creation_date": "1515043588766",
                   "number_of_shards": "3",
                   "number_of_replicas": "1",
                   "uuid": "8a7GoGgwQFyr8s3Ehs8HSA",
                   "version": {
                       "created": "5060099"
                   },
                   "provided_name": "demo"
               }
           }
       }
    }

    可以看到映射和设置已完成配置。

    如果有其他需要的配置,可以参考官网api,然后只要拼接对应的json字符串,使用RestClient请求即可。

  2. 创建类型映射

    首先必须保证索引存在,才能创建类型映射。

    /POST {{host}}:{{port}}/newspaper/sports/_mapping
    {
    "properties":{
        "content":{
            "type":"text",
            "analyzer":"ik_max_word",
            "index":"analyzed"
        }
    }
    }
    @Test
    public void indexTest2() {
        try {
            IndexRequest indexRequest = new IndexRequest();
            XContentBuilder builder = JsonXContent.contentBuilder()
                    .startObject()
                        .startObject("properties")
                            .startObject("content")
                                .field("type","text")
                                .field("analyzer","ik_max_word")
                                .field("index","analyzed")
                            .endObject()
                        .endObject()
                    .endObject();
            indexRequest.source(builder);
            String source = indexRequest.source().utf8ToString();
            HttpEntity entity = new NStringEntity(source, ContentType.APPLICATION_JSON);
            Response response = client.performRequest("post", "/newspaper/sports/_mapping", Collections.<String, String> emptyMap(),
                    entity);
            System.out.println(response);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
    /GET {{host}}:{{port}}/newspaper
    {
       "newspaper": {
           "aliases": {},
           "mappings": {
               "sports": {
                   "properties": {
                       "content": {
                           "type": "text",
                           "analyzer": "ik_max_word"
                       }
                   }
               }
           },
           "settings": {
               "index": {
                   "creation_date": "1515045134942",
                   "number_of_shards": "5",
                   "number_of_replicas": "1",
                   "uuid": "HZ7b3ey1TNu2erzTtG-1Fg",
                   "version": {
                       "created": "5060099"
                   },
                   "provided_name": "newspaper"
               }
           }
       }
    }

    创建sports类型映射成功。主要点还是在于拼接json字符串。

  3. 查看索引是否存在

    public boolean checkIndexExist(String index) throws IOException {
        Response response = restClient.performRequest("HEAD", index);
        boolean exist = response.getStatusLine().getReasonPhrase().equals("OK");
        return exist;
    }
  4. 删除索引

    /DELETE {{host}}:{{port}}/newspaper/
    Response response = restClient.performRequest("DELETE", indexName);

总结

​ 以上便是对索引的大部分操作,可以看到绝大部分都是通过restClient绕过rhlClient的空类型检测。所以如果还有其他对索引的需求,也可以尝试使用IndexRequest的Json拼接工具,拼接好Json字符串后,使用restClient发出请求。

  • 7
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你可以使用 RestHighLevelClient 来创建 Elasticsearch 索引。以下是一个示例代码: ```java import org.apache.http.HttpHost; import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentType; import java.io.IOException; public class IndexCreationExample { public static void main(String[] args) throws IOException { RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http"))); CreateIndexRequest request = new CreateIndexRequest("your_index_name"); request.settings(Settings.builder() .put("index.number_of_shards", 1) .put("index.number_of_replicas", 1)); // 设置索引映射 String mapping = """ { "properties": { "title": { "type": "text" }, "content": { "type": "text" } } } """; request.mapping(mapping, XContentType.JSON); CreateIndexResponse response = client.indices().create(request); if (response.isAcknowledged()) { System.out.println("索引创建成功"); } else { System.out.println("索引创建失败"); } client.close(); } } ``` 你需要将 `localhost` 和 `9200` 替换为你的 Elasticsearch 主机和端口,`"your_index_name"` 替换为你想要创建的索引的名称。在 `request.mapping()` 中设置索引的映射,上述示例中的映射包含了 `title` 和 `content` 字段,你可以根据你的需求进行调整。 你可以根据实际情况修改其他设置,比如 `index.number_of_shards` 和 `index.number_of_replicas`,然后调用 `client.indices().create(request)` 方法来创建索引。创建成功后,`response.isAcknowledged()` 返回 true。 记得在使用完 RestHighLevelClient 后关闭客户端 `client.close()`。希望这能对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值