原生JAVA操作 Elasticsearch索引
原创
wx62be9d88ce294博主文章分类:大数据 ©著作权
文章标签 java elasticsearch 大数据 json 文章分类 Hadoop 大数据
©著作权归作者所有:来自51CTO博客作者wx62be9d88ce294的原创作品,请联系作者获取转载授权,否则将追究法律责任
package com.yqq.app13;import org.apache.http.HttpHost;import 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; public class IndexTest { RestHighLevelClient client; public void init(){ client = new RestHighLevelClient( RestClient.builder(new HttpHost("node0",9200,"http"))); } 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); } 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(); } } public void deleteIndex(){ DeleteIndexRequest request = new DeleteIndexRequest("student"); try { client.indices().delete(request,RequestOptions.DEFAULT); } catch (IOException e) { e.printStackTrace(); } } public void close(){ try { client.close(); } catch (IOException e) { e.printStackTrace(); } }}
- 赞
- 收藏
- 评论
- *举报
下一篇:Elasticsearch集群
Original: https://blog.51cto.com/u_15704423/5434939
Author: wx62be9d88ce294
Title: 原生JAVA操作 Elasticsearch索引
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/516872/
转载文章受原作者版权保护。转载请注明原作者出处!