版本:7.8.0
下载好elasticsearch,logstash,kibana,ik分词器
https://www.elastic.co/cn/downloads/elasticsearch
https://www.elastic.co/cn/downloads/logstash
https://www.elastic.co/cn/downloads/kibana
https://github.com/medcl/elasticsearch-analysis-ik/releases
启动elasticsearch
解压后直接点击bin目录下elasticsearch.bat文件,在浏览器访问localhost:9200
使用curl命令操作es
创建索引
新增数据
查询数据
启动kibana
解压后直接点击bin目录下kibana.bat,在kibana.yml可以看到默认配置elasticsearch.host :http://localhost:9200
浏览器访问localhost:5601
在kibana上操作es
点击devtools
查询
删除索引
创建索引
新增数据
安装ik分词器
在es的plugins的文件夹下创建了一个ik文件夹
将ik分词器的压缩包解压后的所有文件放到ik文件夹下
然后 重启es
使用es默认的分词器查询
使用ik_smart分词器查询
使用ik_max_word分词器查询
使用logstash同步mysql数据到elasticsearch
1、解压后,在config文件夹下创建用来同步mysql的配置文件mysql.conf
配置文件内容
input {
jdbc {
jdbc_driver_library => "C:\\soft\\logstash-7.8.0\\mysql-connector-java-8.0.21.jar"
jdbc_connection_string => "jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC"
jdbc_driver_class => "com.mysql.cj.jdbc.Driver"
jdbc_user => "root"
jdbc_password => "root"
schedule => "* * * * * *"
clean_run => true
statement => "select * from blog where update_time>=:sql_last_value and update_time < now() order by update_time DESC;"
}
}
output {
elasticsearch {
hosts => ["127.0.0.1:9200"]
index => "blog"
document_id => "%{id}"
}
}
创建表
CREATE TABLE `blog` (
`id` int NOT NULL AUTO_INCREMENT,
`title` varchar(255) COLLATE utf8mb4_croatian_ci DEFAULT NULL,
`content` varchar(255) COLLATE utf8mb4_croatian_ci DEFAULT NULL,
`update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_croatian_ci;
启动logstash
bin/logstash -f ../config/mysql.conf
数据库插入一条数据,查看kibana
logstash重启遇到的问题
在终端,ctrl+c发现并不能退出logstash,但是直接关闭窗口,再启动的时候会提示已经启动了,不能再启动,这个时候把data文件夹下面的.lock删除就可以了
使用java客户端elasticsearch-rest-high-level-client操作es,实现全文检索
引入maven依赖
org.elasticsearch.client
elasticsearch-rest-high-level-client
7.8.0
org.elasticsearch.client
elasticsearch-rest-client
7.8.0
org.elasticsearch
elasticsearch
7.8.0
利用spring的便利,创建springbean注入到spring容器中
@Configuration
public class EsConfig {
@Bean
public RestHighLevelClient restHighLevelClient(){
HttpHost httpHost = new HttpHost("localhost", 9200, "http");
RestClientBuilder builder = RestClient.builder(httpHost);
return new RestHighLevelClient(builder);
}
}
Blog.java
@Data
public class Blog {
private Integer id;
private String title;
private String content;
}
EsBlogManager.java
@Service
public class EsBlogManager {
@Autowired
private RestHighLevelClient restHighLevelClient;
public List searchByKeyWord(String keyWord){
List biogs = new ArrayList<>();
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.should(QueryBuilders.matchPhraseQuery("title",keyWord));
boolQueryBuilder.should(QueryBuilders.matchPhraseQuery("content",keyWord));
sourceBuilder.query(boolQueryBuilder);
try {
SearchResponse search = restHighLevelClient.search(searchRequest(sourceBuilder), RequestOptions.DEFAULT);
SearchHit[] hits = search.getHits().getHits();
for (SearchHit hit:hits) {
Map sourceAsMap = hit.getSourceAsMap();
String jsonString = JSON.toJSONString(sourceAsMap);
Blog blog = JSON.parseObject(jsonString,Blog.class);
biogs.add(blog);
}
} catch (IOException e) {
e.printStackTrace();
}
return biogs;
}
private SearchRequest searchRequest(SearchSourceBuilder sourceBuilder){
SearchRequest searchRequest = new SearchRequest("blog");
searchRequest.source(sourceBuilder);
return searchRequest;
}
}