tensquare搜索微服务
搜索微服务创建Module(省略)
准备工作
引入依赖
<?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">
<parent>
<artifactId>tensquare_parent</artifactId>
<groupId>com.tensquare</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>tensquare_search</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>com.tensquare</groupId>
<artifactId>tensquare_common</artifactId>
<version>${tensquare.version}</version>
</dependency>
</dependencies>
</project>
application.yml
server:
port: 9007
spring:
application:
name: tensquare-search
data:
elasticsearch:
cluster-nodes: 192.168.136.104:9300
启动类
package com.tensquare.search;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SearchApplication {
public static void main(String[] args) {
SpringApplication.run(SearchApplication.class, args);
}
}
实体类
之前也有一个文章实体类,是Mysql
在使用。
这个文章实体类,是ES
所使用的实体类。
package com.tensquare.search.pojo;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import java.io.Serializable;
/**
* 文章实体类
*/
@Document(indexName="tensquare",type="article")
public class Article implements Serializable {
@Id
private String id;//ID
@Field(index= true,analyzer="ik_max_word",searchAnalyzer="ik_max_word")
private String title;//标题
@Field(index= true,analyzer="ik_max_word",searchAnalyzer="ik_max_word")
private String content;//文章正文
private String state;//审核状态
}
添加文章到ES
ArticleSearchController
package com.tensquare.search.controller;
import com.tensquare.search.pojo.Article;
import com.tensquare.search.service.ArticleSearchService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@CrossOrigin
@RequestMapping("/article")
public class ArticleSearchController {
@Autowired
private ArticleSearchService articleSearchService;
@RequestMapping(method = RequestMethod.POST)
public Result save(@RequestBody Article article) {
articleSearchService.save(article);
return new Result(true, StatusCode.OK, "操作成功");
}
}
ArticleSearchService
package com.tensquare.search.service;
import com.tensquare.search.dao.ArticleSearchDao;
import com.tensquare.search.pojo.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ArticleSearchService {
@Autowired
private ArticleSearchDao articleSearchDao;
/**
* 增加文章
*
* @param article
*/
public void save(Article article) {
articleSearchDao.save(article);
}
}
ArticleSearchDao
package com.tensquare.search.dao;
import com.tensquare.search.pojo.Article;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface ArticleSearchDao extends ElasticsearchRepository<Article,String> {
}
文章搜索
ArticleSearchController新增方法
@RequestMapping(value = "/search/{keywords}/{page}/{size}", method = RequestMethod.GET)
public Result findByTitleLike(@PathVariable String keywords,
@PathVariable int page, @PathVariable int size) {
Page<Article> articlePage = articleSearchService.findByTitleLike(keywords, page, size);
return Result.ok( "查询成功", new PageResult<>(articlePage.getTotalElements(), articlePage.getContent()));
}
ArticleSearchService新增方法
/**
* 按名称查询文章
*
* @param keywords
* @param page
* @param size
* @return
*/
public Page<Article> findByTitleLike(String keywords, int page, int size) {
PageRequest pageRequest = PageRequest.of(page - 1, size);
return articleSearchDao.findByTitleOrContentLike(keywords, keywords, pageRequest);
}
ArticleSearchDao
Page<Article> findByTitleOrContentLike(String title, String content, Pageable pageable);
同步文章数据到ES
安装logstash(省略)
配置logstash
使用logstash
导入数据到ES
,任务配置为:
input {
jdbc {
# 数据库连接地址
jdbc_connection_string => "jdbc:mysql://192.168.136.104:3306/tensquare_article?characterEncoding=UTF8"
# 用户名密码
jdbc_user => "root"
jdbc_password => "123456"
# 数据库驱动包路径
jdbc_driver_library => "D:/logstash‐5.6.8/mysqletc/mysql-connector-java-5.1.46.jar"
# driver class
jdbc_driver_class => "com.mysql.jdbc.Driver"
jdbc_paging_enabled => "true"
jdbc_page_size => "50000"
# 要执行的sql,
statement => "select id,title,content from tb_article"
# 定时字段 各字段含义(由左至右)分、时、天、月、年,类似cron表达式
schedule => "* * * * *"
}
}
output {
elasticsearch {
# ESIP地址与端口
hosts => "192.168.136.104:9200"
# ES索引名称(自己定义的)
index => "tensquare"
# 自增ID编号
document_id => "%{id}"
document_type => "article"
}
stdout {
# 以JSON格式输出
codec => json_lines
}
}
运行
# 将配置放入logstash.conf,然后执行logstash -f命令并指定执行的文件为logstash.conf
logstash -f logstash.conf