栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

ElasticSearch-TransportClient客户端操作Es(五)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ElasticSearch-TransportClient客户端操作Es(五)

引入maven依赖

  org.elasticsearch
  elasticsearch
  6.8.0


  org.elasticsearch.client
  transport
  6.8.0


  org.elasticsearch.plugin
  transport-netty4-client
  6.8.0

java操作es 创建索引和类型
  1. 创建客户端操作对象
//创建ES客户端操作对象
@Test
public void init() throws UnknownHostException {
  PreBuiltTransportClient preBuiltTransportClient = new PreBuiltTransportClient(Settings.EMPTY);
  preBuiltTransportClient.addTransportAddress(new TransportAddress(
    InetAddress.getByName("192.168.202.200"),9300));
}
  1. 创建索引
//创建索引
@Test
public void createIndex() throws UnknownHostException, ExecutionException, InterruptedException {
  PreBuiltTransportClient preBuiltTransportClient = new PreBuiltTransportClient(Settings.EMPTY);
  preBuiltTransportClient.addTransportAddress(new TransportAddress(
    InetAddress.getByName("192.168.202.200"),9300));
  //定义索引请求
  CreateIndexRequest ems = new CreateIndexRequest("ems");
  //执行索引创建
  CreateIndexResponse createIndexResponse = preBuiltTransportClient.admin().indices().create(ems).get();
  System.out.println(createIndexResponse.isAcknowledged());
}
  1. 删除索引
//删除索引
@Test
public void deleteIndex() throws UnknownHostException, ExecutionException, InterruptedException {
  PreBuiltTransportClient preBuiltTransportClient = new PreBuiltTransportClient(Settings.EMPTY);
  preBuiltTransportClient.addTransportAddress(new TransportAddress(
    InetAddress.getByName("192.168.202.200"),9300));
  //定义索引请求
  DeleteIndexRequest ems = new DeleteIndexRequest("ems");
  //执行索引删除
  AcknowledgedResponse acknowledgedResponse = preBuiltTransportClient.admin().indices().delete(ems).get();
  System.out.println(acknowledgedResponse.isAcknowledged());
}
  1. 创建索引和类型
//创建索引类型和映射
@Test
public void init() throws UnknownHostException, ExecutionException, InterruptedException {
  PreBuiltTransportClient preBuiltTransportClient = new PreBuiltTransportClient(Settings.EMPTY);
  preBuiltTransportClient.addTransportAddress(new TransportAddress(
    InetAddress.getByName("192.168.202.200"),9300));
  //创建索引
  CreateIndexRequest ems = new CreateIndexRequest("ems");
  //定义json格式映射
  String json = "{"properties":{"name":{"type":"text","analyzer":"ik_max_word"},"age":{"type":"integer"},"sex":{"type":"keyword"},"content":{"type":"text","analyzer":"ik_max_word"}}}";
  //设置类型和mapping
  ems.mapping("emp",json, XContentType.JSON);
  //执行创建
  CreateIndexResponse createIndexResponse = preBuiltTransportClient.admin().indices().create(ems).get();
  System.out.println(createIndexResponse.isAcknowledged());
}
索引一条记录
  1. 指定id索引记录
//索引一条文档 指定id
@Test
public void createIndexOptionId() throws JsonProcessingException {
  Emp emp = new Emp("小陈", 23, "男", "这是一个单纯的少年,单纯的我!");
  String s = JSONObject.toJSONString(emp);
  IndexResponse indexResponse = transportClient.prepareIndex("ems", "emp", "1").setSource(s, XContentType.JSON).get();
  System.out.println(indexResponse.status());
}
各种普通查询



高亮查询

过滤查询

一次完整的搜索

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/632496.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号