GET _search
{"query": {"match_all": {}}
}
#创建索引
PUT /zxy
{"mappings":{"properties":{"info":{"type":"text","analyzer":"ik_smart"},"email":{"type":"keyword","index":false},"name":{"type":"object","properties":{"firstName":{"type":"keyword"},"lastName":{"type":"keyword"}}}}}
}
#新添索引字段
PUT /zxy/_mapping
{"properties":{"age":{"type":"integer"}}
}GET /zxy
#插入文档
POST /zxy/_doc/1
{"age":"21","email":"2429203557@qq.com","info":"哈哈有几个哈","name":{"firstName":"赵","lastName":"兴宇"}
}
#查询文档
GET /zxy/_doc/1#删除文档
DELETE /zxy/_doc/1# 一.全量修改 全部修改   id存在就是修改,不存在就是新增
PUT /zxy/_doc/1
{"age":"21","email":"zhaoxingyu@qq.com","info":"哈哈有几个哈","name":{"firstName":"赵","lastName":"兴宇"}
}#局部修改文档字段
POST /zxy/_update/1
{"doc":{"info":"纱雾天下第一!"}
}#酒店的mapping
PUT /hotel
{"mappings": {"properties": {"id":{"type": "keyword"},"name":{"type": "text","analyzer": "ik_max_word","copy_to": "all"},"address":{"type": "keyword","index": false},"price":{"type": "integer"},"score":{"type": "integer"},"brand":{"type": "keyword","copy_to": "all"},"city":{"type": "keyword"},"starName":{"type": "keyword"},"business":{"type": "keyword","copy_to": "all"},"location":{"type": "geo_point"},"pic":{"type": "keyword","index": false},"all":{"type": "text","analyzer": "ik_max_word"}}}
}GET /hotelDELETE /hotelGET /hotel/_doc/47478#批量查询
GET /hotel/_search#查询所有
GET /hotel/_search
{"query": {"match_all": {}}
}#全文检索查询
GET /hotel/_search
{"query": {"match": {"all": "北京"}}
}#允许同时查询多个字段
GET /hotel/_search
{"query": {"multi_match": {"query": "五星级","fields": ["city","brand","starName"]}}
}#精确查询,查找keyword 不可分词
# term对值查询(完全一致才行)
GET /hotel/_search
{"query": {"term": {"city": {"value": "北京"}}}
}#精确查询 ,对范围查询
# gte:#大于等于  lte :#小于等于  没有e就是没有等于
GET /hotel/_search
{"query": {"range": {"price": {"gte": 200, "lte": 300    }}}
}#地理查询
# geo_bounding_box: 查询geo_point值落在某个矩形范围的所有文档
GET /hotel/_search
{"query": {"geo_bounding_box":{"location":{"top_left":{"lat":31.1,"lon":121.5},"bottom_right":{"lat":30.9,"lon":121.7}}}}
}#geo_distance:查询到指定中心点小于某个距离值的所有文档GET /hotel/_search
{"query": {"geo_distance":{"distance":"5km","location":"31.21, 121.5"}}
}#复合查询 将其他简单查询组合起来 实现更复杂的搜索逻辑
#function score :算分函数查询,算分控制文档排名#高亮
GET /hotel/_search
{"query": {"match": {"all": "如家"}},"highlight": {"fields": {"name":{"require_field_match": "false"}}}
}/*** @author zxy* @Classname HotelIndexTest* 对索引的增删改查* @Date 2022/1/19 9:41*/public class HotelIndexTest {private RestHighLevelClient client;@Testvoid testInit() {System.out.println(client);}/*** 创建索引库*/@Testvoid createHotelIndex() throws IOException {// 1.创建Request对象CreateIndexRequest request = new CreateIndexRequest("hotel");// 2.准备请求的参数: DSL语句request.source(MAPPING_TEMPLATE, XContentType.JSON);// 3.发起请求client.indices().create(request, RequestOptions.DEFAULT);}/*** 判断索引是否存在** @throws IOException*/@Testvoid existsHotelIndex() throws IOException {GetIndexRequest request = new GetIndexRequest("hotel");boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);System.err.println(exists ? "索引库已经存在" : "索引库不存在");}/*** 获取查看索引*/@Testvoid getHotelIndex() throws IOException {GetIndexRequest request = new GetIndexRequest("hotel");GetIndexResponse hotel = client.indices().get(request, RequestOptions.DEFAULT);System.out.println(hotel.getMappings());}/*** 删索引库*/@Testvoid deleteHotelIndex() throws IOException {DeleteIndexRequest request = new DeleteIndexRequest("hotel");client.indices().delete(request, RequestOptions.DEFAULT);}//连接  ,最前@BeforeEachvoid setUp() {this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://localhost:9200")//可多个 集群配置
//                HttpHost.create("http://localhost:9200")));}//结束释放资源,最后@AfterEachvoid tearDown() throws IOException {this.client.close();}
}/*** @author zxy* @Classname HotelDocumentTest* 对文档的增删改* @Date 2022/1/19 10:48*/
@SpringBootTest
public class HotelDocumentTest {private RestHighLevelClient client;@Autowiredprivate IHotelService hotelService;@Testvoid testInit() {System.out.println(client);}/*** 新增文档*/@Testvoid testIndexDocument() throws IOException {//根据id查询酒店数据Hotel hotel = hotelService.getById(38609L);// 因为文档的location只有一个字段,把经纬度合在一起,所以将其转换HotelDoc hotelDoc = new HotelDoc(hotel);// 1.准备Request对象IndexRequest request = new IndexRequest("hotel").id(hotel.getId().toString());// 2.准备JSON文档request.source(JSON.toJSONString(hotelDoc), XContentType.JSON);// 3. 发送请求client.index(request, RequestOptions.DEFAULT);}/*** 根据id查询文档 ,根据id查询到的文档数据是json,需要反序列化成java对象*/@Testvoid testGetDocumentById() throws IOException {GetRequest request = new GetRequest("hotel", "38609");GetResponse response = client.get(request, RequestOptions.DEFAULT);String json = response.getSourceAsString();HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);System.out.println(hotelDoc);System.out.println(json);}/*** 根据id删除文档*/@Testvoid testDeleteDocumentById() throws IOException {DeleteRequest request = new DeleteRequest("hotel", "38609");client.delete(request, RequestOptions.DEFAULT);}/*** 根据id判断文档是否存在*/@Testvoid testExistsDocumentById() throws IOException {GetRequest request = new GetRequest("hotel", "38609");boolean exists = client.exists(request, RequestOptions.DEFAULT);System.out.println(exists ? "文档存在" : "文档不存在");}/*** 根据id修改文档*/@Testvoid testUpdateDocumentById() throws IOException {UpdateRequest request = new UpdateRequest("hotel", "38609");// 2.准备参数,每两个参数为一对 key value;request.doc("city","贵州","name","赵兴宇家酒店");client.update(request,RequestOptions.DEFAULT);}/*** 批量导入文档  (批量处理文档)*/@Testvoid testBulkRequest() throws IOException {List<Hotel> hotels = hotelService.list();// 1.创建requestBulkRequest request = new BulkRequest();// 2.准备参数,添加多个新增的requestfor (Hotel hotel : hotels) {HotelDoc hotelDoc = new HotelDoc(hotel);request.add(new IndexRequest("hotel").id(hotel.getId().toString()).source(JSON.toJSONString(hotelDoc),XContentType.JSON));// 可以批量新增,删除,修改
//            request.add(new UpdateRequest());}// 3.发送请求client.bulk(request,RequestOptions.DEFAULT);}/*** 批量查询*/@Testvoid testSearchDocument() throws IOException {SearchRequest request = new SearchRequest("hotel");SearchResponse response = client.search(request, RequestOptions.DEFAULT);System.out.println(response);}//连接  ,最前@BeforeEachvoid setUp() {this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://localhost:9200")//可多个 集群配置
//                HttpHost.create("http://localhost:9200")));}//结束释放资源,最后@AfterEachvoid tearDown() throws IOException {this.client.close();}}/**
* DSL语法的查询
*/public class RestClientDocumentTest {private RestHighLevelClient client;/*** 查找所有文档*/@Testvoid testMatchAll() throws IOException {//1.准备requestSearchRequest request = new SearchRequest("hotel");// 2.准备DSL参数request.source().query(QueryBuilders.matchAllQuery());// 3.发送请求,得到相应结果SearchResponse response = client.search(request, RequestOptions.DEFAULT);// 4.解析响应结果SearchHits searchHits = response.getHits();//4.1查询的总条数handleResponse(searchHits);}/*** 多种条件查询*/@Testvoid testMatch() throws IOException {//1.准备requestSearchRequest request = new SearchRequest("hotel");// 2.准备DSL参数//(1).全文检索查询request.source().query(QueryBuilders.matchQuery("all", "如家"));//(2).多字段查询request.source().query(QueryBuilders.multiMatchQuery("如家", "name", "business"));//(3).termQuery精确查询(词条查询)request.source().query(QueryBuilders.termQuery("city", "上海"));//(4).范围查询request.source().query(QueryBuilders.rangeQuery("price").gte(100).lte(150));//(5).复合查询//(5).1创建布尔查询BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();//(5).2添加term查询boolQuery.must(QueryBuilders.termQuery("city", "杭州"));//(5).3谭家rangeboolQuery.filter(QueryBuilders.rangeQuery("price").lte(250));//将条件添加进请求request.source().query(boolQuery);// 3.发送请求,得到相应结果SearchResponse response = client.search(request, RequestOptions.DEFAULT);// 4.解析响应结果SearchHits searchHits = response.getHits();//4.1查询的总条数handleResponse(searchHits);}/*** 查询排序,分页以及高亮*/@Testvoid testSource() throws IOException {//1.准备requestSearchRequest request = new SearchRequest("hotel");// 2.准备DSL参数request.source().query(QueryBuilders.matchAllQuery());//分页request.source().from(0).size(5);//价格高亮显示request.source().highlighter(new HighlightBuilder().field("name")//是否需要与匹配字段对应.requireFieldMatch(false));//价格排序request.source().sort("price", SortOrder.ASC);// 3.发送请求,得到相应结果SearchResponse response = client.search(request, RequestOptions.DEFAULT);// 4.解析响应结果SearchHits searchHits = response.getHits();//抽离函数handleResponse(searchHits);}/*** 抽取出的方法,解析响应结果** @param searchHits*/private void handleResponse(SearchHits searchHits) {long total = searchHits.getTotalHits().value;System.out.println("一共有:" + total + "条数据");//4.2查询的结果数组SearchHit[] hits = searchHits.getHits();for (SearchHit hit : hits) {//4.3得到sourceString json = hit.getSourceAsString();HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);//获取高亮字段Map<String, HighlightField> highlightFields = hit.getHighlightFields();if (!CollectionUtils.isEmpty(highlightFields)) {HighlightField highlightField = highlightFields.get("name");if (highlightField != null) {String name = highlightField.getFragments()[0].string();hotelDoc.setName(name);}}//4.4打印System.out.println(hotelDoc);}}@Testvoid name() {}//    @Test
//    void name() {
//    }
//
//    @Test
//    void name() {
//    }//连接  ,最前@BeforeEachvoid setUp() {this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://localhost:9200")//可多个 集群配置
//                HttpHost.create("http://localhost:9200")));}//结束释放资源,最后@AfterEachvoid tearDown() throws IOException {this.client.close();}
}

springcloud-es的学习相关推荐

  1. es入门学习7-java-es自身API

    es入门学习7-java-es自身API 使用起前面的集群环境进行学习 一. 公共的东西 1.1.项目位置 1.2.导入依赖 <dependency><groupId>org. ...

  2. 肝了很久,冰河整理出这份4万字的SpringCloud与SpringCloudAlibaba学习笔记!!

    写在前面 不少小伙伴让我整理下有关SpringCloud和SpringCloudAlibaba的知识点,经过3天的收集和整理,冰河整理出这份4万字的SpringCloud与SpringCloudAli ...

  3. 尚硅谷 SpringCloud 第二季学习笔记【已完结】

    SpringCloud 一.介绍 (一)cloud和boot之间的依赖关系 https://spring.io/projects/spring-cloud#overview Finchley 是基于 ...

  4. 尚硅谷周阳老师2020最新Springcloud完整版学习

    最新SpringCloud 目录 最新SpringCloud 一.简介 版本号的对应 总架构: 二.入门体验 父工程的创建: cloud-provider-payment-8001 开启热部署: cl ...

  5. 为什么springcloud值得我们学习?

    springcloud好不好?好! 它的微服务理念适合所有公司的业务吗?不一定! 但是不使用它需要学习它吗?需要! 虽然去年就听了朋友说了springcloud全家桶如何如何方便?但是一看到全家桶中E ...

  6. ES入门学习:ElasticSearch、Kibana、ik分词器的安装、简单使用及SpringBoot集成

    前言 es是采用Java语言开发,因此,想要安装运行es需要提前准备好jdk环境,关于linux配置jdk在前文linux配置jdk 本文主要介绍es的安装.kibana的安装和简单使用及ik分词器的 ...

  7. es检索学习笔记第一篇

    文章目录 概念 倒排索引 分词器ik 创建mapping 查看索引,修改,删除 添加文档,查看文档,修改文档,查看文档 RestClient 一.映射分析 二.使用client创建索引等 创建Rest ...

  8. 尚硅谷周阳老师 SpringCloud第二季学习笔记

    前言:首先感谢尚硅谷周阳老师的讲解,让我对springcloud有了很好的理解,周阳老师的讲课风格真的很喜欢,内容充实也很幽默,随口一说就是一个段子,我也算是周阳老师的忠实粉丝啦. 先说说课程总体内容 ...

  9. SpringCloud源码学习笔记之Eureka客户端——DiscoveryClient接口的层级结构

    1.DiscoveryClient接口和类   在SpringCloud框架中,有一个DiscoveryClient接口和一个同名的DiscoveryClient类,其中:DiscoveryClien ...

  10. springcloud微服务学习笔记

    此篇内容较长. 目录 一.关于微服务 二.微服务工程的构建 1.新建maven父工程 2.新建公共模块 3.新建微服务提供者8001 4.新建微服务消费者80 三.服务和发现注册中心(Eureka) ...

最新文章

  1. PHP Session有效期的相关问题
  2. 【Boost】boost库中thread多线程详解10——condition条件变量
  3. python中文乱码 def decode-python处理一些乱码的中文文本时decode('utf-8')报错的处理...
  4. JZYZOJ1140 飞船控制站
  5. php中time()与$_SERVER[REQUEST_TIME]用法区别
  6. 符号“”和const在函数里不同位置的用法
  7. linux中nginx的nginx.config文件的配置和启动(包括重启)
  8. android imageview 获取bitmap缩放大小,android – Imageview缩放方法“centercrop”作为代码...
  9. android SoundPool例子,Android SoundPool即时音效的使用Demo
  10. html img动态设置图片大小,Js动态设置Img大小
  11. 网易云音乐api歌单数据获取
  12. linux系统显示无法挂载,linux中的mount系统调用无法通过df命令显示文件系统的挂载点...
  13. duplicate designator is not allowedC/C++(2906)
  14. 关于Android开发中如何使用dp表示长宽
  15. 电脑重装Win10如何选择32位和64位的系统
  16. 天境生物启动A股上市辅导:已实现盈利,臧敬五不再是主要股东?
  17. html mailto 不起作用,HTML Mailto 使用手记
  18. 谷歌浏览器扩展工具---eye dropper取色器使用
  19. AZ-104认证考试攻略
  20. 中星9号卫星PK中星6B+鑫诺3号组合

热门文章

  1. 常见的7种软件规模估算方法 优劣势比较
  2. 基于声道重混缩算法的WAV音频消除人声方法的初探01
  3. (TCP-over-UDP library)基于UDP协议之上实现通用、可靠、高效的TCP协议
  4. 如何用APPS赚钱?
  5. 【安卓学习之开发工具】 Android 学习-- 下载过的一些项目
  6. jpg、png格式的图片转换为eps格式
  7. 目标检测项目中面对高分辨率图像的滑动窗口技术(二)(代码开源,超简便API封装,直接调用进行切图及保存)
  8. jQuery弹出深色系层菜单
  9. crmeb开源版二开好方便
  10. Linux系统如何进行完整的CPU性能跑分测试