Springboot integrates elasticsearch7

The elasticsearch 6 used for development before, because the daughter-in-law company will use elasticsearch7 recently, so I wrote a demo.

It is found that there are still some differences between elasticsearch6 and 7.

Closer to home. The es version used this time is version 7.13.1, and the ik tokenizer plug-in is installed. The installation process is omitted here. If you just want to run the demo, download the windows version directly, which is clean and hygienic.

First look at the versions supported by springboot. Here I randomly selected the 2.5.9 version of springboot

Introduce related jar packages, since there is springboot-starter, definitely use his

    <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    </dependency>

Add directly to the configuration file according to the previous experience of developing 6

 Fuck. It's out of date. Don't panic, fortunately, it's springboot's own stuff. No way, look through the automatic assembly class to find the feeling.

 

 See the propereties. Looks like victory is not far away

 

 Fuck. Isn't this all done for me? It is indeed spring boot. Lazy favorite. Then configure it directly. start writing demo

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

 Create an entity class, use an entity class with mybatis-plus here, Baidu if you don’t understand the @Document tag. There is still a difference between this and 6. es6 previously supported multiple types, es6 only supported one type, and es7 has removed the type.

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

    private static final long serialVersionUID = 1L;

    /**
     * id
     */
    @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;

    /**
     * 效力级别(0:中央级,1:国务院级,2:部委级,3:部委司局和直属事业单位级,4:省(直辖市、计划单列市)级,5:省(直辖市、计划单列市)属事业单位级,6:地市级,7:地市所属事业单位级,8:其他)
     */
    @Field(type = FieldType.Auto)
    private String effectiveLevel;

    /**
     * 效力级别id
     */
    @Field(type = FieldType.Auto)
    private Integer effectiveLevelId;

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

    /**
     * 时效性(0:无有效期限,正在执行有效,1:有有效期限,正在执行有效,2:已废止)
     */
    @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;

    /**
     * 主题分类(0:国家科技创新领导体制,1:科技成果转化,2:科技人才发展,3:产学研协同创新,4:中央财政科技计划和经费管理改革,5:科技治理制度,6:区域创新发展,7:科技开放合作,8:科技创新立法)
     */
    @Field(type = FieldType.Auto)
    private String titleClass;

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

    /**
     * 公文类型(0:决定,1:通知,2:通报,3:报告,4:请示,5:批复,6:意见,7:函,8:会议纪要,9:命令(令),10:通告,11:议案,12:其他)
     */
    @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;

    /**
     * 父级政策id
     */
    @Field(type = FieldType.Auto)
    private String parentPolicyid;

    /**
     * 专题id
     */
    @Field(type = FieldType.Auto)
    private String topicId;

    /**
     * 附件id
     */
    @Field(type = FieldType.Auto)
    private String attachId;

    /**
     * 审核状态(0:待审核,1:审核未通过,2:审核通过)
     */
    @Field(type = FieldType.Auto)
    private String status;

    /**
     * 是否删除(0:未删除,1:已删除)
     */
    @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;

    /**
     * 是否废止旧政(0:是,1:否)
     */
    @Field(type = FieldType.Auto)
    private Integer isReplaceOld;

    /**
     * 同时废止旧政id
     */
    @Field(type = FieldType.Auto)
    private String oldpolicyId;

    /**
     * 旧政名称
     */
    @Field(type = FieldType.Auto)
    private String oldpolicyName;

    /**
     * 是否被新政替代(0:是,1:否)
     */
    @Field(type = FieldType.Auto)
    private Integer isReplaced;

    /**
     * 新政id
     */
    @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;
    
}

Create dao using springboot-data-jpa, String is the primary key of the entity. Such a simple CRUD has

@Component
public interface RmPolicyPublicationDao extends ElasticsearchRepository<RmPolicyPublication,String> {

}

 Direct injection of dao and template can be used directly, which is simply too convenient

    @Resource
    private RmPolicyPublicationDao rmPolicyPublicationDao;

    @Resource
    private ElasticsearchRestTemplate elasticsearchTemplate;

 write any query

 @Override
    public List<RmPolicyPublication> 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("<span style='color:red'>").postTags("</span>"));
        NativeSearchQuery nativeSearchQuery = nativeSearchQueryBuilder.build();

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

        List<RmPolicyPublication> list = new ArrayList<>();
        List<SearchHit<RmPolicyPublication>> searchHits = search.getSearchHits();
        for (SearchHit<RmPolicyPublication> searchHit : searchHits) {
            Map<String, List<String>> 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;
    }

It's done, there's nothing left. I just threw the demo on the code cloud and watched it for myself.

springboot-es-demo: springboot integrated es7

Guess you like

Origin blog.csdn.net/evil_lrn/article/details/122837528