Spring Boot 配置ElasticSearch 搭建

这里强烈建以不要使用高版本的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 // 特定语言的分词器,不支持中文
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271

推荐阅读更多精彩内容