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

ElasticSearch-TransportClient客户端 *** 作Es(五),第1张

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());
}
各种普通查询



高亮查询

过滤查询

一次完整的搜索

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5636023.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存