java创建es索引,二、ElasticSearch 使用java接口创建索引及操作文档

ElasticSearch (2)使用java接口创建索引及操作文档

所有Elasticsearch操作都是使用Client对象执行的。Client 定义的所有API都是异步执行的(要么使用事件监听器回调或者使用Future模式)。此外,客户端上的操作可以批量累积和执行。

Elasticsearch官方计划在Elasticsearch 7.0中弃TransportClient,并在8.0中完全删除它。故,应该使用Java高级REST client,rest client执行HTTP请求来执行操作,无需再序列化的Java请求。Java高级REST Client API目前支持更常用的api,但还需要添加更多的api。

1、连接ElasticSearch

新建一个maven项目,在pom.xml文件中添加依赖: (注意版本对应)

org.elasticsearch.client

transport

5.5.2

com.google.code.gson

gson

2.8.2

连接的代码:

package com.asiainfo.test;

import org.elasticsearch.client.transport.TransportClient;

import org.elasticsearch.common.settings.Settings;

import org.elasticsearch.common.transport.InetSocketTransportAddress;

import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;

public class ConnTest {

// elasticsearch的端口和ip

private static String host = "10.21.13.48";

private static int port = 9300;

public static void main(String[] args) throws Exception {

@SuppressWarnings({ "resource", "uncheck

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你回答这个问题。 使用Java客户端创建索引和映射,需要连接Elasticsearch服务器,并使用Elasticsearch Java API进行操作。以下是一个简单的示例代码,用于创建索引和映射: ```java import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.json.JsonXContent; import java.io.IOException; public class ElasticSearchIndexAndMapping { private static final String INDEX_NAME = "example_index"; private static final String TYPE_NAME = "example_type"; public static void main(String[] args) throws IOException { RestHighLevelClient client = new RestHighLevelClient(/* your Elasticsearch client configuration */); // Create index request CreateIndexRequest request = new CreateIndexRequest(INDEX_NAME); // Create index mapping XContentBuilder builder = XContentFactory.jsonBuilder(); builder.startObject(); { builder.startObject("properties"); { builder.startObject("field1"); { builder.field("type", "text"); } builder.endObject(); } builder.endObject(); } builder.endObject(); // Put mapping request PutMappingRequest mappingRequest = new PutMappingRequest(INDEX_NAME); mappingRequest.type(TYPE_NAME); mappingRequest.source(builder); // Execute requests client.indices().create(request, RequestOptions.DEFAULT); client.indices().putMapping(mappingRequest, RequestOptions.DEFAULT); client.close(); } } ``` 在此示例中,我们使用了 `CreateIndexRequest` 和 `PutMappingRequest` 类来创建索引和映射。我们首先创建了一个名为 `example_index` 的索引,并指定了一个名为 `example_type` 的类型。然后,我们使用 `XContentBuilder` 来构建映射。在此示例中,我们只创建了一个名为 `field1` 的字段,并将其类型设置为 `text`。最后,我们使用 `RestHighLevelClient` 来执行创建索引和映射的请求。 请注意,以上示例仅表示如何创建索引和映射。如果您需要更详细的功能,例如添加文档或查询索引,请查看Elasticsearch Java API文档

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值