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

网站建设结算系统宣传网站站点最有效的方式是

网站建设结算系统,宣传网站站点最有效的方式是,成都最新数据消息,wordpress 微论坛主题Elasticsearch 是一个分布式、RESTful 风格的搜索和数据分析引擎#xff0c;能够实现近乎实时的搜索。 Elasticsearch官网https://www.elastic.co/cn/ 这篇文章主要简单介绍一下Elasticsearch#xff0c;Elasticsearch的java API博主也在学习中#xff0c;文章会持续更新~ …Elasticsearch 是一个分布式、RESTful 风格的搜索和数据分析引擎能够实现近乎实时的搜索。 Elasticsearch官网https://www.elastic.co/cn/ 这篇文章主要简单介绍一下ElasticsearchElasticsearch的java API博主也在学习中文章会持续更新~ 目录 第一步下载Elasticsearch 下载7.6.2版本 下载其他版本 第二步安装Elasticsearch 第三步安装kibana 第四步Springboot整合Elasticsearch 1、创建springboot项目 2、在pom.xml中添加依赖 3、修改配置文件 4、创建数据库和es的实体类 5 、创建mapper接口及映射文件 6、创建Elasticsearch的查询接口 7、开启mapper包扫描 第五步从mysql导入数据到es 第六步学习DSL 1、无条件查询默认返回10条数据 2、指定返回的数据条数 3、指定查询字段 4、分页查询 5、查询指定ID的数据 6、删除索引 7、条件查询 第七步在java中使用Elasticsearch 1、通过ElasticsearchRepository 2、通过ElasticsearchRestTemplate 3、通过RestHighLevelClient 第一步下载Elasticsearch 下载7.6.2版本 文章中使用的Elasticsearch版本是7.6.2可以通过以下网盘链接下载里面有7.6.2版本的ik分词器链接永久有效。 elasticsearch7.6.2下载https://pan.baidu.com/s/1D_HS8w_WW3dfQllGzNGv8A?pwdp3aa 下载完成后把下面选中的Elasticsearch压缩文件解压到D盘 然后把ik分词器解压到Elasticsearch的plugins目录下并把文件夹名称修改为ik。 下载其他版本 如需安装其他版本可自行在官网下载。 1、访问官网在首页点击页面上方的【文档】 2、点击All Elastic docs 3、点击选择other versions 4、在左侧版本列表选择对应的版本点击对应链接比如选择7.6.2版本 5、然后点击Installing the Elastic Stack 6、 在页面找到Elasticsearch点击后面的install instructions安装说明 7、然后找到对应操作系统比如windows只需要点击Install Elasticsearch with .zip on Windows 8、在打开的页面向下滚动找到Download and install the .zip package 9、点击后面的zip压缩文件链接开始下载https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.6.2-windows-x86_64.zip 第二步安装Elasticsearch 把下载下来的压缩包解压到系统盘建议解压到D盘如图 打开bin目录双击下面的文件启动Elasticsearch 稍微等一下启动完成后访问localhost:9200如果返回了以下格式的数据说明elasticsearch到此安装完成 第三步安装kibana 问了方便使用elasticsearch的Query DSL(Domain Specified Language领域专用语言)需要安装一下Elasticsearch的Kibana可视化控制台管理工具。 安装步骤和第二步类似这里就不赘述了安装完成后还是解压到D盘 然后修改一下config目录下的kibaba.yml配置文件 使用文本编辑器打开yml文件找到以下内容并取消注释修改为对应的值 server.port: 5601server.host: localhostserver.name: heyunlin # 这个可以随意取名elasticsearch.hosts: [http://localhost:9200] # elasticsearch服务器的地址i18n.locale: zh-CN # 页面使用中文 最后打开bin目录双击kibaba.bat启动kibaba 启动完成后在浏览器地址栏输入http://localhost:5601/app/kibana#/dev_tools/console 然后就可以在左边写我们的DSL了点击右边的运行按钮即可直接执行查询语句。 第四步Springboot整合Elasticsearch 这一步是java程序员最注重的怎么在java中使用elasticsearch。 1、创建springboot项目 首先创建一个springboot项目elastic 2、在pom.xml中添加依赖 提供了完整的pom文件可直接复制。 ?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.3.4.RELEASE/versionrelativePath//parentgroupIdcom.example/groupIdartifactIdelastic/artifactIdversion0.0.1-SNAPSHOT/versionpropertiesjava.version1.8/java.versiondruid.version1.1.21/druid.versionmysql.version8.0.28/mysql.versionlombok.version1.18.22/lombok.versionmybatis.version2.2.2/mybatis.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion${mysql.version}/version/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion${lombok.version}/version/dependencydependencygroupIdcom.alibaba/groupIdartifactIddruid/artifactIdversion${druid.version}/version/dependencydependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot-starter/artifactIdversion${mybatis.version}/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-elasticsearch/artifactId/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build /project 3、修改配置文件 修改elastic.yml复制以下内容 spring:# 数据库配置datasource:username: rootpassword: rooturl: jdbc:mysql://localhost:3306/elastictype: com.alibaba.druid.pool.DruidDataSourceserver:port: 9021mybatis:mapper-locations: classpath:mapper/*Mapper.xml 4、创建数据库和es的实体类 创建entity包然后创建Song.javaDocument(indexName songs)注解指定索引名为songsField注解配置字段的类型只有text类型的字段会被分词。 package com.example.elastic.entity;import lombok.Data; import org.springframework.data.annotation.Id; import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.annotations.Field; import org.springframework.data.elasticsearch.annotations.FieldType;/*** 歌曲* author heyunlin* version 1.0*/ Data Document(indexName songs) public class Song {IdField(type FieldType.Keyword)private String id;/*** 歌曲名*/Field(type FieldType.Text, analyzer ik_max_word)private String name;/*** 歌手*/Field(type FieldType.Text, analyzer ik_max_word)private String singer;/*** 描述信息*/Field(type FieldType.Text, analyzer ik_max_word)private String note;/*** 歌曲文件*/private String url;/*** 歌曲文件是否存在/是否已上传*/Field(type FieldType.Long)private Integer uploaded;// /** // * 最后一次修改时间 // */ // DateTimeFormat(pattern yyyy-MM-dd HH:mm:ss) // JsonFormat(pattern yyyy-MM-dd HH:mm:ss, timezone GMT8) // private LocalDateTime lastUpdateTime; } 5 、创建mapper接口及映射文件 创建mapper包创建一个接口SongMapper.java package com.example.elastic.mapper;import com.example.elastic.entity.Song; import org.springframework.stereotype.Repository;import java.util.List;/*** author heyunlin* version 1.0*/ Repository public interface SongMapper {ListSong selectAll(); } 在resources目录下创建mapper包然后创建SongMapper.xml ?xml version1.0 encodingUTF-8 ? !DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtdmapper namespacecom.example.elastic.mapper.SongMapperresultMap idresultMap typecom.example.elastic.entity.Songresult propertyid columnid /result propertyname columnname /result propertynote columnnote /result propertysinger columnsinger /result propertyurl columnurl /result propertyuploaded columnuploaded / !-- result propertylastUpdateTime columnlast_update_time /--/resultMapselect idselectAll resultMapresultMapselect * from song/select /mapper 6、创建Elasticsearch的查询接口 创建一个接口继承ElasticsearchRepositoryE, T接口该接口的第一个参数类型为实体类型二个参数类型是实体类的ID属性的数据类型在这里是String。 package com.example.elastic.repository;import com.example.elastic.entity.Song; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import org.springframework.stereotype.Repository;/*** author heyunlin* version 1.0*/ Repository public interface SongRepository extends ElasticsearchRepositorySong, String {} 7、开启mapper包扫描 创建一个配置类MybatisConfig在类上面使用Configuration将该类声明为配置类通过MapperScan注解指定mapper包扫描路径。 package com.example.elastic.config;import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Configuration;/*** Mybatis配置类* author heyunlin* version 1.0*/ Configuration MapperScan(basePackages com.example.elastic.mapper) public class MybatisConfig {} 第五步从mysql导入数据到es 通过上一步骤已经成功完成es的整合接下来查询mysql数据库把mysql的数据保存到es中。 接下来需要创建数据库elastic然后运行sql脚本SQL脚本文件在文章末尾的项目链接对应项目上在这里就不贴出来了太长了。 修改测试类运行initData()方法 package com.example.elastic;import com.example.elastic.entity.Song; import com.example.elastic.mapper.SongMapper; import com.example.elastic.repository.SongRepository; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;import java.util.List;SpringBootTest class ElasticApplicationTests {Autowiredprivate SongMapper songMapper;Autowiredprivate SongRepository repository;Testvoid initData() {ListSong list songMapper.selectAll();for (Song song : list) {repository.save(song);}}} 第六步学习DSL 完成第五步之后我们的es里已经有了803条歌曲的数据了接下来学习DSL的使用DSL就是Elasticsearch特有的查询语言。 DSL的格式 其中index_name指的是Elasticsearch中的索引名我们歌曲对应的索引名通过Document注解指定为了songs GET /index_name/_search {json请求体数据} 接下来介绍一下Elasticsearch中常用的DSL 1、无条件查询默认返回10条数据 GET /songs/_search {query: {match_all: {}} } 返回的数据格式为了避免太占位置只查询了5条记录。 hits里是查询结果信息hits.total.value表示符合查询条件的总记录数hits.hits表示的是返回的数据_source里是具体的数据。 {took : 2,timed_out : false,_shards : {total : 1,successful : 1,skipped : 0,failed : 0},hits : {total : {value : 808,relation : eq},max_score : 1.0,hits : [{_index : songs,_type : _doc,_id : 20210522154945,_score : 1.0,_source : {_class : com.example.elastic.entity.Song,id : 20210522154945,name : 诺言,singer : 陈洁丽,note : 《百变机兽之洛洛历险记》动画ED,uploaded : 0}},{_index : songs,_type : _doc,_id : 20210522155349,_score : 1.0,_source : {_class : com.example.elastic.entity.Song,id : 20210522155349,name : 快乐星猫,singer : 牛奶咖啡,note : 《快乐星猫》动画主题曲,uploaded : 0}},{_index : songs,_type : _doc,_id : 20210522155118,_score : 1.0,_source : {_class : com.example.elastic.entity.Song,id : 20210522155118,name : 无别,singer : 张信哲,note : 《天官赐福》动画OP,uploaded : 0}},{_index : songs,_type : _doc,_id : 20210522154331,_score : 1.0,_source : {_class : com.example.elastic.entity.Song,id : 20210522154331,name : 爱一点,singer : 王力宏、章子怡,note : ,uploaded : 0}},{_index : songs,_type : _doc,_id : 20210522154139,_score : 1.0,_source : {_class : com.example.elastic.entity.Song,id : 20210522154139,name : 多肉少女,singer : 赵芷彤Cassie,note : ,uploaded : 0}}]} } 2、指定返回的数据条数 通过size指定需要返回的结果数以下查询语句将会返回20条数据而非默认的10条 GET /songs/_search {query: {match_all: {}},size: 20 } 3、指定查询字段 _source是一个数组指定需要返回哪些字段设置为false则不会返回数据。 GET /songs/_search {query: {match_all: {}},size: 5, _source: [name, singer, note] } 4、分页查询 通过fromsize实现分页查询下面查询了第6-10条记录相当于mysql中的limit 5, 5和mysql类似from默认为0 GET /songs/_search {query: {match_all: {}},from: 5,size: 5 } 5、查询指定ID的数据 GET /songs/_doc/20210522155349 6、删除索引 发送路径为/songs的delete请求即可删除songs这个索引。 DELETE /songs 7、条件查询 以下是查询歌曲名中包含“爱”字的歌曲不指定返回的结果数则默认返回前10条。 GET /songs/_search {query: {match: {name: 爱}} } 第七步在java中使用Elasticsearch 这个章节会介绍三种通过java操作Elasticsearch的方式提供了简单的使用案例代码。 1、通过ElasticsearchRepository ElasticsearchRepository有一套标准的方法命名规范符合规范的方法名在输入的时候就会有提示比如findByXxx()ElasticsearchRepository会自动为其实现类中符合命名规范的方法生成对应的DSL语句。 我们在之前的SongRepository接口中声明一个findByName()方法根据歌曲名查询歌曲列表。 package com.example.elastic.repository;import com.example.elastic.entity.Song; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import org.springframework.stereotype.Repository;import java.util.List;/*** author heyunlin* version 1.0*/ Repository public interface SongRepository extends ElasticsearchRepositorySong, String {ListSong findByName(String name);} 然后通过测试类测试该方法 package com.example.elastic;import com.example.elastic.entity.Song; import com.example.elastic.repository.SongRepository; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;import java.util.List; import java.util.Optional;/*** author heyunlin* version 1.0*/ SpringBootTest public class ElasticsearchRepositoryTests {Autowiredprivate SongRepository songRepository;Testvoid testFindByName() {ListSong list songRepository.findByName(雨爱);System.out.println(共查询到 list.size() 条记录。);System.out.println(~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~);for (Song song : list) {System.out.println(song);}}Testvoid testSave() {Song song new Song();song.setId(2023);song.setName(雨爱);song.setNote(雨爱);Song save songRepository.save(song);System.out.println(save save);}Testvoid testDelete() {Song song new Song();song.setId(2023);song.setName(雨爱);song.setNote(雨爱);songRepository.delete(song);}Testvoid testDeleteById() {songRepository.deleteById(2023);testFindByName();}Testvoid testDeleteAll() {songRepository.deleteAll();}Testvoid testExistsById() {songRepository.existsById(2023);}Testvoid testFindById() {testSave();OptionalSong optional songRepository.findById(2023);if (optional.isPresent()) {Song song optional.get();System.out.println(song);}}Testvoid testCount() {long count songRepository.count();System.out.println(count);}} 如图查询出来73首符合条件的歌曲。 2、通过ElasticsearchRestTemplate 下面通过简单的案例来介绍ElasticsearchRestTemplate的使用相关的API博主也在学习中。 package com.example.elastic;import com.example.elastic.entity.Song; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.data.elasticsearch.core.IndexOperations; import org.springframework.data.elasticsearch.core.SearchHit; import org.springframework.data.elasticsearch.core.SearchHits; import org.springframework.data.elasticsearch.core.query.Query;import java.util.List;/*** author heyunlin* version 1.0*/ SpringBootTest public class ElasticsearchRestTemplateTests {Autowiredprivate ElasticsearchRestTemplate elasticsearchRestTemplate;Testvoid test() {SearchHitsSong search elasticsearchRestTemplate.search(Query.findAll(), Song.class);ListSearchHitSong searchHits search.getSearchHits();for (SearchHitSong searchHit : searchHits) {System.out.println(searchHit);}}Testvoid testIndexOps() {IndexOperations indexOperations elasticsearchRestTemplate.indexOps(Song.class);System.out.println(indexOperations.exists());}} 3、通过RestHighLevelClient 例如下面代码删除了songs索引中ID为2023的文档。 package com.example.elastic;import org.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.Requests; import org.elasticsearch.client.RestHighLevelClient; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;import java.io.IOException;/*** author heyunlin* version 1.0*/ SpringBootTest public class RestHighLevelClientTests {AutowiredRestHighLevelClient restHighLevelClient;Testvoid test() throws IOException {DeleteRequest songs Requests.deleteRequest(songs);songs.id(2023);DeleteResponse deleteResponse restHighLevelClient.delete(songs, RequestOptions.DEFAULT);int status deleteResponse.status().getStatus();System.out.println(status);}} DeleteRequest表示一次删除请求必须通过id()方法设置文档ID否则会抛出异常执行delete()方法会得到一个删除操作的响应对象可以通过getStatus()得到响应状态码就和我们的http请求响应状态码一样。 因为type这个概念在Elasticsearch7.x版本已经被弃用了所以type()这个方法也被声明了已废弃。 那么DeleteRequest又要通过什么方式得到呢 点开DeleteRequest的源码在类的注释上已经告诉我们最好的创建方式是通过Requests.deleteRequest()方法。 然后我们点开这个方法这个方法的参数是String类型变量名为index很显然这就是我们需要操作的索引的名字。这个方法的注释上说了必须设置id和type因为type的概念已经被删除则需要设置id这个id顾名思义就是文档ID。 /*** Creates a delete request against a specific index. Note the {link DeleteRequest#type(String)} and* {link DeleteRequest#id(String)} must be set.** param index The index name to delete from* return The delete request* see org.elasticsearch.client.Client#delete(org.elasticsearch.action.delete.DeleteRequest)*/ public static DeleteRequest deleteRequest(String index) {return new DeleteRequest(index); } 好了文章就分享到这里了看完不要忘了点赞收藏哦~ 项目已开源可按需获取 Springboot整合Elasticsearchhttps://gitee.com/he-yunlin/elastic.git
http://www.dnsts.com.cn/news/139754.html

相关文章:

  • 网站建设哪家技术好宁波工业设计公司排名
  • 环保网站建设公司公司介绍页面设计
  • 嘉定南翔网站建设网站开发可选的方案有
  • 实训报告网站开发oppo商店官网入口
  • 青岛网站建设q479185700棒深圳 网页设计公司
  • 营销型网站优势乐从网站建设
  • 广昌网站建设制作站酷网官网登录
  • 现在推广平台有哪些东莞seo建站广告
  • 集群注册的公司可以做网站备案德阳网站怎么做seo
  • 网站开发能进入无形资产吗沈阳网站建设公司熊掌号
  • 宠物网站项目安阳网约车
  • 中国画廊企业网站模板工程建筑公司
  • 织梦瀑布流网站模板微信小程序开发需要哪些技术
  • 在什么网站做兼职翻译网站开发与客户交流
  • 网站推广制作贵州企业网站建设公司
  • 江西网站设计欣赏不错的建设工程人员查询
  • 东平网站建设关键词带淘宝的网站不收录
  • 深圳做网站 百度智能小程序wordpress时间中文版
  • 南京科技网站设计费用化妆品网页设计素材
  • 百度做网站需要多少钱网站开发文章
  • 滕州seo优化大师免费下载
  • 做电影网站要怎么拿到版权郑州网站关键词优化公司
  • 微信公众号端网站开发软件公司网站素材
  • 杯子电子商务网站的建设城市建设网站的项目背景
  • 移动网站开发语言旅行网站首页模板
  • 医保局网站建设中标公告网站租服务器
  • 免费网站知乎新网站前期如何做seo
  • 网站的设计原则郑州上海做网站的公司
  • 实搜网站建设国外超酷网站
  • 2000个免费货源网站微商城推广平台哪个好