ElasticSearch Java API 增删改查操作
2021/6/2 20:22:41
本文主要是介绍ElasticSearch Java API 增删改查操作,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1.添加pom.xml依赖
API基本操作 2.连接到elasticsearch集群
private static TransportClient client; static {// 1、获取客户端对象,设置连接的集群名称 Settings settings= Settings.builder().put("cluster.name","elasticsearch").build(); client=new PreBuiltTransportClient(settings); // 2、连接集群 try { client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("hadoop-001"), 9300)); System.out.println(client.toString()); } catch (UnknownHostException e) { e.printStackTrace(); }}
3.创建索引
public static void creatIndex(){
//1.创建索引(indices指数) client.admin().indices().prepareCreate("blog").get(); //2.关闭连接 client.close(); }
4.删除索引
public static void deleteIndex(){ //1.创建索引(indices指数) client.admin().indices().prepareDelete("blog").get(); //2.关闭连接 client.close(); }
5 新建文档(源数据是手写的 json 串)
public static void creatIndexByJason(){ // 1、文档数据准备 String json = "{" + "\"id\":\"1\"," + "\"title\":\"基于Lucene的搜索服务器\"," + "\"content\":\"它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口\"" + "}"; String jason2="{"+ "\"id\":\"1\","+"\"title\":\"基于Lucene的搜索服务器\"," +"\"content\":\"它提供了一个分布式多用户的全文搜索引擎,基于RESTful web接口\""+"}"; //2.创建文档 IndexResponse indexResponse=client.prepareIndex("blog","article","1").setSource(json).execute().actionGet(); // 3、打印返回的结果 System.out.println("index:" + indexResponse.getIndex()); System.out.println("type:" + indexResponse.getType()); System.out.println("id:" + indexResponse.getId()); System.out.println("version:" + indexResponse.getVersion()); System.out.println("result:" + indexResponse.getResult()); // 4、关闭连接 client.close(); }
6 新建文档(源数据是以 map 方式添加的键值对)
public static void creatIndexByMap(){ // 1、文档数据准备 Map json.put("id", "2"); json.put("title", "基于Lucene的搜索服务器"); json.put("content", "它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口"); IndexResponse indexResponse=client.prepareIndex("blog","article","2").setSource(json).execute().actionGet(); // 3、打印返回的结果 System.out.println("index:" + indexResponse.getIndex()); System.out.println("type:" + indexResponse.getType()); System.out.println("id:" + indexResponse.getId()); System.out.println("version:" + indexResponse.getVersion()); System.out.println("result:" + indexResponse.getResult()); // 4、关闭连接 client.close(); }
7 新建文档(源数据是通过 es 构建器构建的数据)
public static void creatIndexByBuilder() { //1.通过es自带的帮助类,来构建json数据 XContentBuilder builder= null; try { builder = XContentFactory.jsonBuilder().startObject() .field("id","3") .field("title","基于Lucene的搜索服务器") .field("content", "它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口") .endObject(); } catch (IOException e) { e.printStackTrace(); } IndexResponse indexResponse=client.prepareIndex("blog","article","3").setSource(builder).execute().actionGet(); // 3、打印返回的结果 System.out.println("index:" + indexResponse.getIndex()); System.out.println("type:" + indexResponse.getType()); System.out.println("id:" + indexResponse.getId()); System.out.println("version:" + indexResponse.getVersion()); System.out.println("result:" + indexResponse.getResult()); // 4、关闭连接 client.close(); }
8 搜索文档数据(单个索引)
public static void getData() { // 1、查询文档 GetResponse response = client.prepareGet("blog", "article", "1").get(); // 2、打印搜索的结果 System.out.println(response.getSourceAsString()); // 3、关闭连接 client.close(); }
9 搜索文档数据(多个索引)
public static void getMultiData() { // 1、查询多个文档 MultiGetResponse response = client.prepareMultiGet() .add("blog", "article", "1") .add("blog", "article", "2", "3") .add("blog", "article", "2").get(); // 2、遍历返回的结果 for (MultiGetItemResponse itemResponse : response) { GetResponse getResponse = itemResponse.getResponse(); // 如果获取到查询结果 if (getResponse.isExists()) { String sourceAsString = getResponse.getSourceAsString(); System.out.println(sourceAsString); } } // 3、关闭资源 client.close(); }
10 更新文档数据(update)
public static void updateData() { // 1、创建更新数据的请求对象 UpdateRequest updateRequest = new UpdateRequest(); updateRequest.index("blog"); updateRequest.type("article"); updateRequest.id("3"); try { updateRequest.doc(XContentFactory.jsonBuilder().startObject() .field("title", "基于Lucene的搜索服务器") // 对没有的字段进行添加,对已有的字段进行替换 .field("content", "它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。大数据前景无限") .field("createDate", "2017-8-22").endObject()); } catch (IOException e) { e.printStackTrace(); } // 2、获取更新后的值 UpdateResponse indexResponse = null; try { indexResponse = client.update(updateRequest).get(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } // 3、打印返回的结果 System.out.println("index:" + indexResponse.getIndex()); System.out.println("type:" + indexResponse.getType()); System.out.println("id:" + indexResponse.getId()); System.out.println("version:" + indexResponse.getVersion()); System.out.println("result:" + indexResponse.getResult()); // 4、关闭连接 client.close(); }
11 更新文档数据(upsert)
public static void upsertData() throws Exception { // 设置查询条件,查找不到则添加 IndexRequest 内容 IndexRequest indexRequest = new IndexRequest("blog", "article", "5") .source(XContentFactory.jsonBuilder().startObject() .field("title", "搜索服务器") .field("content","Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。").endObject()); // 设置更新,查找到则按照 UpdateRequest 更新 UpdateRequest upsert = new UpdateRequest("blog", "article", "5") .doc(XContentFactory.jsonBuilder().startObject().field("user", "李四").endObject()).upsert(indexRequest); client.update(upsert).get(); client.close(); }
12 删除文档数据(prepareDelete)
public static void deleteData() { // 1、删除文档数据 DeleteResponse indexResponse = client.prepareDelete("blog", "article", "2").get(); // 2、打印返回的结果 System.out.println("index:" + indexResponse.getIndex()); System.out.println("type:" + indexResponse.getType()); System.out.println("id:" + indexResponse.getId()); System.out.println("version:" + indexResponse.getVersion()); System.out.println("result:" + indexResponse.getResult()); // 3、关闭连接 client.close(); }
条件查询
1 查询所有(matchAllQuery)
public static void matchAllQuery(){
//1.执行查询(查询所有) SearchResponse searchResponse=client.prepareSearch("blog").setTypes("article") .setQuery(QueryBuilders.matchAllQuery()).get(); // 2、打印查询结果 SearchHits hits=searchResponse.getHits();//获取命中数,查询结果有多少对象 System.out.println("查询结果一共有"+hits.totalHits+"条"); for (SearchHit hit : hits) { System.out.println(hit.getSourceAsString()); } }
2 对所有字段分词查询(queryStringQuery)
public static void queryStringQuery() { // 1、条件查询(对所有字段分词查询) SearchResponse searchResponse = client.prepareSearch("blog").setTypes("article") .setQuery(QueryBuilders.queryStringQuery("全文")).get(); // 2、打印查询结果 SearchHits hits = searchResponse.getHits(); // 获取命中次数,查询结果有多少对象 System.out.println("查询结果有:" + hits.getTotalHits() + "条"); Iterator while (iterator.hasNext()) { SearchHit searchHit = iterator.next(); // 每个查询对象 System.out.println(searchHit.getSourceAsString()); // 获取字符串格式打印 } // 3、关闭连接 client.close(); }
3 通配符查询(wildcardQuery)
public static void wildcardQuery() { // 1、通配符查询 SearchResponse searchResponse = client.prepareSearch("blog").setTypes("article") .setQuery(QueryBuilders.wildcardQuery("content", "*全*")).get(); // 2、打印查询结果 SearchHits hits = searchResponse.getHits(); // 获取命中次数,查询结果有多少对象 System.out.println("查询结果有:" + hits.getTotalHits() + "条"); Iterator while (iterator.hasNext()) { SearchHit searchHit = iterator.next(); // 每个查询对象 System.out.println(searchHit.getSourceAsString()); // 获取字符串格式打印 } // 3、关闭连接 client.close(); }
4 词条查询(TermQuery)
public static void termQuery() { // 1、词条查询 SearchResponse searchResponse = client.prepareSearch("blog").setTypes("article") .setQuery(QueryBuilders.termQuery("content", "全")).get(); // 因为没有使用 IK 分词器,所有只能一个字一个字的查 // 2、打印查询结果 SearchHits hits = searchResponse.getHits(); // 获取命中次数,查询结果有多少对象 System.out.println("查询结果有:" + hits.getTotalHits() + "条"); Iterator while (iterator.hasNext()) { SearchHit searchHit = iterator.next(); // 每个查询对象 System.out.println(searchHit.getSourceAsString()); // 获取字符串格式打印 } // 3、关闭连接 client.close(); }
5 模糊查询(fuzzy)
public static void fuzzyQuery() { // 1、模糊查询 SearchResponse searchResponse = client.prepareSearch("blog").setTypes("article") .setQuery(QueryBuilders.fuzzyQuery("title", "lucene")).get(); // 2、打印查询结果 SearchHits hits = searchResponse.getHits(); // 获取命中次数,查询结果有多少对象 System.out.println("查询结果有:" + hits.getTotalHits() + "条"); Iterator while (iterator.hasNext()) { SearchHit searchHit = iterator.next(); // 每个查询对象 System.out.println(searchHit.getSourceAsString()); // 获取字符串格式打印 } // 3、关闭连接 client.close(); }
映射相关操作
public static void createMapping() throws Exception { // 1、创建索引(indices 指数) //client.admin().indices().prepareCreate("blog2").get(); // 1、设置 mapping XContentBuilder builder = XContentFactory.jsonBuilder() .startObject() .startObject("article2") .startObject("properties") .startObject("id2") .field("type", "text") .field("store", "true") .endObject() .startObject("title2") .field("type", "text") .field("store", "false") .endObject() .startObject("content2") .field("type", "text") .field("store", "true") .endObject() .endObject() .endObject() .endObject(); // 2、添加 mapping PutMappingRequest mapping = Requests.putMappingRequest("blog2").type("article2").source(builder); client.admin().indices().putMapping(mapping).get(); // 3、关闭资源 client.close(); }
这篇关于ElasticSearch Java API 增删改查操作的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-26Mybatis官方生成器资料详解与应用教程
- 2024-11-26Mybatis一级缓存资料详解与实战教程
- 2024-11-26Mybatis一级缓存资料详解:新手快速入门
- 2024-11-26SpringBoot3+JDK17搭建后端资料详尽教程
- 2024-11-26Springboot单体架构搭建资料:新手入门教程
- 2024-11-26Springboot单体架构搭建资料详解与实战教程
- 2024-11-26Springboot框架资料:新手入门教程
- 2024-11-26Springboot企业级开发资料入门教程
- 2024-11-26SpringBoot企业级开发资料详解与实战教程
- 2024-11-26Springboot微服务资料:新手入门全攻略