天津网络推广网站建设公司,仿v电影的模板?好像是wordpress,临颖网站建设,淘宝支持做微交易网站吗#x1f468;#x1f393;作者简介#xff1a;一位大四、研0学生#xff0c;正在努力准备大四暑假的实习 #x1f30c;上期文章#xff1a;详解SpringCloud微服务技术栈#xff1a;ElasticSearch原理精讲、安装、实践 #x1f4da;订阅专栏#xff1a;微服务技术全家… 作者简介一位大四、研0学生正在努力准备大四暑假的实习 上期文章详解SpringCloud微服务技术栈ElasticSearch原理精讲、安装、实践 订阅专栏微服务技术全家桶 希望文章对你们有所帮助 在前面已经学习了如何使用DSL语句去操作ElasticSearch的索引库和文档现在需要用ES官方提供的RestClient这个客户端本质就是组装DSL语句通过http请求发送给ES从而方便我们使用Java代码进行操作。 ElasticSearch实战1——RestClient操作索引库与文档 导入demohotel数据结构分析RestClient操作索引库初始化RestClient创建索引库删除和判断索引库 RestClient操作文档新增文档查询文档更新文档删除文档批量导入文档 导入demo
sql文件和项目工程从网盘下自行导入 链接https://pan.baidu.com/s/1CDgGJGvpSu0-s6bFfLWvVA?pwdnrx2 提取码nrx2 hotel数据结构分析
mapping映射要考虑的问题字段名、数据类型、是否参与搜索、是否分词、若分词分词器选什么 所以我们需要针对数据库的字段来做mapping映射 这里面的信息在做mapping的时候主要是要会选择是否要参与搜索、是否分词这需要根据业务需求来实现比如用户找酒店的时候肯定不会是搜索酒店的地址因此酒店地址不应该加入搜索也不应该分词。
需要注意的是ES中对地理坐标的描述并不是字符串也不是数字它支持了2种坐标数据类型 geo_point由维度latitude和精度longitude确定的一个点 geo_shape有多个geo_point组成的复杂几何图形 容易发现里面有很多字段存在ES中将来都要参与搜索也就意味着当用户输入某个字段的时候需要根据这多个字段来搜效率肯定不高。 而ES提供了一个功能能够实现多字段搜索同时效率还高的即字段拷贝。也就是使用copy_to属性将当前字段拷贝到指定字段示例
all:{type: text,analyzer: ik_max_word
}
brand:{type: keyword,copy_to: all
}这样我们就可以把需要搜索的字段全部联合到all里面即可实现用户输入一个信息多字段搜索且效率是会大大提高的。 因此在dev tools中编写DSL代码
# 酒店索引库及mapping映射
PUT /hotel
{mapping: {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},startName:{type: keyword},business:{type: keyword,copy_to: all},location:{type: geo_point},pic:{type: keyword,index: false},all:{type: text,analyzer: ik_max_word}}}
}上面代码没有正式执行用来做个基础样式最终创建索引库还是要用ES的java客户端来实现方便之后简化开发。
RestClient操作索引库
初始化RestClient
1、引入ES的RestHighLevelClient依赖 dependencygroupIdorg.elasticsearch.client/groupIdartifactIdelasticsearch-rest-high-level-client/artifactIdversion7.12.1/version/dependency2、SpringBoot默认的ES版本是7.6.2因此需要覆盖
3、初始化RestHighLevelClient
public class HotelIndexTest {private RestHighLevelClient client;Testvoid testInit() {System.out.println(client);}BeforeEach //提前完成RestHighLevelClient对象的初始化void setUp() {this.client new RestHighLevelClient(RestClient.builder(HttpHost.create(http://192.168.177.130:9200)));}AfterEach //结束后要销毁这个初始化void tearDown() throws IOException {this.client.close();}
}创建索引库
结合DSL语句会比较好看懂代码如下 Testvoid createHotelIndex() throws IOException {//创建request对象CreateIndexRequest request new CreateIndexRequest(hotel);//准备请求的参数为JSON格式request.source(MAPPING_TEMPLATE, XContentType.JSON);//发送请求indices为index复数可以获得操作索引库的所有对象默认方式发送client.indices().create(request, RequestOptions.DEFAULT);}其中MAPPING_TEMPLATE就是之前的DSL语句复制过来当作静态常量来存储
在DEV TOOLS中输入GET /hotel即可查看是否成功创建
删除和判断索引库
其实这个可以自行的去做也挺容易实现的毕竟indices已经将所有操作索引库的api都封装好了按CtrlShift空格即可查看所有方法。 删除索引库 Testvoid testDeleteIndex() throws IOException {DeleteIndexRequest request new DeleteIndexRequest(hotel);client.indices().delete(request, RequestOptions.DEFAULT);}判断索引库是否存在 Testvoid testExistsHotelIndex() throws IOException {GetIndexRequest request new GetIndexRequest(hotel);boolean exists client.indices().exists(request, RequestOptions.DEFAULT);System.out.println(exists ? 存在 : 不存在);}RestClient操作文档
写一个小demo去数据库中查询酒店数据导入到hotel索引库从而实现酒店数据的CRUD。 需要先进行JavaRestClient的初始化在之前有代码了直接CV一下。 而对于文档的操作不再需要indices了。
新增文档
这里需要用的方法是index表示新增文档时候创建倒排索引。 private RestHighLevelClient client;Resourceprivate IHotelService hotelService;Testvoid testAddDocument() throws IOException {//根据id查询酒店Hotel hotel hotelService.getById(61083L);/*** Hotel对象中的地理位置是分为经度和纬度分别来存的但是ES中是用geo_point来存的* 因此需要先转换为文档类型HotelDoc*/HotelDoc hotelDoc new HotelDoc(hotel);//准备request对象IndexRequest request new IndexRequest(hotel).id(hotelDoc.getId().toString());//准备JSON文档传进去的需要是JSON格式的字符串request.source(JSON.toJSONString(hotelDoc), XContentType.JSON);//发送请求client.index(request, RequestOptions.DEFAULT);}查询文档
根据id查询到的文档数据是JSON需要反序列化为java对象
Testvoid testGetDocumentById() throws IOException {GetRequest request new GetRequest(hotel, 61083);GetResponse response client.get(request, RequestOptions.DEFAULT);//source里面是这个hotel的相关数据String json response.getSourceAsString();//将json反序列化为java对象HotelDoc hotelDoc JSON.parseObject(json, HotelDoc.class);System.out.println(hotelDoc hotelDoc);}更新文档
之前讲解了全量更新和局部更新这里只演示局部更新 Testvoid testUpdateDocument() throws IOException {UpdateRequest request new UpdateRequest(hotel, 61083);request.doc(price, 952,starName, 四钻);client.update(request, RequestOptions.DEFAULT);}删除文档 Testvoid testDeleteDocument() throws IOException {DeleteRequest request new DeleteRequest(hotel, 61083);client.delete(request, RequestOptions.DEFAULT);}再次查询返回null
批量导入文档
之前的新增都是一次就新增一条数据现在利用JavaRestClient批量导入酒店数据到ES中。 思路 1、利用mybatis-plus查询酒店数据 2、将查询到的酒店数据Hotel转换为文档类型数据HotelDoc 3、利用JavaRestClient中的Bulk批处理实现批量新增文档 Testvoid testBulkRequest() throws IOException {//批量查询酒店数据ListHotel hotels hotelService.list();//创建requestBulkRequest request new BulkRequest();//将hotels转换成HotelDocfor (Hotel hotel : hotels) {HotelDoc hotelDoc new HotelDoc(hotel);//准备参数添加多个新增的requestrequest.add(new IndexRequest(hotel).id(hotelDoc.getId().toString()).source(JSON.toJSONString(hotelDoc), XContentType.JSON));}//发送请求client.bulk(request, RequestOptions.DEFAULT);}在dev tools使用GET /hotel/_search进行批量查询