springelasticsearch

Spring Boot 配置ElasticSearch 史上最全

2019-06-23  本文已影响257人  _大叔_

这里强烈建以不要使用高版本的ES,Spring boot 目前最高支持6.x,没有到7,我是用的是ES7,但在连接的时候报版本不对,降成6.6.0,之后装了分词插件就好了。

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

2.配置文件

spring:
  data:
    elasticsearch:
      #配置客户端的其他信息
      #properties:
      # 多个节点以,号分割
      cluster-nodes: 192.168.15.130:9300,192.168.15.128:9300
      # 集群名称
      cluster-name: ES
      # 是否启用ElasticSearch存储库 默认为true
      repositories:
        enabled: true

3.实体类

package com.cloud.mqtt.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;

/**
 * @ClassName BlogInfo
 * @Desccription 日志
 * @Author 大叔
 * @Dare 2019/6/19 17:27
 **/
@Data
@Document(indexName = "blog",type = "blogInfo",shards = 1,replicas = 1)
public class BlogInfo {

    /**
     * 主键id
     */
    @Id
    private String id;

    /**
     * 标题
     * 是否需要分词?  Text 表示文本,可被分词也可以被索引,keyword表示为关键字,
     *                  不分词,但可以被索引,只是String可被分词
     * 是否需要索引?  index为true表示可被索引,为false表示不可被索引
     * 是否需要存储?  store为true表示可被存储,为false表示不可被存储
     */
    @Field(type = FieldType.Text,analyzer = "ik_max_word")
    private String title;

    //**
     * 创建时间
     */
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    private Date createDate;

    /**
     * 关键字
     */
    @Field(type = FieldType.Keyword)
    private String keyword;

    /**
     * 内容
     */
    @Field(type = FieldType.Text,analyzer = "ik_max_word")
    private String content;

}

4.注解解释

@Document

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.springframework.data.elasticsearch.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.elasticsearch.index.VersionType;
import org.springframework.data.annotation.Persistent;

@Persistent
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface Document {
    // 索引库的名称,类似我们的 mysql 库的名称
    String indexName();
    // 文档类型,类似我们的 mysql 表的名称
    String type() default "";
    // 是否使用服务配置
    boolean useServerConfiguration() default false;
    // 分区 默认5
    short shards() default 5;
    // 复制分区 默认1
    short replicas() default 1;
    // 刷新间隔
    String refreshInterval() default "1s";
    // 索引文件存储类型
    String indexStoreType() default "fs";
    // 是否创建索引
    boolean createIndex() default true;

    VersionType versionType() default VersionType.EXTERNAL;
}

@Field

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.springframework.data.elasticsearch.annotations;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
@Documented
@Inherited
public @interface Field {
    // 自动检测属性的类型
    FieldType type() default FieldType.Auto;
    // 是否创建属性的索引
    boolean index() default true;
    // 
    DateFormat format() default DateFormat.none;

    String pattern() default "";
    // 是否存储
    boolean store() default false;
    
    boolean fielddata() default false;
    // 指定字段搜索时使用的分词器
    String searchAnalyzer() default "";
    // 指定分词器,存储字段时
    String analyzer() default "";

    String normalizer() default "";
    // 如果某个字段需要被忽略,就加进去
    String[] ignoreFields() default {};
    // 是否解析
    boolean includeInParent() default false;

    String[] copyTo() default {};
}

5.Dao层

package com.cloud.mqtt.dao;

import com.cloud.mqtt.entity.BlogInfo;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

/**
 * @ClassName BlogDao
 * @Desccription Dao
 * @Author 大叔
 * @Dare 2019/6/19 17:33
 **/
@Repository
public interface BlogDao extends ElasticsearchRepository<BlogInfo,String> {
    // 可以用jpa 语法
    void queryAllByContent(String str);
}

在这里我就不写service了,和web(controller)层公用了,叫web我是懒。。

6.web层

package com.cloud.mqtt.web;

import com.cloud.mqtt.dao.BlogDao;
import com.cloud.mqtt.entity.BlogInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @ClassName TestWeb
 * @Desccription
 * @Author 大叔
 * @Dare 2019/6/19 21:21
 **/
@RestController
@RequestMapping("/test")
public class TestWeb {

    @Autowired
    private BlogDao blogDao;
    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

}

7.ES-Head (建以安装在本地)

需要nodejs,下载地址在这里奉上:https://github.com/mobz/elasticsearch-head

ES健康插件
安装好了之后,请求你的ES,在浏览器打开控制台会发现跨域报错,只需要在ES的elasticsearch.yml 文件下添加如下既可:
network.host: 0.0.0.0

#因为需要跨服务器访问,所以需要允许外部访问
http.cors.enabled: true
http.cors.allow-origin: "*"

8.ES Id

用spring boot 新增的数据是没有id的,一般id都为导入数据的id,自己用的话就uuid吧。

9.如果项目中使用了分词器,则服务器的ES必须下载对应版本的分词器插件到plugins下

插件下载地址:https://github.com/medcl/elasticsearch-analysis-ik/releases
分词器:https://github.com/medcl/elasticsearch-analysis-ik
顺便介绍几个分词器:

standard // 会将词汇转换成小写形式,并去除停用词(a,e,an 没有实际意义的单词)和标点符号,支持中文采用的方法为单字切分
simple // 首先会通过非字母字符来分割文本信息,然后将词汇单元统一为小写形式,该分析器会去掉数字类型的字符
Whitespace // 仅仅是去除空格,对字符没有转小写,不支持中文;并且对生成的词汇单元进行其他的标准化处理
language // 特定语言的分词器,不支持中文
上一篇 下一篇

猜你喜欢

热点阅读