Springboot + ElasticSearch 构建博客检索系统

ElasticSearch 软件安装见 https://blog.csdn.net/weixin_43758633/article/details/111397877

第1章 演示 postman、kibana对ES的交互

postman 操作

Get 查看所有索引

localhost:9200/_all

PUT 创建索引-test

localhost:9200/test  


DEL 删除索引-test

localhost:9200/test  


PUT 创建索引-person-1

localhost:9200/person


PUT 新增数据-person-1

localhost:9200/person/_doc/1

{
	"first_name" : "John",
  "last_name" : "Smith",
  "age" : 25,
  "about" : "I love to go rock climbing",
  "interests" : [ "sports", "music" ]
}

PUT 新增数据-person-2

localhost:9200/person/_doc/2

{
	"first_name" : "Eric",
  "last_name" : "Smith",
  "age" : 23,
  "about" : "I love basketball",
  "interests" : [ "sports", "reading" ]
}

GET 搜索数据-person-id

localhost:9200/person/_doc/1

GET 搜索数据-person-name

localhost:9200/person/_doc/_search?q=first_name:john

{
  "took": 56,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 0.6931472,
    "hits": [
      {
        "_index": "person",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.6931472,
        "_source": {
          "first_name": "John",
          "last_name": "Smith",
          "age": 25,
          "about": "I love to go rock climbing",
          "interests": [
            "sports",
            "music"
          ]
        }
      }
    ]
  }
}

Kibana

GET /person/_doc/1

POST /person/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "first_name": "Eric"
          }
        }
      ]
    }
  }
}

//should
POST /person/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "last_name": "Smith"
          }
        },
        {
          "match": {
            "about": "game"
          }
        }
      ]
    }
  }
}
//结果为两个

//改为must
POST /person/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "last_name": "Smith"
          }
        },
        {
          "match": {
            "about": "game"
          }
        }
      ]
    }
  }
}
//结果为1个

第2章 博客网站全文检索

3-1 基于 Mysql 实现

在这里插入图片描述

CREATE DATABASE blog;

USE blog;

CREATE TABLE `t_blog` (    
   `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增id',    
   `title` varchar(60) DEFAULT NULL COMMENT '博客标题',    
   `author` varchar(60) DEFAULT NULL COMMENT '博客作者',    
   `content` mediumtext COMMENT '博客内容',    
   `create_time` datetime DEFAULT NULL COMMENT '创建时间',    
   `update_time` datetime DEFAULT NULL COMMENT '更新时间',    
   PRIMARY KEY (`id`)    
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4


# 自己造的几条数据
INSERT INTO `blog`.`t_blog`(`id`, `title`, `author`, `content`, `create_time`, `update_time`) VALUES (1, 'Springboot 为什么这', 'bywind', '没错 Springboot ', '2019-12-08 01:44:29', '2019-12-08 01:44:34');
INSERT INTO `blog`.`t_blog`(`id`, `title`, `author`, `content`, `create_time`, `update_time`) VALUES (3, 'Springboot 中 Redis', 'bywind', 'Spring Boot', '2019-12-08 01:44:29', '2019-12-08 01:44:29');
INSERT INTO `blog`.`t_blog`(`id`, `title`, `author`, `content`, `create_time`, `update_time`) VALUES (4, 'Springboot 中如何优化', 'bywind', NULL, '2019-12-08 01:44:29', '2019-12-08 01:44:29');
INSERT INTO `blog`.`t_blog`(`id`, `title`, `author`, `content`, `create_time`, `update_time`) VALUES (5, 'Springboot 消息队列', 'bywind', NULL, '2019-12-08 01:44:29', '2019-12-08 01:44:29');
INSERT INTO `blog`.`t_blog`(`id`, `title`, `author`, `content`, `create_time`, `update_time`) VALUES (6, 'Docker Compose + Springboot', 'bywind', NULL, '2019-12-08 01:44:29', '2019-12-08 01:44:29');
SELECT * FROM t_blog WHERE title LIKE "%spring%" or content LIKE "%spring%"

在这里插入图片描述

3-2 基于ES实现

在这里插入图片描述
在这里插入图片描述

第4章 Mysql、ES 数据同步

4-1 数据同步中间件

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
不足:不支持 ES6.X 以上、Mysql 8.X 以上
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
time 标识最大时间

4-2 logstash全量、增量同步解决方案

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

jar 包下载地址
https://mvnrepository.com/artifact/mysql/mysql-connector-java/5.1.31

下载logstash并解压
在这里插入图片描述
编辑mysql配置文件
在这里插入图片描述

input{
    jdbc{
        # jdbc驱动包位置
        jdbc_driver_library => "D:\\JavaTools\\elasticsearch\\logstash-7.6.1\\mysql-connector-java-5.1.31.jar"
        # 要使用的驱动包类
        jdbc_driver_class => "com.mysql.jdbc.Driver"
        # mysql数据库的连接信息
        jdbc_connection_string => "jdbc:mysql://127.0.0.1:3306/blog"
        # mysql用户
        jdbc_user => "root"
        # mysql密码
        jdbc_password => "19950818"
        # 定时任务,多久执行一次查询,默认一分钟,如果想要没有延迟,可以使用 schedule => "* * * * * *"
        schedule => "* * * * *"
        # 清空上传的sql_last_value记录
        clean_run => true
        # 你要执行的语句
        statement => "select * FROM t_blog WHERE update_time >= :sql_last_value AND update_time < NOW() ORDER BY update_time desc"
    }
}

output {
    elasticsearch{
        # es host : port
        hosts => ["127.0.0.1:9200"]
        # 索引
        index => "blog"
        # _id
        document_id => "%{id}"
    }
}

运行
logstash -f ../config/mysql.conf
在这里插入图片描述
查看

GET /blog/_stats


POST /blog/_search
{
  
}

POST /blog/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match_phrase": {
            "title": "springboot"
          }
        },
        {
          "match_phrase": {
            "content": "springboot"
          }
        }
      ]
    }
  }
}

sql改进

  • v1:
    在这里插入图片描述
    会丢失临界点数据 R12和R22
  • v2:
    在这里插入图片描述
    会有重复数据,存在不必要的性能开销
  • v3:
    在这里插入图片描述

第 5 章

5-1 分词器介绍

在这里插入图片描述
下载ik分词器并解压到下面的路径
在这里插入图片描述
分词测试

POST _analyze
{
  "analyzer": "standard",
  "text" : "hello imooc"
}

{
  "tokens" : [
    {
      "token" : "hello",
      "start_offset" : 0,
      "end_offset" : 5,
      "type" : "<ALPHANUM>",
      "position" : 0
    },
    {
      "token" : "imooc",
      "start_offset" : 6,
      "end_offset" : 11,
      "type" : "<ALPHANUM>",
      "position" : 1
    }
  ]
}


POST _analyze
{
  "analyzer": "standard"
  , "text": "我是中国人"
}
{
  "tokens" : [
    {
      "token" : "我",
      "start_offset" : 0,
      "end_offset" : 1,
      "type" : "<IDEOGRAPHIC>",
      "position" : 0
    },
    {
      "token" : "是",
      "start_offset" : 1,
      "end_offset" : 2,
      "type" : "<IDEOGRAPHIC>",
      "position" : 1
    },
    {
      "token" : "中",
      "start_offset" : 2,
      "end_offset" : 3,
      "type" : "<IDEOGRAPHIC>",
      "position" : 2
    },
    {
      "token" : "国",
      "start_offset" : 3,
      "end_offset" : 4,
      "type" : "<IDEOGRAPHIC>",
      "position" : 3
    },
    {
      "token" : "人",
      "start_offset" : 4,
      "end_offset" : 5,
      "type" : "<IDEOGRAPHIC>",
      "position" : 4
    }
  ]
}
     

POST _analyze
{
  "analyzer": "ik_smart"
  , "text": "我是中国人"
}
{
  "tokens" : [
    {
      "token" : "我",
      "start_offset" : 0,
      "end_offset" : 1,
      "type" : "CN_CHAR",
      "position" : 0
    },
    {
      "token" : "是",
      "start_offset" : 1,
      "end_offset" : 2,
      "type" : "CN_CHAR",
      "position" : 1
    },
    {
      "token" : "中国人",
      "start_offset" : 2,
      "end_offset" : 5,
      "type" : "CN_WORD",
      "position" : 2
    }
  ]
}


POST _analyze
{
  "analyzer": "ik_max_word"
  , "text": "我是中国人"
}

{
  "tokens" : [
    {
      "token" : "我",
      "start_offset" : 0,
      "end_offset" : 1,
      "type" : "CN_CHAR",
      "position" : 0
    },
    {
      "token" : "是",
      "start_offset" : 1,
      "end_offset" : 2,
      "type" : "CN_CHAR",
      "position" : 1
    },
    {
      "token" : "中国人",
      "start_offset" : 2,
      "end_offset" : 5,
      "type" : "CN_WORD",
      "position" : 2
    },
    {
      "token" : "中国",
      "start_offset" : 2,
      "end_offset" : 4,
      "type" : "CN_WORD",
      "position" : 3
    },
    {
      "token" : "国人",
      "start_offset" : 3,
      "end_offset" : 5,
      "type" : "CN_WORD",
      "position" : 4
    }
  ]
}

可以自己添加字典
在这里插入图片描述

5-3 springboot 项目搭建

参考笔记地址:https://www.yuque.com/gaohanghang/vx5cb2/aa576g#wENL7
代码地址:https://github.com/gaohanghang/springboot-blog-es (转自上述笔记)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值