Springboot+Elasticsearch+IK分词器实现全文检索(2)
logstash实时同步数据到Elasticsearch中
logstash下载地址[\[https://blog.csdn.net/weixin_37281289/article/details/101483434\]](%5Bhttps://blog.csdn.net/weixin_37281289/article/details/101483434%5D)
官网下载地址[https://www.elastic.co/cn/downloads/elasticsearch](https://www.elastic.co/cn/downloads/elasticsearch)
下载完成后打开config文件,在config文件下新建mysql.conf文件用于与mysql数据库进行连接
内容如下
```java
input{
jdbc{
#jdbc驱动包位置
jdbc_driver_library =>"/Users/zhang/Downloads/elasticsearch/logstash-6.3.2/mysql-connector-java-8.0.16.jar"
#要使用的驱动包类,有过开发经验的都应该知道这个
jdbc_driver_class =>"com.mysql.jdbc.Driver"
#mysql数据库连接信息
jdbc_connection_string =>"jdbc:mysql://127.0.0.1:3306/abcd"
#mysql用户名
jdbc_user =>"root"
#mysql密码
jdbc_password =>"自己到密码"
#定时任务,多久执行一次查询,默认是一分钟,如何想要没有延迟,可以使用schedule =>"* * * * *"
schedule =>"* * * * *"
#清空上一次的sql_last_value记录
clean_run =>true
#你想要执行的语句
statement =>"SELECT * FROM t_abcd 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=>"abcd"
#_id
document_id=>"%{id}"
}
```
在logstash文件到根目录下引入mysql到驱动程序
启动命令bin目录下 输入logstash -f ../config/mysql.conf//此为mac电脑到命令
查看是否有数据
GET /abcd/_stats
查看是否有数据
POST /abcd/_search
{
}
下载Ik分词器用于分词
下载地址 [https://github.com/medcl/elasticsearch-analysis-ik](https://github.com/medcl/elasticsearch-analysis-ik)
[https://github.com/medcl/elasticsearch-analysis-ik/releases](https://github.com/medcl/elasticsearch-analysis-ik/releases)找到对应的版本和es版本一致
下载完成后需要去到es安装目录中的plugins目录中新建一个文件夹,将下载后的ik包解压后把里面的所有东西都复制到新建的文件夹中即可,之后需要重启es
```java
第一种方式
POST _analyze
{
"analyzer": "ik_smart",
"text": "我是中国人"
}
第二种将词分为更多的部分
POST _analyze
{
"analyzer": "ik_max_word",
"text": "我是中国人"
}
```
ik/config/main.dic中包括很多的词
springboot+Elasticsearch
主要:springboot的版本不要太高比如我用的是2.2.0的版本,版本太高有报错
在配置时需要先打开es文件中的elasticsearch.yml文件将my-application这句话的注解打开否则会报错
springboot application.properties配置
```java
#es配置
spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300
spring.data.elasticsearch.cluster-name=my-application
```
实体类配置pojo
```java
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.DateFormat;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import javax.persistence.Column;
import java.util.Date;
@Data
@Document(indexName = "blog",type = "doc",useServerConfiguration = true,createIndex = false)
public class ESBlog {
@Id
private Integer id;
//添加需要分词的字段用 @Field(type = FieldType.Text,analyzer = "ik_max_word")
@Field(type = FieldType.Text,analyzer = "ik_max_word")
private String title;
@Field(type = FieldType.Text,analyzer = "ik_max_word")
private String author;
@Field(type = FieldType.Text,analyzer = "ik_max_word")
private String content;
@Field(type = FieldType.Date,format = DateFormat.custom,pattern = "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis")
private Date createTime;
@Field(type = FieldType.Date,format = DateFormat.custom,pattern = "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis")
private Date updateTime;
}
```
DAO类
```java
public interface EsBlogDao extends ElasticsearchRepository<ESBlog,Integer> {
}
```
Controller类
```java
package com.example.springbootelasticsearch.BlogController;
import com.example.springbootelasticsearch.esdao.EsBlogDao;
import com.example.springbootelasticsearch.espojo.ESBlog;
import com.example.springbootelasticsearch.esservice.ESBlogservice;
import com.example.springbootelasticsearch.mysqlpojo.MysqlBlog;
import com.example.springbootelasticsearch.mysqlservice.MysqlService;
import com.example.springbootelasticsearch.pojvo.Blogvo;
import org.apache.lucene.queryparser.xml.builders.BooleanQueryBuilder;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
@RestController
@RequestMapping("/blog")
public class BlogController {
@Autowired
EsBlogDao esBlogDao;
@PostMapping("/search")
public Object search(@RequestBody Blogvo blogvo){
HashMap<String,Object> map=new HashMap<>();
StopWatch stopWatch=new StopWatch();
stopWatch.start();
String type=blogvo.getType();
BoolQueryBuilder builder= QueryBuilders.boolQuery();
//matchQuery先将搜索词进行分词然后再进行查询只要里面包含任意一个词都可以被查出
// builder.should(QueryBuilders.matchPhraseQuery("title",blogvo.getKeyword()));
// builder.should(QueryBuilders.matchPhraseQuery("content",blogvo.getKeyword()));
//matchPhraseQuery搜索词不进行分词,精准匹配搜索词,如何不一致则无法进行匹配
//相当于or
builder.should(QueryBuilders.matchPhraseQuery("title",blogvo.getKeyword()));
builder.should(QueryBuilders.matchPhraseQuery("content",blogvo.getKeyword()));
Page<ESBlog> search= (Page<ESBlog>) esBlogDao.search(builder);
List<ESBlog> content = search.getContent();
map.put("list",content);
stopWatch.stop();
long totalTimeMillis = stopWatch.getTotalTimeMillis();
map.put("duration",totalTimeMillis);
return map;
}
}
```
至此springboot配置完成启动即可访问
持续更新java知识。。。。