摘要:一依赖刚开始少这个包创建索引失败官方文档并没有给这个提示二开始之前的准备官方文档连接操作所用到的实体类三关于索引的操作官方文档新增索引索引名称分片副本内容查询指定索引索引名称删除索引四关于文档的操作官方文档创建文档索引名称前
一、Maven依赖
二、开始之前的准备org.elasticsearch elasticsearch 7.1.0 org.elasticsearch.client elasticsearch-rest-high-level-client 7.1.0
官方文档
/** * 连接ES * @return */ public RestHighLevelClient start() { RestHighLevelClient restHighLevelClient = new RestHighLevelClient( RestClient.builder( new HttpHost("192.168.100.151", 9201, "http"), new HttpHost("192.168.100.151", 9202, "http"), new HttpHost("192.168.100.151", 9203, "http"))); return restHighLevelClient; } /** * 操作所用到的实体类 */ @Data class Article{ private long id; private String title; public Article(long id, String title) { this.id = id; this.title = title; } }三、关于索引的操作
官方文档
新增索引
public void createIndex(RestHighLevelClient client) { //索引名称 CreateIndexRequest request = new CreateIndexRequest("hello"); //分片副本 request.settings(Settings.builder().put("index.number_of_shards", 5) .put("index.number_of_replicas", 1)); //内容 Mapid = new HashMap <>(); id.put("type","text"); id.put("store",true); Map title = new HashMap <>(); title.put("type","text"); title.put("store",true); title.put("index",true); title.put("analyzer", "standard"); Map properties = new HashMap <>(); properties.put("id",id); properties.put("title",title); Map mapping = new HashMap <>(); mapping.put("properties",properties); request.mapping(mapping); try { CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT); System.out.println(response.toString()); } catch (IOException e) { e.printStackTrace(); } }
查询指定索引
public void getIndex(RestHighLevelClient client) throws IOException { //索引名称 GetIndexRequest request = new GetIndexRequest("hello"); try { boolean exists = client.indices().exists(request, RequestOptions.DEFAULT); System.out.println(exists); } catch (IOException e) { e.printStackTrace(); } }
删除索引
public void delIndex(RestHighLevelClient client){ DeleteIndexRequest request = new DeleteIndexRequest("hello"); try { AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT); System.out.println(delete.toString()); } catch (IOException e) { e.printStackTrace(); } }四、关于文档的操作
官方文档
创建文档
public void createDocument(RestHighLevelClient client){ //索引名称 IndexRequest indexRequest = new IndexRequest("hello"); ObjectMapper mapper = new ObjectMapper(); Article article = new Article(3L, "web前端"); byte[] json = new byte[0]; try { json = mapper.writeValueAsBytes(article); //可以设置文章ID indexRequest.id("5"); indexRequest.source(json, XContentType.JSON); IndexResponse index = client.index(indexRequest, RequestOptions.DEFAULT); } catch (Exception e) { e.printStackTrace(); } }
根据文档ID查询文档
public void getDocument(RestHighLevelClient client){ GetRequest getRequest = new GetRequest("hello", "1"); GetResponse documentFields = null; try { documentFields = client.get(getRequest, RequestOptions.DEFAULT); System.out.println(documentFields.toString()); } catch (IOException e) { e.printStackTrace(); } }
更新文档
public void updateDocument(RestHighLevelClient client){ UpdateRequest updateRequest = new UpdateRequest("hello", "1"); Article article = new Article(2L, "java入门到放弃"); ObjectMapper mapper = new ObjectMapper(); byte[] json = new byte[0]; try { json = mapper.writeValueAsBytes(article); IndexRequest indexRequest = new IndexRequest("hello"); indexRequest.source(json, XContentType.JSON); updateRequest.doc(indexRequest); UpdateResponse updateResponse = client.update( updateRequest, RequestOptions.DEFAULT); System.out.println(updateResponse); } catch (Exception e) { e.printStackTrace(); } }
删除文档
public void delDocument(RestHighLevelClient client){ DeleteRequest request = new DeleteRequest("hello", "1"); try { DeleteResponse deleteResponse = client.delete( request, RequestOptions.DEFAULT); System.out.println(deleteResponse); } catch (IOException e) { e.printStackTrace(); } }
查询文档
public void searchDocument(RestHighLevelClient client){ SearchRequest searchRequest = new SearchRequest(); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(QueryBuilders.matchAllQuery()); searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = null; try { searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); System.out.println(searchResponse.toString()); } catch (IOException e) { e.printStackTrace(); } }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/75357.html
摘要:时间年月日星期四说明本文部分内容均来自慕课网。那么里面的数据就可以分为各种各样的索引,比如汽车索引图书索引家具索引等等。图书索引又可以细分为各种类型,比如科普类小说类技术类等等。具体到每一本书籍,就是文档,就是整个图书里面最小的存储单位。 时间:2017年09月14日星期四说明:本文部分内容均来自慕课网。@慕课网:http://www.imooc.com教学源码:无学习源码:https...
摘要:当时自己在本地测试搭建集群后,给分配了另外一个任务就是去了解中的自带分词英文分词中文分词的相同与差异以及自己建立分词需要注意的点。还有就是官网的文档了,非常非常详细,还有,版本的是有中文的官方文档,可以凑合着看。 前提 人工智能、大数据快速发展的今天,对于 TB 甚至 PB 级大数据的快速检索已然成为刚需,大型企业早已淹没在系统生成的浩瀚数据流当中。大数据技术业已集中在如何存储和处理这...
阅读 3012·2021-11-25 09:43
阅读 1591·2021-11-24 11:15
阅读 2330·2021-11-22 15:25
阅读 3481·2021-11-11 16:55
阅读 3206·2021-11-04 16:10
阅读 2752·2021-09-14 18:02
阅读 1668·2021-09-10 10:50
阅读 1051·2019-08-29 15:39