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的更多相关文章

  1. java操作elasticsearch实现基本的增删改查操作

    一.在进行java操作elasticsearch之前,请确认好集群的名称及对应的ES节点ip和端口 1.查看ES的集群名称 #进入elasticsearch.yml配置文件/opt/elasticse ...

  2. Java操作ElasticSearch之创建客户端连接

    Java操作ElasticSearch之创建客户端连接 3 发布时间:『 2017-09-11 17:02』  博客类别:elasticsearch  阅读(3157) Java操作ElasticSe ...

  3. java操作elasticsearch实现组合桶聚合

    1.terms分组查询 //分组聚合 @Test public void test40() throws UnknownHostException{ //1.指定es集群 cluster.name 是 ...

  4. java操作elasticsearch实现query String

    1.CommonTersQuery: 指定字段进行模糊查询 //commonTermsQuery @Test public void test35() throws UnknownHostExcept ...

  5. java操作elasticsearch实现聚合查询

    1.max 最大值 //max 求最大值 @Test public void test30() throws UnknownHostException{ //1.指定es集群 cluster.name ...

  6. java操作elasticsearch实现前缀查询、wildcard、fuzzy模糊查询、ids查询

    1.前缀查询(prefix) //prefix前缀查询 @Test public void test15() throws UnknownHostException { //1.指定es集群 clus ...

  7. java操作elasticsearch实现条件查询(match、multiMatch、term、terms、reange)

    1.条件match query查询 //条件查询match query @Test public void test10() throws UnknownHostException { //1.指定e ...

  8. java操作elasticsearch实现查询删除和查询所有

    后期博客本人都只给出代码,具体的说明在代码中也有注释. 1.查询删除 //查询删除:将查询到的数据进行删除 @Test public void test8() throws UnknownHostEx ...

  9. java操作elasticsearch实现批量添加数据(bulk)

    java操作elasticsearch实现批量添加主要使用了bulk 代码如下: //bulk批量操作(批量添加) @Test public void test7() throws IOExcepti ...

  10. elasticsearch基本操作之--使用java操作elasticsearch

    /** * 系统环境: vm12 下的centos 7.2 * 当前安装版本: elasticsearch-2.4.0.tar.gz */ es 查询共有4种查询类型 QUERY_AND_FETCH: ...

随机推荐

  1. vs 调试 iis中的网站

    打开网站,在vs中附加进程,选择w3wp.exe,如果不能下断点,设置一下pdb文件位置

  2. 28.QT-QPainter介绍

    介绍 可以在QPaintDevice类上绘制各种图形 QPaintDevice类表示QPainter的绘图设备(画布) QpaintDevice子类有QImage.QOpenGLPaintDevice ...

  3. 【Java每日一题】20170117

    20170116问题解析请点击今日问题下方的“[Java每日一题]20170117”查看(问题解析在公众号首发,公众号ID:weknow619) package Jan2017; import jav ...

  4. mysql无法远程连接

    在mysql的mysql数据库下: select user,host from user;(查看,没有本机的访问权限) grant all privileges on *.* to root@&quo ...

  5. 判断文本是否溢出/hover显示全部

    前言 在工作中我们经常会遇到,文字过多,需要用省略号,并且鼠标hover的时候 还需要 显示全部的文字的需求. 正文 文字过多需要用省略号的实现:上代码啦 .ellipsis { width: 100 ...

  6. wepy框架须知

    安装脚手架 . 安装构件工具 npm i wepy-cli -g .创建项目 wepy new 项目名 .实时编译 wepy build --watch 或 npm run dev .使用微信开发者工 ...

  7. layui 自定义表单验证的几个实例

    *注:使用本方法请先引入layui依赖的layu.js和layui.css 1.html <input type="text" name="costbudget&q ...

  8. 【代码笔记】Web-Javascript-Javascript函数

    一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  9. IDEA项目搭建十四——Web站点Controller基类及布局页静态资源设计

    一.简介 站点搭建完成后,编写页面时一般会有如下几个需求 1.嵌套静态页面时有很大一部分通用代码,如css.js这部分可以使用thymeleaf的局部片段代码块组成 2.这些静态资源默认放在程序中,但 ...

  10. 开发Spring过程中几个常见异常(三):java.lang.ClassCastException: com.sun.proxy.$Proxy4 cannot be cast to com.edu.aop.ArithmeticCalculatorImpl at com.edu.aop.Main.main(Main.java:11)

    这个异常是在开发Spring案例时遇到的. 贴一下完整异常信息: Exception in thread "main" java.lang.ClassCastException: ...