package org.springblade.es.service; import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.json.JsonXContent; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.io.IOException; @Service public class ElasticsearchIndexService { @Autowired private RestHighLevelClient client; public void createIndex() throws IOException { CreateIndexRequest request = new CreateIndexRequest("your_index"); XContentBuilder builder = JsonXContent.contentBuilder() .startObject() .startObject("properties") .startObject("field1").field("type", "text").endObject() .startObject("field2").field("type", "text").endObject() // ... 定义其他字段 .endObject() .endObject(); request.mapping("_doc", builder); CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT); boolean acknowledged = createIndexResponse.isAcknowledged(); System.out.println(acknowledged); } }