栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Springboot集成elasticsearch7

Springboot集成elasticsearch7

之前开发用的elasticsearch 6,由于媳妇公司最近要使用elasticsearch7,所以写了一个demo。

发现elasticsearch6和7还是有一些区别的。

言归正传。本次使用的es版本是7.13.1版本,安装了ik分词器插件,这里省略了安装过程,如果就是想跑demo直接下载windows版本的,干净又卫生。

先看一下springboot支持的版本。这里随便选了2.5.9版本的springboot

引入相关jar包,既然有springboot-starter肯定用他的

    
			org.springframework.boot
			spring-boot-starter-data-elasticsearch
    

按照之前开发6的经验直接配置文件添加

 卧槽。竟然过时了。不慌,还好是springboot自己的东西。没办法,翻一翻自动装配类找找感觉。

 

 看到propereties了。看来离胜利不远了再往下看

 

 卧槽。这不都帮我做完了吗。不愧是springboot。懒人最爱。那就配置上直接用。开始写demo

#Elasticsearch 配置
spring.elasticsearch.rest.uris=http://127.0.0.1:9200
spring.elasticsearch.rest.username=elastic
spring.elasticsearch.rest.password=tongyu1956125

 创建实体类,这里和mybatis-plus用一个实体类,@document标签不懂的百度吧。这里面和6还是有差别的。es6以前支持多个type,es6只支持1个type,es7已经移除type了。

@Data
@document(indexName = "rm_policy_publication",replicas = 1,shards = 5)
public class RmPolicyPublication extends Model {

    private static final long serialVersionUID = 1L;

    
    @Field(type = FieldType.Keyword)
    @TableId(type = IdType.ID_WORKER_STR)
    private String id;

    
    @Field(type = FieldType.Text,analyzer = "ik_max_word",searchAnalyzer = "ik_max_word")
    private String title;

    
    @Field(type = FieldType.Keyword) // 这个不分词 可以用于关键字查询  具体哪个需要分词 哪个不分词 你要自己确定
    private String serialNumber;

    
    @Field(type = FieldType.Text,analyzer = "ik_max_word",searchAnalyzer = "ik_max_word")
    private String policyNumber;

    
    @Field(type = FieldType.Auto)
    private String effectiveLevel;

    
    @Field(type = FieldType.Auto)
    private Integer effectiveLevelId;

    
    @Field(type = FieldType.Text,analyzer = "ik_max_word",searchAnalyzer = "ik_max_word")
    private String province;

    
    @Field(type = FieldType.Auto)
    private String timely;
    @Field(type = FieldType.Auto)
    private Integer timelyId;

    
    private String issuerClass;

    
    @Field(type = FieldType.Text,analyzer = "ik_max_word",searchAnalyzer = "ik_max_word")
    private String issuer;

    private Integer issuerId;

    
    @Field(type = FieldType.Text,analyzer = "ik_max_word",searchAnalyzer = "ik_max_word")
    private String assIssuer;
    @Field(type = FieldType.Auto)
    private Integer assIssuerId;

    
    @Field(type = FieldType.Date)
    private Date publicDate;

    
    @Field(type = FieldType.Auto)
    private Date implementDate;

    
    @Field(type = FieldType.Auto)
    private Date revocateDate;

    
    @Field(type = FieldType.Auto)
    private String caption;

    
    @Field(type = FieldType.Keyword)
    private String keyword;

    
    @Field(type = FieldType.Auto)
    private String titleClass;

    @Field(type = FieldType.Auto)
    private Integer titleClassId;

    
    @Field(type = FieldType.Text,analyzer = "ik_max_word",searchAnalyzer = "ik_max_word")
    private String policyClass;

    @Field(type = FieldType.Auto)
    private Integer policyClassId;

    
    @Field(type = FieldType.Auto)
    private String featuredClass;

    
    @Field(type = FieldType.Auto)
    private String dataSource;

    
    @Field(type = FieldType.Text,analyzer = "ik_max_word",searchAnalyzer = "ik_max_word")
    private String text;

    
    @Field(type = FieldType.Auto)
    private String parentPolicyid;

    
    @Field(type = FieldType.Auto)
    private String topicId;

    
    @Field(type = FieldType.Auto)
    private String attachId;

    
    @Field(type = FieldType.Auto)
    private String status;

    
    @Field(type = FieldType.Auto)
    private String isDelete;

    
    @Field(type = FieldType.Auto)
    private Integer extend;
    @Field(type = FieldType.Auto)
    private Date createTime;
    @Field(type = FieldType.Auto)
    private String createId;
    @Field(type = FieldType.Auto)
    private String createName;

    
    @Field(type = FieldType.Auto)
    private Integer isReplaceOld;

    
    @Field(type = FieldType.Auto)
    private String oldpolicyId;

    
    @Field(type = FieldType.Auto)
    private String oldpolicyName;

    
    @Field(type = FieldType.Auto)
    private Integer isReplaced;

    
    @Field(type = FieldType.Auto)
    private String newpolicyId;

    
    @Field(type = FieldType.Auto)
    private String newpolicyName;
    @Field(type = FieldType.Auto)
    private String pdfId;
    @Field(type = FieldType.Auto)
    private String textNonformat;
    
}

创建dao使用springboot-data-jpa ,String是实体的主键。这样简单的CRUD都有了

@Component
public interface RmPolicyPublicationDao extends ElasticsearchRepository {

}

 直接注入dao和template可以直接用,简直太方便了

    @Resource
    private RmPolicyPublicationDao rmPolicyPublicationDao;

    @Resource
    private ElasticsearchRestTemplate elasticsearchTemplate;

 随便写一个查询

 @Override
    public List highQuery() {
        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("text", "化部办");


        logger.info("DSL:{}",matchQueryBuilder.toString());
        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
        nativeSearchQueryBuilder.withQuery(matchQueryBuilder)
                .withFields("text")
                .withHighlightFields(new HighlightBuilder.Field("text"))
                .withHighlightBuilder(new HighlightBuilder().preTags("").postTags(""));
        NativeSearchQuery nativeSearchQuery = nativeSearchQueryBuilder.build();

        SearchHits search = elasticsearchTemplate.search(nativeSearchQuery, RmPolicyPublication.class);

        List list = new ArrayList<>();
        List> searchHits = search.getSearchHits();
        for (SearchHit searchHit : searchHits) {
            Map> highlightFields = searchHit.getHighlightFields();
            //将高亮的内容填充到content中
            searchHit.getContent().setIssuer(highlightFields.get("text")==null ? searchHit.getContent().getIssuer():highlightFields.get("text").get(0));
            list.add(searchHit.getContent());
        }
        return list;
    }

完成了,剩下也没啥了  demo我直接扔 码云上了 自己看吧。

springboot-es-demo: springboot集成es7

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/735817.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号