网文网站开发方案,宁波网站制作哪家优惠多,推广员是做什么的,订餐网站模板下载Java操作Elasticsearch的实用指南 一、创建索引二、增删改查 一、创建索引 
在ElasticSearch中索引相当于mysql中的表,mapping相当于表结构#xff0c;所以第一步我们要先创建索引。 
假设我们有一张文章表的数据需要同步到ElasticSearch#xff0c;首先需要根据数据库表创建… Java操作Elasticsearch的实用指南 一、创建索引二、增删改查 一、创建索引 
在ElasticSearch中索引相当于mysql中的表,mapping相当于表结构所以第一步我们要先创建索引。 
假设我们有一张文章表的数据需要同步到ElasticSearch首先需要根据数据库表创建ES的索引结构。 
-- 文章表
create table if not exists post
(id         bigint auto_increment comment id primary key,title      varchar(512)                       null comment 标题,content    text                               null comment 内容,tags       varchar(1024)                      null comment 标签列表json 数组,thumbNum   int      default 0                 not null comment 点赞数,favourNum  int      default 0                 not null comment 收藏数,userId     bigint                             not null comment 创建用户 id,createTime datetime default CURRENT_TIMESTAMP not null comment 创建时间,updateTime datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment 更新时间,isDelete   tinyint  default 0                 not null comment 是否删除,index idx_userId (userId)
) comment 帖子 collate  utf8mb4_unicode_ci; 
ElasticSearch的索引结构: 
aliases:别名为了方便后续数据迁移字段类型是text这个字段可以被分词可模糊查询字段类型是keyword只能完全匹配精确查询。analyzer(存储时生效的分词器):用ik_max_word拆的更碎、索引更多更有可能被搜出来search analyzer (查询时生效的分词器):用ik_smart更偏向于用户想要搜的分词。 
PUT post
{aliases: {post: {}},mappings: {properties: {title: {type: text,analyzer: ik_max_word,search_analyzer: ik_smart,fields: {keyword: {type: keyword,ignore_above: 256}}},content: {type: text,analyzer: ik_max_word,search_analyzer: ik_smart,fields: {keyword: {type: keyword,ignore_above: 256}}},tags: {type: keyword},userId: {type: keyword},createTime: {type: date},updateTime: {type: date},isDelete: {type: keyword}}}
}二、增删改查 
使用java客户端进行增删改查第一步导入依赖。 !-- elasticsearch--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-elasticsearch/artifactId/dependency第一种方式: ElasticsearchRepositoryPostEsDTO,Long默认提供了简单的增删改查多用于可预期的、相对没那么复杂的查询、自定义查询。  Testvoid testSelect() {System.out.println(postEsDao.count());PagePostEsDTO PostPage  postEsDao.findAll(PageRequest.of(0, 5, Sort.by(createTime)));ListPostEsDTO postList  PostPage.getContent();System.out.println(postList);}Testvoid testAdd() {PostEsDTO postEsDTO  new PostEsDTO();postEsDTO.setId(1L);postEsDTO.setTitle(我是章三);postEsDTO.setContent(张三学习java学习使我快乐);postEsDTO.setTags(Arrays.asList(java, python));postEsDTO.setUserId(1L);postEsDTO.setCreateTime(new Date());postEsDTO.setUpdateTime(new Date());postEsDTO.setIsDelete(0);postEsDao.save(postEsDTO);System.out.println(postEsDTO.getId());}Testvoid testFindById() {OptionalPostEsDTO postEsDTO  postEsDao.findById(1L);System.out.println(postEsDTO);}Testvoid testCount() {System.out.println(postEsDao.count());}Testvoid testFindByCategory() {ListPostEsDTO postEsDaoTestList  postEsDao.findByUserId(1L);System.out.println(postEsDaoTestList);}ES 中_开头的字段表示系统默认字段比如 _id如果系统不指定会自动生成。但是不会在surce 字段中补充 id 的值所以建议大家手动指定。 
支持根据方法名自动生成方法比如: 
ListcPostEsDTO findByTitle(String title);第二种方式: Spring 默认给我们提供的提作 es 的客户端对象 ElasticsearchRestTemplate也提供了增制改查它的增删改查更灵活适用于更复杂的操作。 ES的搜索条件 
GET /_search
{query: { bool: {   组合条件must: [   必须都满足{ match: { title:   Search  }},   模糊查询   { match: { content: Elasticsearch }}],filter: [ { term:  { status: published }},  精确查询{ range: { publish_date: { gte: 2015-01-01 }}}  范围查询],should : [{ term : { tags : env1 } },{ term : { tags : deployed } }],minimum_should_match : 1,   包含匹配最少匹配1条boost : 1.0}}
}对于复杂的查询建议使用第二种方式。 
//依赖注入
Resourceprivate ElasticsearchRestTemplate elasticsearchRestTemplate;三个步骤 1、取参数 2、把参数组合为ES支持的搜索条件 3、从返回值中取结果 Long id  postQueryRequest.getId();Long notId  postQueryRequest.getNotId();String searchText  postQueryRequest.getSearchText();String title  postQueryRequest.getTitle();String content  postQueryRequest.getContent();ListString tagList  postQueryRequest.getTags();ListString orTagList  postQueryRequest.getOrTags();Long userId  postQueryRequest.getUserId();// es 起始页为 0long current  postQueryRequest.getCurrent() - 1;long pageSize  postQueryRequest.getPageSize();String sortField  postQueryRequest.getSortField();String sortOrder  postQueryRequest.getSortOrder();BoolQueryBuilder boolQueryBuilder  QueryBuilders.boolQuery();// 过滤boolQueryBuilder.filter(QueryBuilders.termQuery(isDelete, 0));if (id ! null) {boolQueryBuilder.filter(QueryBuilders.termQuery(id, id));}if (notId ! null) {boolQueryBuilder.mustNot(QueryBuilders.termQuery(id, notId));}if (userId ! null) {boolQueryBuilder.filter(QueryBuilders.termQuery(userId, userId));}// 必须包含所有标签if (CollectionUtils.isNotEmpty(tagList)) {for (String tag : tagList) {boolQueryBuilder.filter(QueryBuilders.termQuery(tags, tag));}}// 包含任何一个标签即可if (CollectionUtils.isNotEmpty(orTagList)) {BoolQueryBuilder orTagBoolQueryBuilder  QueryBuilders.boolQuery();for (String tag : orTagList) {orTagBoolQueryBuilder.should(QueryBuilders.termQuery(tags, tag));}orTagBoolQueryBuilder.minimumShouldMatch(1);boolQueryBuilder.filter(orTagBoolQueryBuilder);}// 按关键词检索if (StringUtils.isNotBlank(searchText)) {boolQueryBuilder.should(QueryBuilders.matchQuery(title, searchText));//  boolQueryBuilder.should(QueryBuilders.matchQuery(description, searchText));boolQueryBuilder.should(QueryBuilders.matchQuery(content, searchText));boolQueryBuilder.minimumShouldMatch(1);}// 按标题检索if (StringUtils.isNotBlank(title)) {boolQueryBuilder.should(QueryBuilders.matchQuery(title, title));boolQueryBuilder.minimumShouldMatch(1);}// 按内容检索if (StringUtils.isNotBlank(content)) {boolQueryBuilder.should(QueryBuilders.matchQuery(content, content));boolQueryBuilder.minimumShouldMatch(1);}// 排序SortBuilder? sortBuilder  SortBuilders.scoreSort();if (StringUtils.isNotBlank(sortField)) {sortBuilder  SortBuilders.fieldSort(sortField);sortBuilder.order(CommonConstant.SORT_ORDER_ASC.equals(sortOrder) ? SortOrder.ASC : SortOrder.DESC);}// 分页PageRequest pageRequest  PageRequest.of((int) current, (int) pageSize);// 构造查询NativeSearchQuery searchQuery  new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withPageable(pageRequest).withSorts(sortBuilder).build();SearchHitsPostEsDTO searchHits  elasticsearchRestTemplate.search(searchQuery, PostEsDTO.class);后记 美好的一天到此结束下次继续努力欲知后续请看下回分解写作不易感谢大家的支持  文章转载自: http://www.morning.pdghl.cn.gov.cn.pdghl.cn http://www.morning.hnk25076he.cn.gov.cn.hnk25076he.cn http://www.morning.mdgb.cn.gov.cn.mdgb.cn http://www.morning.yrblz.cn.gov.cn.yrblz.cn http://www.morning.dmtld.cn.gov.cn.dmtld.cn http://www.morning.hqrkq.cn.gov.cn.hqrkq.cn http://www.morning.qwpdl.cn.gov.cn.qwpdl.cn http://www.morning.bfgpn.cn.gov.cn.bfgpn.cn http://www.morning.pxwzk.cn.gov.cn.pxwzk.cn http://www.morning.yksf.cn.gov.cn.yksf.cn http://www.morning.jwtwf.cn.gov.cn.jwtwf.cn http://www.morning.datadragon-auh.cn.gov.cn.datadragon-auh.cn http://www.morning.tnqk.cn.gov.cn.tnqk.cn http://www.morning.sknbb.cn.gov.cn.sknbb.cn http://www.morning.jrkzk.cn.gov.cn.jrkzk.cn http://www.morning.llgpk.cn.gov.cn.llgpk.cn http://www.morning.ynjhk.cn.gov.cn.ynjhk.cn http://www.morning.bylzr.cn.gov.cn.bylzr.cn http://www.morning.wmqrn.cn.gov.cn.wmqrn.cn http://www.morning.pwksz.cn.gov.cn.pwksz.cn http://www.morning.yjqkk.cn.gov.cn.yjqkk.cn http://www.morning.mjytr.cn.gov.cn.mjytr.cn http://www.morning.lhrwy.cn.gov.cn.lhrwy.cn http://www.morning.ljdhj.cn.gov.cn.ljdhj.cn http://www.morning.trplf.cn.gov.cn.trplf.cn http://www.morning.dodoking.cn.gov.cn.dodoking.cn http://www.morning.clyhq.cn.gov.cn.clyhq.cn http://www.morning.pjyrl.cn.gov.cn.pjyrl.cn http://www.morning.ylqpp.cn.gov.cn.ylqpp.cn http://www.morning.rlhgx.cn.gov.cn.rlhgx.cn http://www.morning.rmyt.cn.gov.cn.rmyt.cn http://www.morning.qyjqj.cn.gov.cn.qyjqj.cn http://www.morning.rwcw.cn.gov.cn.rwcw.cn http://www.morning.trkl.cn.gov.cn.trkl.cn http://www.morning.xqxlb.cn.gov.cn.xqxlb.cn http://www.morning.rrwft.cn.gov.cn.rrwft.cn http://www.morning.cmqrg.cn.gov.cn.cmqrg.cn http://www.morning.gfpyy.cn.gov.cn.gfpyy.cn http://www.morning.rmxwm.cn.gov.cn.rmxwm.cn http://www.morning.bmfqg.cn.gov.cn.bmfqg.cn http://www.morning.tmzlt.cn.gov.cn.tmzlt.cn http://www.morning.yqhdy.cn.gov.cn.yqhdy.cn http://www.morning.mcjrf.cn.gov.cn.mcjrf.cn http://www.morning.tplht.cn.gov.cn.tplht.cn http://www.morning.tqpds.cn.gov.cn.tqpds.cn http://www.morning.wlgpz.cn.gov.cn.wlgpz.cn http://www.morning.dbfp.cn.gov.cn.dbfp.cn http://www.morning.knlbg.cn.gov.cn.knlbg.cn http://www.morning.xrnh.cn.gov.cn.xrnh.cn http://www.morning.rfrnc.cn.gov.cn.rfrnc.cn http://www.morning.pyncx.cn.gov.cn.pyncx.cn http://www.morning.qxlxs.cn.gov.cn.qxlxs.cn http://www.morning.rsfp.cn.gov.cn.rsfp.cn http://www.morning.wschl.cn.gov.cn.wschl.cn http://www.morning.jnbsx.cn.gov.cn.jnbsx.cn http://www.morning.drswd.cn.gov.cn.drswd.cn http://www.morning.xxknq.cn.gov.cn.xxknq.cn http://www.morning.nlbhj.cn.gov.cn.nlbhj.cn http://www.morning.nlpbh.cn.gov.cn.nlpbh.cn http://www.morning.ypzr.cn.gov.cn.ypzr.cn http://www.morning.rqmqr.cn.gov.cn.rqmqr.cn http://www.morning.hcwjls.com.gov.cn.hcwjls.com http://www.morning.qtfss.cn.gov.cn.qtfss.cn http://www.morning.ltqzq.cn.gov.cn.ltqzq.cn http://www.morning.lmdfj.cn.gov.cn.lmdfj.cn http://www.morning.rntgy.cn.gov.cn.rntgy.cn http://www.morning.nbqwt.cn.gov.cn.nbqwt.cn http://www.morning.yzfrh.cn.gov.cn.yzfrh.cn http://www.morning.pbygt.cn.gov.cn.pbygt.cn http://www.morning.ctswj.cn.gov.cn.ctswj.cn http://www.morning.rbzd.cn.gov.cn.rbzd.cn http://www.morning.zxcny.cn.gov.cn.zxcny.cn http://www.morning.rnfn.cn.gov.cn.rnfn.cn http://www.morning.nqpy.cn.gov.cn.nqpy.cn http://www.morning.kszkm.cn.gov.cn.kszkm.cn http://www.morning.lngyd.cn.gov.cn.lngyd.cn http://www.morning.dyrzm.cn.gov.cn.dyrzm.cn http://www.morning.lwygd.cn.gov.cn.lwygd.cn http://www.morning.rfbq.cn.gov.cn.rfbq.cn http://www.morning.bfmrq.cn.gov.cn.bfmrq.cn