Elasticsearch5.3 用JAVA创建索引
来源:互联网 发布:html5权威指南 源码 编辑:程序博客网 时间:2024/05/16 11:41
创建索引
1,首先我们要有已经搭建好的Elasticsearch 并且是没问题的!
废话不多说直接上代码了
import com.google.gson.Gson;import org.elasticsearch.action.index.IndexRequestBuilder;import org.elasticsearch.action.index.IndexResponse;import org.elasticsearch.client.transport.TransportClient;import org.elasticsearch.common.settings.Settings;import org.elasticsearch.common.transport.InetSocketTransportAddress;import org.elasticsearch.rest.RestStatus;import org.elasticsearch.transport.client.PreBuiltTransportClient;import org.junit.After;import org.junit.Before;import org.junit.Test;import java.io.IOException;import java.net.InetAddress;import java.net.UnknownHostException;import java.util.HashMap;import java.util.Map;//import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;/** * Created by 启才 on 2017/4/25. */public class BookRepositoryTest { TransportClient client; @Before public void setup() throws UnknownHostException { System.out.println("create TransportClient..."); client = new PreBuiltTransportClient(Settings.EMPTY) .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("....."), 9300)); } @After public void clearup() { System.out.println("close TransportClient..."); if (client != null) { client.close(); } } @Test public void doIndex() throws IOException { Gson gson = new Gson(); Map map = new HashMap(); map.put("title", "hello test"); map.put("description", "你好世界"); map.put("price", 25.3); map.put("onSale", "asdasdasda"); map.put("type", "testtypesssss"); map.put("createDate", "2017.0000"); String json = gson.toJson(map); IndexRequestBuilder indexRequestBuilder = client.prepareIndex("wang", "test", "10").setSource(json); IndexResponse response = indexRequestBuilder.get(); // Index name String _index = response.getIndex();// Type name String _type = response.getType();// Document ID (generated or not) String _id = response.getId();// Version (if it's the first time you index this document, you will get: 1) long _version = response.getVersion();// status has stored current instance statement. RestStatus status = response.status(); }}
我们会发现这种创建方式与其他版本的创建方式有那么一点不同,尤其是创建client的时候!
并不是所有的创建方式都是一样的!
创建完成后我们可以通过kibana来查看
过几天我还会有ES的 JAVA API 的各种查询希望能帮助大家!
阅读全文
0 0
- Elasticsearch5.3 用JAVA创建索引
- elasticsearch5.3.0 索引创建及查询
- Elasticsearch5.3 如何创建mapping demo
- elasticsearch5 源码 索引过程
- Elasticsearch5.X索引
- elasticsearch5.2.2使用java API创建transport client
- elasticsearch5.3集群 java 调用实例
- Elasticsearch5.3 JAVA Demo 聚合查询
- Elasticsearch5.3 JAVA代码添加数据 Demo
- elasticsearch5.5.2手动创建索引并配置ik中文分词器
- java 创建索引、删除索引(background)
- elasticsearch5.3linux集群
- 用Lucene创建索引
- elasticsearch5.4.0 java开发注意事项
- elasticsearch5.4的java api
- Elasticsearch5 JAVA API数据遍历
- Elasticsearch5.4 Java编程注意事项
- Elasticsearch5.4 Java API操作
- win10下openssl生成证书过程
- mysql api编程(三) 事务
- js排序算法
- swagger ui应用
- mac下solr6.5的安装与配置
- Elasticsearch5.3 用JAVA创建索引
- 动态按比例绘制波形的思路以及按关系缩放公式
- 科大讯飞人脸识别,语音听写,声纹识别踩坑记
- 软键盘
- AlertDialog字体大小设置 确认取消按钮字体大小设置
- 移植OPENCORES上的I2C MASTER到AXI接口处理器层面实现驱动 之一
- 本地对象之String
- hud上的动态规划练习
- Spring Cloud中,Feign常见问题总结