Docker安装Elasticsearch+SpringBoot整合Elasticsearch新手入门

Elasticsearch是面向文档(document oriented)的,这意味着它可以存储整个对象或文档(document)。然而它不仅仅是存储(store),还会索引(index)每个文档的内容使之可以被搜索。在Elasticsearch中,你可以对文档(而非成行成列的数据)进行索引、搜索、排序、过滤。Elasticsearch比较传统关系型数据库

Relational DB -> Databases -> Tables -> Rows -> Columns

Elasticsearch -> Indices   -> Types -> Documents -> Fields

先安装docker,如果安装了宝塔面板可以直接在商店安装docker

Docker安装Elasticsearch

拉取镜像并启动

#拉取镜像
docker pull elasticsearch:7.7.0

#启动镜像
docker run --name elasticsearch -d -e ES_JAVA_OPTS="-Xms512m -Xmx512m" -e "discovery.type=single-node" -p 9200:9200 -p 9300:9300 elasticsearch:7.7.0

--name 表示镜像启动后的容器名称  

-d: 后台运行容器,并返回容器ID;

-e: 指定容器内的环境变量

-p: 指定端口映射,格式为:主机(宿主)端口:容器端口

浏览器访问ip:9200

ElasticSearch面板

推荐浏览器安装Elasticvue插件管理ElasticSearch,Edge插件商店直接能安装,方便 。当然安装ElasticSearch Head也可。

Elasticvue | Edge插件商店

SpringBoot整合

注:如果Linux是自己电脑的虚拟机SpringBoot能直接访问,但是如果Linux是腾讯云阿里云等云服务器就需要修改ElasticSearch的配置文件才能远程访问。不是云服务器请跳过

docker exec -it elasticsearch bash
# docker命令进入elasticsearch

vi config/elasticsearch.yml
#通过vi编辑器打开配置文件

#文件内容如下
cluster.name: "docker-cluster"
network.host: 0.0.0.0
#添加以下两行 表示所有ip能访问elasticsearch
http.cors.enabled: true
http.cors.allow-origin: "*"

#保存文件 重启elasticsearch

创建Maven工程 在pom文件导入坐标

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.appmy</groupId>
    <artifactId>appmy-springboot-es</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

在cn.appmy下创建启动类

@SpringBootApplication
public class ESApplication {
    public static void main(String[] args) {
        SpringApplication.run(ESApplication.class, args);
    }
}

创建yml文件

server:
  port: 8080
spring:
  data:
    elasticsearch:
      cluster-nodes: 127.0.0.1:9300 
      #如果是云服务器 填服务器ip 记得防火墙
      cluster-name: elasticsearch

创建pojo 添加注解进行映射

/**
 * @Document:放置到类上
 *    indexName = "blog1":表示索引的名称,(小写)
 *    type = "article":表示类型
 * @Id:放置到字段id上
 *    表示该字段的值存放到索引库的_id字段上,表示主键
 * @Field:放置到字段上
 *    store = true:表示该字段的值存储到索引库
 *    index = true:表示该字段的值要建立索引用于搜索
 *    analyzer = "ik_smart":建立索引的时候使用什么分词器
 *    searchAnalyzer = "ik_smart":数据搜索的时候使用什么分词器(可以不写)
 *    type = FieldType.Text:存放字段的数据类型
 */
@Document(indexName = "blog03",type = "article")
public class Article implements Serializable {

    @Id
    private Long id;

    @Field(index = true,searchAnalyzer = "ik_smart",analyzer = "ik_smart",store = true,type = FieldType.Text)
    private String title;

    @Field(index = true,searchAnalyzer = "ik_smart",analyzer = "ik_smart",store = true,type = FieldType.Text)
    private String content;

    public Article() {
    }
    public Article(long id, String title, String content) {
        this.id = id;
        this.title = title;
        this.content = content;
    }


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public String toString() {
        return "Article{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", content='" + content + '\'' +
                '}';
    }
}

创建Dao接口继承ElasticsearchRepository

public interface ArticleDao  extends ElasticsearchRepository<Article,Long>{

}

测试

@SpringBootTest
@RunWith(SpringRunner.class)
public class EsApplicationTest03 {
    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @Autowired
    private ArticleDao dao;

    //创建索引
    //创建映射
    @Test
    public void createMapping() {
        elasticsearchTemplate.createIndex(Article.class);
        elasticsearchTemplate.putMapping(Article.class);
    }


}

原文博客icon-default.png?t=LA92https://appmy.cn/

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_慎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值