原生JAVA操作 Elasticsearch索引

网友投稿 263 2022-11-16

原生JAVA操作 Elasticsearch索引

package com.yqq.app13;import org.apache.org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;import org.elasticsearch.client.RequestOptions;import org.elasticsearch.client.RestClient;import org.elasticsearch.client.RestHighLevelClient;import org.elasticsearch.client.indices.CreateIndexRequest;import org.elasticsearch.client.indices.CreateIndexResponse;import org.elasticsearch.client.indices.PutMappingRequest;import org.elasticsearch.common.settings.Settings;import org.elasticsearch.common.xcontent.XContentType;import org.junit.After;import org.junit.Before;import org.junit.Test;import java.io.IOException;/** * @Author yqq * @Date 2021/11/30 15:30 * @Version 1.0 */public class IndexTest { RestHighLevelClient client; @Before public void init(){ // 创建客户端对象,链接ES client = new RestHighLevelClient( RestClient.builder(new HttpHost("node0",9200," } @Test public void createIndex() throws IOException { // 创建请求对象 CreateIndexRequest request = new CreateIndexRequest("student"); request.settings(Settings.builder() .put("index.number_of_shards",1) .put("index.number_of_replicas",1) ); // 发送请求 CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT); } @Test public void mappingIndex(){ // 创建请求对象 PutMappingRequest request = new PutMappingRequest("student"); request.source("{\"properties\": {" + " \"id\": {" + " \"type\": \"integer\"," + " \"store\": true," + " \"index\": true" + " }," + " \"name\": {" + " \"type\": \"text\"," + " \"store\": true," + " \"index\": true" + " }," + " \"info\": {" + " \"type\": \"text\"," + " \"store\": true," + " \"index\": true" + " }" + " }}", XContentType.JSON); //发送请求 try { client.indices().putMapping(request,RequestOptions.DEFAULT); } catch (IOException e) { e.printStackTrace(); } } @Test public void deleteIndex(){ // 创建请求对象 DeleteIndexRequest request = new DeleteIndexRequest("student"); try { //发送请求 client.indices().delete(request,RequestOptions.DEFAULT); } catch (IOException e) { e.printStackTrace(); } } @After public void close(){ try { client.close(); } catch (IOException e) { e.printStackTrace(); } }}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Elasticsearch集群
下一篇:关于MLK-IM224-4G智能通讯管理机特色功能的介绍
相关文章

 发表评论

暂时没有评论,来抢沙发吧~