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: ...
随机推荐
- matlab rank
k =rank(A) %a is matrix s = svd(A); tol = max(size(A))*eps(max(s)); r = sum(s > tol);
- 数据库索引(Oracle和Mysql)学习总结
旭日Follow_24 的CSDN 博客 ,全文地址请点击: https://mp.csdn.net/postedit/80910082 索引概念: 索引是关系数据库中用于存放每一条记录的一种 ...
- hash 和pushState,replaceState
hash 要点: 1.不会向后台发请求:#是用来指导浏览器动作的,对服务器端完全无用. 2.用来跳转到页面的指定位置: 为网页位置指定标识符,有两个方法.一是使用锚点,比如<a name=& ...
- ASP.NET MVC 使用Jquery异步操作JS代码
$(function () { var ajaxFormSubmit = function () { var $form = $(this); var options = { url: $form.a ...
- SqlMapConfig配置加注解
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC & ...
- 电脑GIF动图制作方法图文详解
我们在电脑上可以看到很多动态图,有趣的.搞笑的.可爱的等等,只要我们要用哪种类型的,网上应有尽有,但是想不想自己制作图片呢?今天我们就来学习一下GIF动图制作的方法. 使用工具: 电脑 操作方法: 1 ...
- 用ABP只要加人即可马上加快项目进展(二) - 分工篇
2018年和1998年其中两大区别就是: 前端蓬勃发展, 前后端分离是一个十分大的趋势. 专门的测试人员角色被取消, 多出了一个很重要的角色, 产品经理 ABP只要加入即可马上加快项目进展, 选择 ...
- MySQL 授予普通用户PROCESS权限
在MySQL中如何给普通用户授予查看所有用户线程/连接的权限,当然,默认情况下show processlist是可以查看当前用户的线程/连接的. mysql> grant process on ...
- C#核心基础--类的继承
继承 一个类可以继承自另一个类.在 C#中,类与类之间只存在单一继承.也就是说,一个类的直接基类只能有一个.当类与类之间实现继承的时候,子类可以将它的直接基类的所有成员当做自己的成员,除了类的静态构造 ...
- Windows Server 2016-Active Directory域服务端口汇总
本章为大家简单整理一下有关Windows server Active Directory和Active Directory域服务(AD DS)组件的端口要求.生产环境中我们在做网络调整.防火墙或者开关 ...