elasticsearch入门 springboot2集成elasticsearch spring-data-elasticsearch实现全文搜索,图文讲解带源码

总结

本文从基础到高级再到实战,由浅入深,把MySQL讲的清清楚楚,明明白白,这应该是我目前为止看到过最好的有关MySQL的学习笔记了,我相信如果你把这份笔记认真看完后,无论是工作中碰到的问题还是被面试官问到的问题都能迎刃而解!

MySQL50道高频面试题整理:

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

| springboot版本 | Elasticsearch版本 |

| — | — |

| 2.1.3.RELEASE | 6.4.3 |

一,首先是创建springboot项目


在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

如上图箭头所指,springboot版本选2.1.3,然后添加web和elasticsearch仓库

  • 创建项目完成后,我们完整的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>

    <parent>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-parent</artifactId>

        <version>2.1.3.RELEASE</version>

        <relativePath/> <!-- lookup parent from repository -->

    </parent>

    <groupId>com.qcl</groupId>

    <artifactId>es</artifactId>

    <version>0.0.1</version>

    <name>es</name>

    <description>Demo project for Spring Boot</description>



    <properties>

        <java.version>1.8</java.version>

    </properties>



    <dependencies>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>



        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-test</artifactId>

            <scope>test</scope>

        </dependency>

    </dependencies>



    <build>

        <plugins>

            <plugin>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-maven-plugin</artifactId>

            </plugin>

        </plugins>

    </build>



</project>



spring-boot-starter-data-elasticsearch:就是我们所需要集成的es。


<dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>

        </dependency>



二,下载elasticsearch本地版本

这里下载本地elasticsearch,其实和我们下载本地mysql是一样的,你要用elasticsearch肯定要下载一个本地版本用来存储查询数据啊。

下面来简单的讲解下elasticsearch版本的下载步骤

  • 1,到官网

    https://www.elastic.co/downloads/

    在这里插入图片描述

    选择箭头所指,点击download

    在这里插入图片描述

    选择你所对应的系统,这里要注意,虽然官方最新版本是6.6.2,我们springboot项目里使用的是6.4.3版本。这个没有关系的,官方版本是向下兼容的。

  • 2,下载成功后解压,并进入到config文件夹下

    在这里插入图片描述

    进入config文件夹后,找到elasticsearch.yml

    在这里插入图片描述

    然后用下面这个文件替换elasticsearch.yml里面的内容


# ======================== Elasticsearch Configuration =========================

#

# NOTE: Elasticsearch comes with reasonable defaults for most settings.

#       Before you set out to tweak and tune the configuration, make sure you

#       understand what are you trying to accomplish and the consequences.

#

# The primary way of configuring a node is via this file. This template lists

# the most important settings you may want to configure for a production cluster.

#

# Please consult the documentation for further information on configuration options:

# https://www.elastic.co/guide/en/elasticsearch/reference/index.html

#

# ---------------------------------- Cluster -----------------------------------

#

# Use a descriptive name for your cluster:

#

cluster.name: my-application

#

# ------------------------------------ Node ------------------------------------

#

# Use a descriptive name for the node:

#

node.name: node-1

#

# Add custom attributes to the node:

#

#node.attr.rack: r1

#

# ----------------------------------- Paths ------------------------------------

#

# Path to directory where to store the data (separate multiple locations by comma):

#

#path.data: /path/to/data

#

# Path to log files:

#

#path.logs: /path/to/logs

#

# ----------------------------------- Memory -----------------------------------

#

# Lock the memory on startup:

#

#bootstrap.memory_lock: true

#

# Make sure that the heap size is set to about half the memory available

# on the system and that the owner of the process is allowed to use this

# limit.

#

# Elasticsearch performs poorly when the system is swapping the memory.

#

# ---------------------------------- Network -----------------------------------

#

# Set the bind address to a specific IP (IPv4 or IPv6):

#

network.host: 0.0.0.0

#

# Set a custom port for HTTP:

#

http.port: 9200

#

# For more information, consult the network module documentation.

#

# --------------------------------- Discovery ----------------------------------

#

# Pass an initial list of hosts to perform discovery when new node is started:

# The default list of hosts is ["127.0.0.1", "[::1]"]

#

#discovery.zen.ping.unicast.hosts: ["host1", "host2"]

#

# Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):

#

#discovery.zen.minimum_master_nodes: 

#

# For more information, consult the zen discovery module documentation.

#

# ---------------------------------- Gateway -----------------------------------

#

# Block initial recovery after a full cluster restart until N nodes are started:

#

#gateway.recover_after_nodes: 3

#

# For more information, consult the gateway module documentation.

#

# ---------------------------------- Various -----------------------------------

#

# Require explicit names when deleting indices:

#

#action.destructive_requires_name: true



#qcl自己加的

http.cors.enabled: true 

http.cors.allow-origin: "*"

node.master: true

node.data: true



这里的cluster.name: my-application就代表我们的es的名称叫my-application

  • 3,启动es

    进入到bin文件

    在这里插入图片描述

    在这里插入图片描述

    点击elasticsearch脚本,即可启动es,脚本运行完,在浏览器中输入http://localhost:9200/

    如果出现下面信息,就代表es启动成功。

    在这里插入图片描述

三,配置es

在创建的springboot项目下的application.yml做如下配置

在这里插入图片描述


#url相关配置,这里配置url的基本url

server:

  port: 8080

spring:

  ## Elasticsearch配置文件(必须)

  ## 该配置和Elasticsearch本地文件config下的elasticsearch.yml中的配置信息有关

  data:

    elasticsearch:

      cluster-name: my-application

      cluster-nodes: 127.0.0.1:9300



四,添加数据到es,并实现搜索

  • 1,创建bean

    我们像jpa那样,创建es自己的bean,如下


package com.qcl.es;



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;





/**

 * Created by qcl on 2018/7/10.

 * ES相关

 */

@Document(indexName = "user", type = "docs", shards = 1, replicas = 0)

public class UserES {



    //主键自增长

    @Id

    private Long id;//主键



    @Field(type = FieldType.Text, analyzer = "ik_max_word")

    private String userName;

    private String userPhone;





    public Long getId() {

        return id;

    }



    public void setId(Long id) {

        this.id = id;

    }



    public String getUserName() {

        return userName;



# **感受:**

其实我投简历的时候,都不太敢投递阿里。因为在阿里一面前已经过了字节的三次面试,投阿里的简历一直没被捞,所以以为简历就挂了。

特别感谢一面的面试官捞了我,给了我机会,同时也认可我的努力和态度。对比我的面经和其他大佬的面经,自己真的是运气好。别人8成实力,我可能8成运气。所以对我而言,我要继续加倍努力,弥补自己技术上的不足,以及与科班大佬们基础上的差距。希望自己能继续保持学习的热情,继续努力走下去。

**也祝愿各位同学,都能找到自己心动的offer。**

分享我在这次面试前所做的准备(刷题复习资料以及一些大佬们的学习笔记和学习路线),都已经整理成了电子文档

![拿到字节跳动offer后,简历被阿里捞了起来,二面迎来了P9"盘问"](https://img-blog.csdnimg.cn/img_convert/3f376103b8d77a06d89d4ab3d07f2b07.webp?x-oss-process=image/format,png)

> **本文已被[CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)收录**

**[需要这份系统化的资料的朋友,可以点击这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**


其实我投简历的时候,都不太敢投递阿里。因为在阿里一面前已经过了字节的三次面试,投阿里的简历一直没被捞,所以以为简历就挂了。

特别感谢一面的面试官捞了我,给了我机会,同时也认可我的努力和态度。对比我的面经和其他大佬的面经,自己真的是运气好。别人8成实力,我可能8成运气。所以对我而言,我要继续加倍努力,弥补自己技术上的不足,以及与科班大佬们基础上的差距。希望自己能继续保持学习的热情,继续努力走下去。

**也祝愿各位同学,都能找到自己心动的offer。**

分享我在这次面试前所做的准备(刷题复习资料以及一些大佬们的学习笔记和学习路线),都已经整理成了电子文档

[外链图片转存中...(img-dqY4y1Ac-1715283376324)]

> **本文已被[CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)收录**

**[需要这份系统化的资料的朋友,可以点击这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**

  • 17
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值