ElasticSearch(三):Java操作ElasticSearch索引之CRUD
package com.gxy.ESChap01; import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map; import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import com.google.gson.JsonObject; public class ESIndex { private static String host="192.168.56.3"; // 服务器地址
private static int port=9300; // 端口 private TransportClient client=null; //获取客户端
@SuppressWarnings({ "resource", "unchecked" })
@Before
public void getClient() throws Exception {
try {
client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host),port));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} //关闭客户端
@After
public void close() {
if(client!=null) {
client.close();
}
}
/***
* 创建索引,添加文档
* @throws Exception
*/
@Test
public void testIndex() throws Exception {
IndexResponse response=client.prepareIndex("book","java","1")
.setSource(XContentFactory.jsonBuilder()
.startObject()
.field("name","MySQL从入门到删库跑路")
.field("publishDate","2012-11-11")
.field("price",100)
.endObject()).get();
System.out.println("索引名称:"+response.getIndex());
System.out.println("类型:"+response.getType());
System.out.println("文档ID:"+response.getId());
System.out.println("当前实例状态:"+response.status()); }
/**
* 添加文档,创建索引
*/
@Test
public void TestIndex2() {
JsonObject jsonObiect=new JsonObject();
jsonObiect.addProperty("name", "java从入门到放弃");
jsonObiect.addProperty("publishDate", "2018-05-06");
jsonObiect.addProperty("price", 62);
System.out.println(jsonObiect);
IndexResponse response=client.prepareIndex("book", "java", "2")
.setSource(jsonObiect.toString(), XContentType.JSON).get();
System.out.println("索引名称:"+response.getIndex());
System.out.println("类型:"+response.getType());
System.out.println("文档ID:"+response.getId());
System.out.println("当前实例状态:"+response.status()); } /**
* 根据ID获取文档
*/
@Test
public void testGetById() {
GetResponse response=client.prepareGet("book","java","1").get();
System.out.println(response.getSourceAsString());
}
/***
* 根据ID修改文档
* @throws Exception
* @throws InterruptedException
*/
@Test
public void testUpdate() throws InterruptedException, Exception {
UpdateRequest request=new UpdateRequest("book","java","1");
Map<String,Object> source = new HashMap<String, Object>();
source.put("name", "python网络爬虫");
source.put("publishDate", "2012-11-12");
source.put("price", 102);
request.doc(source);
UpdateResponse response=client.update(request).get();
System.out.println("索引名称:"+response.getIndex());
System.out.println("类型:"+response.getType());
System.out.println("文档ID:"+response.getId());
System.out.println("当前实例状态:"+response.status());
}
/**
* 根据ID删除文档
*/
@Test
public void testDelete() {
DeleteResponse response=client.prepareDelete("book","java","1").get();
System.out.println("索引名称:"+response.getIndex());
System.out.println("类型:"+response.getType());
System.out.println("文档ID:"+response.getId());
System.out.println("当前实例状态:"+response.status());
}
}
注意:ElasticSearch中通过HTTP请求的端口为9200,通过Java API 请求的端口为9300!!!
ElasticSearch(三):Java操作ElasticSearch索引之CRUD的更多相关文章
- java操作elasticsearch实现基本的增删改查操作
一.在进行java操作elasticsearch之前,请确认好集群的名称及对应的ES节点ip和端口 1.查看ES的集群名称 #进入elasticsearch.yml配置文件/opt/elasticse ...
- Java操作ElasticSearch之创建客户端连接
Java操作ElasticSearch之创建客户端连接 3 发布时间:『 2017-09-11 17:02』 博客类别:elasticsearch 阅读(3157) Java操作ElasticSe ...
- java操作elasticsearch实现组合桶聚合
1.terms分组查询 //分组聚合 @Test public void test40() throws UnknownHostException{ //1.指定es集群 cluster.name 是 ...
- java操作elasticsearch实现query String
1.CommonTersQuery: 指定字段进行模糊查询 //commonTermsQuery @Test public void test35() throws UnknownHostExcept ...
- java操作elasticsearch实现聚合查询
1.max 最大值 //max 求最大值 @Test public void test30() throws UnknownHostException{ //1.指定es集群 cluster.name ...
- java操作elasticsearch实现前缀查询、wildcard、fuzzy模糊查询、ids查询
1.前缀查询(prefix) //prefix前缀查询 @Test public void test15() throws UnknownHostException { //1.指定es集群 clus ...
- java操作elasticsearch实现条件查询(match、multiMatch、term、terms、reange)
1.条件match query查询 //条件查询match query @Test public void test10() throws UnknownHostException { //1.指定e ...
- java操作elasticsearch实现查询删除和查询所有
后期博客本人都只给出代码,具体的说明在代码中也有注释. 1.查询删除 //查询删除:将查询到的数据进行删除 @Test public void test8() throws UnknownHostEx ...
- java操作elasticsearch实现批量添加数据(bulk)
java操作elasticsearch实现批量添加主要使用了bulk 代码如下: //bulk批量操作(批量添加) @Test public void test7() throws IOExcepti ...
- elasticsearch基本操作之--使用java操作elasticsearch
/** * 系统环境: vm12 下的centos 7.2 * 当前安装版本: elasticsearch-2.4.0.tar.gz */ es 查询共有4种查询类型 QUERY_AND_FETCH: ...
随机推荐
- 【Linux】nginx常用命令
相关内容链接 Centos之安装Nginx及注意事项 [nginx]详细配置说明 nginx常用命令 [重新加载配置]sudo nginx -s reload [打开nginx配置]sudo vim ...
- 推荐好用的JavaScript模块
译者按: 作者将自己常用的JavaScript模块分享给大家. 原文:
- FE 命令随笔
FE_CMD ————— * >>>>>>>> Vue ________________________________________________ ...
- 照葫芦画瓢系列之Java --- Maven的集成和使用
一.和Eclipse的集成 1.添加Maven 在windows--> preferences中找到maven选项,如下图: 如果没有上图的Name为apache-maven-3.5.2的项,则 ...
- 如何用ABP框架快速完成项目(5) - 用ABP一个人快速完成项目(1) - 使用代码生成器
用ABP一个人快速完成项目有如下要点: 站在巨人的肩膀上 - 使用代码生成器 站在巨人的肩膀上 - 使用成熟控件框架, 一个框架不够就上两个, 两个不够就上三个 通过微服务模式而不是盖楼式来避免难度升 ...
- Java 创建线程/停止线程
继承 Thread 类 class MyThread1 extends Thread{ @Override public void run(){ System.out.println("继承 ...
- IntelliJ IDEA 编译Java程序出现 'Error:java: 无效的源发行版: 9' 的解决方案
最新安装的IntelliJ IDEA 2018.1编译器,创建Java Project,并选择之前安装好的Eclipse配置的JDK,如图所示: 在工程中添加 Main.class, main函数中写 ...
- JMeter 报告监听器导入.jtl结果文件报错解决方案
JMeter 报告监听器导入.jtl结果文件报错解决方案 by:授客 QQ:1033553122 1. 问题描述 把jmeter压测时生成的 .jtl结果文件导入监听器报告中,弹出如下错误提示 ...
- Spark操作HBase报:org.apache.hadoop.hbase.client.RetriesExhaustedWithDetailsException异常解决方案
一.异常信息 19/03/21 15:01:52 WARN scheduler.TaskSetManager: Lost task 4.0 in stage 21.0 (TID 14640, hnte ...
- SQL Agent Job 报“Access to the remote server is denied because the current security context is not trusted”
SQL Server 2005(Microsoft SQL Server 2005 - 9.00.5000.00)下的一个作业执行一个存储过程,存储过程中动态SQL语句使用链接服务器(Linked S ...