Elasticsearch常用操作解析
发表于:2024-09-22 作者:千家信息网编辑
千家信息网最后更新 2024年09月22日,创建Maven管理的Java项目在pom.xml中添加依赖:6.1.1 org.elasticsearch.client transport ${es.ve
千家信息网最后更新 2024年09月22日Elasticsearch常用操作解析
创建Maven管理的Java项目
在pom.xml中添加依赖:
6.1.1 org.elasticsearch.client transport ${es.version}
然后创建一个单元测试类ESApp:
private TransportClient client; @Before public void setUp() throws Exception { Settings settings = Settings.builder() .put("cluster.name", "mycluster") .put("client.transport.sniff", "true")//增加自动嗅探配置 .build(); client = new PreBuiltTransportClient(settings); client.addTransportAddress(new TransportAddress(InetAddress.getByName("10.8.24.94"), 9300)); System.out.println(client.toString()); }
运行后报错
java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonFactory com.fasterxml.jackson.core jackson-core 2.9.3 com.fasterxml.jackson.core jackson-databind 2.9.3 com.fasterxml.jackson.core jackson-annotations 2.9.3
运行后成功拿到ES的client:
创建一个Index
@Test public void createIndex() { client.admin().indices().prepareCreate(INDEX).get(); System.out.println("创建Index成功"); }
删除一个Index
@Test public void deleteIndex() { client.admin().indices().prepareDelete(INDEX).get(); System.out.println("删除Index成功"); }
放入数据的三种方式
//不推荐使用,太繁琐拼json格式 @Test public void createDoc() { String json = "{\"name\":\"若泽数据\"}"; IndexResponse response = client.prepareIndex(INDEX, TYPE, "100") .setSource(json, XContentType.JSON) .get(); } //推荐使用 @Test public void test01() throws Exception { Map json = new HashMap(); json.put("name", "ruozedata"); json.put("message", "trying out Elasticsearch"); IndexResponse response = client.prepareIndex(INDEX, TYPE, "101").setSource(json).get(); System.out.println(response.getVersion()); }//推荐使用 @Test public void test02() throws Exception { XContentBuilder builder = jsonBuilder() .startObject() .field("user", "ruoze") .field("postDate", new Date()) .field("message", "trying out Elasticsearch") .endObject(); IndexResponse response = client.prepareIndex(INDEX, TYPE, "102").setSource(builder).get(); System.out.println(response.getVersion()); }
拿到一条数据
@Test public void getDoc() { GetResponse response = client.prepareGet(INDEX, TYPE, "100").get(); System.out.println(response.getSourceAsString()); }
拿到多条数据
@Test public void getDocsByIds() { MultiGetResponse responses = client.prepareMultiGet() .add(INDEX, TYPE,"100") .add(INDEX, TYPE, "101", "102", "1000") .get(); for (MultiGetItemResponse response : responses) { GetResponse res = response.getResponse(); if (res.isExists()) { System.out.println(res); } else { System.out.println("没有这条数据"); } } }
数据
推荐
成功
运行
繁琐
单元
多条
方式
格式
项目
若泽
后成
测试
管理
配置
常用
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
服务器bmc登录方式
数据库系统绪论笔记
方舟端游火影服务器推荐
网络安全设计摘要
数据库实施包括哪些步骤
sql的数据库名
传输网络技术华为
杭州边锋网络技术公司电话
信息网络技术培训心得
游戏用什么服务器
数据库的表怎么设计
兰州大学网络安全实验室
黄浦区会计数据库系统职能
郑州软件开发费用多少钱
网络安全法 知识题目
小程序更换服务器后打不开了
网络安全学院运动场
大华600路录像机服务器
大专学软件开发怎么找工作
腾讯云服务器挖矿行为自查
上市前临床安全性数据库规模
vf创建数据库的步骤
嵌入式软件开发测试流程
服务器方向和后端
梦幻西游深夜服务器还会掉线吗
煤矿公司用服务器
数据库可视化交互开发工具
房山区数据网络技术咨询哪家好
联想服务器换主硬盘
密云区正规软件开发单价