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

最好的网站建设价格百度app下载安装官方免费下载

最好的网站建设价格,百度app下载安装官方免费下载,有哪些专门做减肥内容的网站,动态网站的功能与特点目录 RestClient定义索引安装与配置测试类插入索引索引的 CRUD批量导入文档 RestClient 定义索引 引入对应 sql 后,需要添加 sql 对应的 es 索引 下面是根据 sql 结构来构建的索引树,我们需要插入到 es 里面,在这里先不要在 devtools 中实现…

目录

    • RestClient
      • 定义索引
      • 安装与配置测试类
      • 插入索引
      • 索引的 CRUD
      • 批量导入文档

RestClient


定义索引

引入对应 sql 后,需要添加 sql 对应的 es 索引

下面是根据 sql 结构来构建的索引树,我们需要插入到 es 里面,在这里先不要在 devtools 中实现,下一节我们将会使用 restclient 来插入这个索引

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"}}}
}

copy_to 字段的作用是将当前字段附加到另外一个字段内,当搜索时就会联合被附加的字段一起搜索

比如上面将 business、brand 和 name 均附加到了 all 字段上,而被附加的 all 并不会额外添加内容,只是被搜索时他们三个会同时被检索,提高效率,仅此而已


安装与配置测试类

pom 内添加 es 依赖项,这里先略去版本

<dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>

然后到 properties 标签内添加 es 的版本,注意这里的版号一定要和你使用的 es 版本严格相等!
比如我这边用的是 es7.8.0,就必须是这个版本!

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

新建一个测试类,使用注解 @BeforeEach@AfterEach 来实现 restclient 链接与关闭的两大事务

@SpringBootTest
public class HotelIndexTest {private RestHighLevelClient client;@BeforeEachvoid setup() {this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://localhost:9200")));}@AfterEachvoid destory() throws IOException {client.close();}
}

插入索引

首先新建一个常量类,把插入索引的 json 保存到里面去

这段文本就是上上一节定义的插入索引 json

package cn.itcast.hotel.constant;public class HotelConst {public static final String HOTEL_INDEX = "{\n" +"  \"mappings\": {\n" +"    \"properties\": {\n" +"      \"id\":{\n" +"        \"type\": \"keyword\"\n" +"      },\n" +"      \"name\":{\n" +"        \"type\": \"text\",\n" +"        \"analyzer\": \"ik_max_word\",\n" +"        \"copy_to\": \"all\"\n" +"      },\n" +"      \"address\":{\n" +"        \"type\": \"keyword\",\n" +"        \"index\": false\n" +"      },\n" +"      \"price\":{\n" +"        \"type\": \"integer\"\n" +"      },\n" +"      \"score\":{\n" +"        \"type\": \"integer\"\n" +"      },\n" +"      \"brand\":{\n" +"        \"type\": \"keyword\",\n" +"        \"copy_to\": \"all\"\n" +"      },\n" +"      \"city\":{\n" +"        \"type\": \"keyword\"\n" +"      },\n" +"      \"starName\":{\n" +"        \"type\": \"keyword\"\n" +"      },\n" +"      \"business\":{\n" +"        \"type\": \"keyword\",\n" +"        \"copy_to\": \"all\"\n" +"      },\n" +"      \"location\":{\n" +"        \"type\": \"geo_point\"\n" +"      },\n" +"      \"pic\":{\n" +"        \"type\": \"keyword\",\n" +"        \"index\": false\n" +"      },\n" +"      \"all\":{\n" +"        \"type\": \"text\",\n" +"        \"analyzer\": \"ik_max_word\"\n" +"      }\n" +"    }\n" +"  }\n" +"}";
}

紧接着在测试类中编写插入 index 的方法即可

@Test
void testCreateIndex() throws IOException {// 创建索引请求CreateIndexRequest request = new CreateIndexRequest("hotel");// 设置索引的源数据request.source(HotelConst.HOTEL_INDEX, XContentType.JSON);// 使用客户端执行创建索引请求client.indices().create(request, RequestOptions.DEFAULT);
}

你可以使用以下两个测试方法来实现删除索引以及判断对应索引存在与否

// 删除索引
@Test
void testDeleteIndex() throws IOException {DeleteIndexRequest request = new DeleteIndexRequest("hotel");client.indices().delete(request, RequestOptions.DEFAULT);
}// 通过get索引,判断get请求的返回值来看索引是否已经创建
@Test
void testExistIndex() throws IOException {GetIndexRequest request = new GetIndexRequest("hotel");boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);System.out.println(exists);
}

索引的 CRUD

创建索引

首先引入 IHotelService 用来通过 id 查询 mysql 对应记录
然后通过 JSON.toJSONString 把对应的实体类转换为 JSON 字符串的格式用来创建索引

@SpringBootTest
public class ElasticSearchCRUDTest {@Autowiredprivate IHotelService hotelService;private RestHighLevelClient client;@Testvoid testIndexDoc() throws IOException {Hotel hotelServiceById = hotelService.getById(61083L);HotelDoc hotelDoc = new HotelDoc(hotelServiceById);IndexRequest request = new IndexRequest("hotel").id(hotelServiceById.getId().toString());request.source(JSON.toJSONString(hotelDoc), XContentType.JSON);client.index(request, RequestOptions.DEFAULT);}
}

根据指定 id 获取索引存储 JSON,并将其转换为实体类后输出

@Test
void testGetDoc() throws IOException{GetRequest request= new GetRequest("hotel","61083");GetResponse response = client.get(request, RequestOptions.DEFAULT);String source = response.getSourceAsString();HotelDoc hotelDoc = JSON.parseObject(source, HotelDoc.class);System.out.println(hotelDoc);
}

更新字段

字段更新有两种方式

  • 全量更新:删掉旧的,新建一个新的插进去
  • 局部更新:在原来的基础上更新需要的内容

下面展示了局部更新的方法

@Test
void testUpdateDoc() throws IOException {UpdateRequest request = new UpdateRequest("hotel", "61083");request.doc("price", "2103","starName", "星钻");client.update(request, RequestOptions.DEFAULT);
}

删除文档就更简单了,直接提供索引以及对应的 id 就好了

@Test
void testDeleteDoc() throws IOException{DeleteRequest request = new DeleteRequest("hotel", "61083");client.delete(request, RequestOptions.DEFAULT);
}

批量导入文档

使用 bulk 请求来实现批量索引查询

@Test
void testBulkImport() throws IOException {BulkRequest request = new BulkRequest();request.add(new IndexRequest("hotel").id("61083").source("json", XContentType.JSON));request.add(new IndexRequest("hotel").id("61083").source("json", XContentType.JSON));request.add(new IndexRequest("hotel").id("61083").source("json", XContentType.JSON));client.bulk(request, RequestOptions.DEFAULT);
}

查询所有酒店后使用 bulk 将他们依次插入索引

@Test
void testBulkIndex() throws IOException {List<Hotel> list = hotelService.list();BulkRequest request = new BulkRequest();for (Hotel hotel : list) {HotelDoc hotelDoc = new HotelDoc(hotel);request.add(new IndexRequest("hotel").id(hotelDoc.getId().toString()).source(JSON.toJSONString(hotelDoc), XContentType.JSON));}client.bulk(request, RequestOptions.DEFAULT);
}
http://www.dtcms.com/wzjs/391514.html

相关文章:

  • 万链网站做的怎么样?360站长
  • 学做衣服上什么网站好厦门seo外包公司
  • 球场 技术支持 东莞网站建设海外短视频跨境电商平台是真的吗
  • 苏州营销型网站制作公司百度企业官网
  • 四川宜宾网站建设百度舆情
  • 做软件的中介网站新闻发布的网站
  • 网站怎么优化自己免费西安网络科技有限公司
  • 建湖做网站价格广告制作
  • 做行程的网站推荐广告联盟接单平台
  • 湛江网站建设策划方案百度网盘官方
  • 网站建设文字教程视频俄罗斯引擎搜索
  • 新网站怎么做论坛推广网络营销品牌案例
  • 中国免费网站建设网站快速建站
  • 辽宁建筑工程网seo的优缺点
  • 最新备案域名查询专业排名优化工具
  • 网站制作经费预算表网上售卖平台有哪些
  • 做网站老板不发工资我拿尾款百度搜索引擎api
  • 做网站全屏尺寸是多少钱开封网络推广哪家好
  • 自己做网站可以揽业务吗淘宝推广费用一般多少
  • 大连网站建设公司怎么能在百度上做推广
  • o2o与网站建设论文项目平台
  • 免费做优化的网站关键词优化是什么意思?
  • 泗县住房和城乡建设局网站长春百度推广电话
  • 衡阳城乡建设部网站首页搜索引擎有哪些分类
  • 照片书哪个网站做的好推广品牌的方法
  • 美萍企业管理软件外贸seo优化公司
  • 网站用户体验肇庆网站建设制作
  • 网站建设与管理维护 大学论文seo标题优化关键词怎么选
  • 网站icp备案咋做外贸怎么建立自己的网站
  • 网站外包费用怎么做分录网络营销案例100例