当前位置: 首页 > news >正文

山东做网站找谁彩虹云商城网站搭建

山东做网站找谁,彩虹云商城网站搭建,大丰哪家专业做网站,低成本门户网站开发目录 12.5 RestClient操作索引库 12.5.1创建库 12.5.2 删除索引库 12.5.3 判断是否存在 12.6 RestClient操作文档 12.6.1 新增文档 12.6.2 查询文档 12.6.3 修改文档 12.6.4 删除文档 12.6.5 批量导入文档 12.5 RestClient操作索引库 酒店mapping映射 ​PUT /hotel{&…

目录

12.5 RestClient操作索引库

12.5.1创建库

12.5.2 删除索引库

12.5.3 判断是否存在

12.6 RestClient操作文档

12.6.1 新增文档

12.6.2 查询文档

12.6.3 修改文档

12.6.4 删除文档

12.6.5 批量导入文档


12.5 RestClient操作索引库

酒店mapping映射

  ​PUT /hotel{"mappings": {"properties": {"id": {"type": "keyword","copy_to": "all"},"name": {"type": "text","analyzer": "ik_max_word"},"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"},"location": {"type": "geo_point"},"pic": {"type": "keyword","index": false},"all": {"type": "text","analyzer": "ik_max_word"}}}}

导入依赖

  <!--elasticSearch依赖--><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>7.12.1</version></dependency>

  <properties><java.version>1.8</java.version><elasticsearch.version>7.12.1</elasticsearch.version></properties>

创建对象

  @SpringBootTestclass HotelIndexTest {private RestHighLevelClient client;​// 客户端初始化@BeforeEachvoid setUp(){this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://192.168.142.129:9200")));}​@Testvoid tetsInit(){System.out.println(client);}​// 客户端销毁@AfterEachvoid tearDown() throws IOException {this.client.close();}}

client.indices()包含了操作索引库的所有方法

12.5.1创建库
  @Testvoid testCreateHotelIndex() throws IOException {// 1. 创建Request对象CreateIndexRequest request = new CreateIndexRequest("hotel");// 2. 准备请求的参数request.source(MAPPING_TEMPLATE,XContentType.JSON);// 3. 发送请求    client.indices()的返回值包含了索引库额所有操作client.indices().create(request,RequestOptions.DEFAULT);}

MAPPING_TEMPLATE是自定义的常量,也就是上面创建索引库的语句

12.5.2 删除索引库
  @Testvoid testDeleteIndex() throws IOException {// 1. 创建request对象DeleteIndexRequest request = new DeleteIndexRequest("hotel");// 2. 发送请求client.indices().delete(request,RequestOptions.DEFAULT);}
12.5.3 判断是否存在
  @Testvoid testExistIndex() throws IOException {// 1. 创建request对象GetIndexRequest request = new GetIndexRequest("hotel");// 2. 发送请求boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);// 3. 输出System.out.println(exists ? "索引库存在" : "索引库不存在");}

12.6 RestClient操作文档

12.6.1 新增文档
  @Testvoid testIndexDocument() throws IOException {// 在数据库查到数据Hotel hotel = iHotelService.getById(61083L);HotelDoc hotelDoc = new HotelDoc(hotel); // 经度 + 纬度 拼接之后的对象 ,即索引库需要的类型// 1. 创建请求对象IndexRequest request = new IndexRequest("hotel").id(hotel.getId().toString());// 2. 准备json文档 把查到的对象转换成json对象request.source(JSON.toJSONString(hotelDoc),XContentType.JSON);// 3. 发送请求client.index(request,RequestOptions.DEFAULT);}
12.6.2 查询文档
  @Testvoid testGetDocumentById() throws IOException {// 1. 准备requestGetRequest request = new GetRequest("hotel", "61083");// 2. 发送请求GetResponse response = client.get(request, RequestOptions.DEFAULT);// 3. 从响应中解析对象String json = response.getSourceAsString();// 4. 把json转成HotelDoc对象HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);System.out.println(hotelDoc);}
12.6.3 修改文档

第二种更新方式,即局部更新的代码 :

  @Testvoid testUpdateDocumentById() throws IOException {// 1. 准备RequestUpdateRequest request = new UpdateRequest("hotel", "61083");// 2. 准备请求参数request.doc("age", 18,"name","Rose");// 3. 发送请求client.update(request,RequestOptions.DEFAULT);}
12.6.4 删除文档
  @Testvoid testDeleteDocumentById() throws IOException {// 1. 准备requestDeleteRequest request = new DeleteRequest("hotel","61083");// 2. 发送请求client.delete(request,RequestOptions.DEFAULT);}
12.6.5 批量导入文档
  @Testvoid testBulk() throws IOException {// 批量查询酒店数据List<Hotel> hotels = iHotelService.list();// 1. 创建Bulk请求BulkRequest request = new BulkRequest();// 2. 准备参数  添加多个新增的requestfor (Hotel hotel : hotels) {// 把hotel转成hotelDoc对象HotelDoc hotelDoc = new HotelDoc(hotel);request.add(new IndexRequest("hotel").id(hotelDoc.getId().toString()).source(JSON.toJSONString(hotelDoc),XContentType.JSON));}​// 3. 发送请求client.bulk(request,RequestOptions.DEFAULT);}
http://www.ahscrl.com/news/12766.html

相关文章:

  • 怎么做免费的宣传网站青岛运营网络推广业务
  • flash网站开源资源
  • 多渠道分销系统安卓手机优化软件哪个好
  • 东莞清洁服务网站建设连云港seo公司
  • 公司级做宣传图的网站女儿考试没圈关键词
  • 怀柔石家庄网站建设广告搜索引擎
  • 建设网站门户世界企业排名500强
  • 移动网站 做优化短视频营销推广方式
  • 做二手车按揭的网站关键词长尾词优化
  • 做网店好还是网站西安最新消息今天
  • 移动平台3g手机网站前端开发布局技巧汇总秦皇岛seo优化
  • 温州市建设小学网站三只松鼠网络营销策略
  • 公司网站制作商电商营销推广方案
  • 布吉做网站5118大数据平台官网
  • 深圳网站建设大公司好我是做推广的怎么找客户
  • 昆明网站建设首选互维谷歌账号注册入口官网
  • 那有名网站是php做的营销外包
  • 如何看别人网站用什么做的合肥网站优化技术
  • 分销网站建站代写
  • 做360手机网站优化快长沙seo网站排名优化公司
  • 做外贸网站需要注意些什么手续官网seo优化
  • 做淘客网站用备案系统优化大师
  • 建行官网个人银行seo排名优化教学
  • 宁波网站优化方法电商培训机构需要什么资质
  • 网站商城微信支付接口百度百科合作模式
  • 做网站的公司叫什么问题岳阳网站界面设计
  • 做淘宝主要看哪些网站有哪些内容域名查询
  • 西安企业网站开发哪家好123网址之家
  • 宝塔建的数据库安装不了wordpressseo和sem哪个工资高
  • wordpress beginning南平seo