ElasticSearch(6.5.4)与SpringBoot整合

一、简介

Elasticsearch(ES)是一个基于Lucene构建的开源、分布式、RESTful接口的全文搜索引擎,也是一个分布式文档数据库,其中每个字段均可被索引,而且每个字段的数据均可被搜索,ES能够横向扩展至数以百计的服务器存储以及处理PB级的数据。可以在极短的时间内存储、搜索和分析大量的数据。通常作为具有复杂搜索场景情况下的核心发动机。

二、作用

ES可以根据用户搜索内容,为用户提供精准搜索,推荐相关商品。

当你想收集日志或者交易数据,想要分析并挖掘这些数据,从而寻找趋势,进行统计、总结或发现异常时,可以使用Logstash或者其他工具来进行收集数据,并将数据存储到ElasticsSearch中,进行搜索和汇总这些数据,找到任何你感兴趣的信息。

三、整合

  1. 在本机安装SE(windows)
    ElasticSearch 的下载地址:
    https://www.elastic.co/downloads/elasticsearch
    注:需已安装JDK1.8
    下载后解压,即可使用
    进入到bin下,双击elasticsearch.bat(打开ES),或者cmd 进入到D:\chengxu\ElasticSearch\elasticsearch-6.3.0 \bin 输入elasticsearch.bat,执行,执行http://localhost:9200 ,出现以下页面,则安装成功
    在这里插入图片描述
    若遇到闪退则可能java环境变量未配置完全(包括用户变量和系统变量)
  2. 安装ElasticSearch-head (ES可视化工具)
    依赖node.js,需先安装
  • 下载node.js

下载地址:
http://nodejs.cn/download/
可选择msi安装程序,直接安装,或使用zip解压安装

  • 安装nodejs(zip版本)
    a.解压后,创建两个文件夹
    node-global :npm全局安装位置
    node-cache:npm 缓存路径
    在这里插入图片描述
    b.配置环境变量
    将node.exe 所在的目录添加到path环境变量,在使用命令行时,可在任意路径使用node命令
    在这里插入图片描述在命令行中输入如下命令测试
    node -v
    npm -v
    如出现版本号基本上已经算是安装好了
    c.自定义
    可通过如下命令自定义全局安装目录及缓存路径
    npm config set prefix “E:\elasticsearch-6.5.4\nodejs\node-global”
    npm config set cache “E:\elasticsearch-6.5.4\nodejs\node-cache”
    执行:npm install webpack -g
    会发现node-global下node_modules中多了webpack 文件夹
    在这里插入图片描述
    webpack是用来打包的module,通常我们会在命令行中执行,而webpack同样在node-global中做了映射,所以只需要将node-global加入path环境变量即可。
    在这里插入图片描述
    然后运行命令安装 grunt :
    npm install -g grunt-cli
    在下载elasticsearch-head之前先修改elasticsearch.yml文件

在文件中加入
http.cors.enabled: true
http.cors.allow-origin: “*”
node.master: true
node.data: true

释放network.host: 192.168.0.1的注释并改为network.host: 0.0.0.0

释放http.port ,cluster.name和node.name的注释

d.elasticsearch-head下载
下载地址:https://github.com/mobz/elasticsearch-head
解压,修改elasticsearch-head-master\Gruntfile.js 添加在这里插入图片描述
进入elasticsearch-head-master_site修改app.js 中下文内容为服务器地址,若是本机部署,也可不修改。
在这里插入图片描述
cmd切换到对应的解压目录,进行npm install安装
启动: npm run start 或grunt server
在这里插入图片描述
输入http://localhost:9200出现
在这里插入图片描述
则表示安装成功

  1. 整合SpringBoot
  • 添加pom依赖
<?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.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.li</groupId>
    <artifactId>demo-es</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo-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-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <!--alibaba druid连接池  -->
        <!--Druid是Java语言中最好的数据库连接池。Druid能够提供强大的监控和扩展功能。-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>

        <!--Mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- 通用 UTIL 包 -->
       <dependency>
           <groupId>org.apache.commons</groupId>
           <artifactId>commons-io</artifactId>
           <version>1.3.2</version>
       </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.3.2</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.54</version>
        </dependency>
        <!-- es核心jar包 -->
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
        <dependency>
            <groupId>net.java.dev.jna</groupId>
            <artifactId>jna</artifactId>
            <!-- <version>3.0.9</version> -->
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.2</version>
        </dependency>

        <!--添加 lombok 依赖(@Getter和@Setter 方法)-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

在配置文件中添加
淡淡的

  • 创建实体类
package com.li.demoes.entity;

import lombok.Getter;
import lombok.Setter;
import org.springframework.data.elasticsearch.annotations.Document;

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Field;

import java.io.Serializable;

/**
 * @author li
 * @Description 实体类
 * @project springboot_learn
 * @package com.dalaoyang.entity
 * @email yangyang@dalaoyang.cn
 * @date 2018/5/4
 */
@Document(indexName = "test-boot",type = "goods",shards = 1,replicas = 0, refreshInterval = "-1")
//indexName索引名称 可以理解为数据库名 必须为小写 不然会报org.elasticsearch.indices.InvalidIndexNameException异常
//type类型 可以理解为表名
@Getter
@Setter
public class Elastic implements Serializable {
    @Id
    private String id;
    @Field
    private String name;
    @Field
    private String content;
    @Field
    private String price;
}

  • 创建ESDao ,继承ElasticsearchRepository
package com.li.demoes.dao;


import com.li.demoes.entity.Elastic;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Component;


@Component
public interface ESDao extends ElasticsearchRepository<Elastic, String> {
    Elastic queryEmployeeById(String id);
}

  • 新建一个controller进行测试,其中包含简单的增删改查
package com.li.demoes.controller;

import com.google.gson.Gson;
import com.li.demoes.dao.ESDao;
import com.li.demoes.entity.Elastic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/es")
public class ElasticSearchController {

    @Autowired
    private ESDao er;

    //增加
    @RequestMapping("/add/{id}")
    public String add(@PathVariable("id")String id){

        Elastic employee=new Elastic();
        employee.setId(id);
        employee.setName("Y.S.K");
        employee.setContent("ooo");
        employee.setPrice("26");
        er.save(employee);

        System.err.println("add a obj");
return "success";
    }

    //删除
    @RequestMapping("/delete")
    public String delete(){
        Elastic employee=new Elastic();
        employee.setId("1");
        er.delete(employee);

        return "success";
    }

    //局部更新
    @RequestMapping("/update")
    public String update(){

        Elastic employee=er.queryEmployeeById("1");
        employee.setName("哈哈");
        er.save(employee);

        System.err.println("update a obj");

        return "success";
    }

    //查询
    @RequestMapping("/query/{id}")
    public Elastic query(@PathVariable("id")String id){

        Elastic accountInfo=er.queryEmployeeById(id);
        System.err.println(new Gson().toJson(accountInfo));

        return accountInfo;
    }

}


可输入http://localhost:8080/demo/es/add/1测试,出现
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值