Elasticsearch的使用
一 . Elasticsearch的安装
从Windows访问Linux的ES
/opt/soft/elasticsearch-7.15.1/bin
elasticsearch.yml
network.host: 0.0.0.0 http.port: 9200 transport.host: localhost transport.tcp.port: 9300
配置禁用安全选项
xpack.security.enabled: false
从Windows访问Linux的Kibana
/opt/soft/kibana-7.15.1/bin
kibana.yml
server.host: “local host” 改为 server.host: “0.0.0.0”
二.基本CRUD
三. URL查询
四.数据库数据导入到ES
如何让别人的电脑链接到我的数据库呢 我们只要做三个步骤就可以了
首先:打开命令行
在其中输入 mysql -uroot -p 点击回车
第二步: 输入自己的 密码
第三步: 输入 grant all privileges on . to root@’%’ identified by ‘自己密码’;
GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%'IDENTIFIED BY 'mypassword' WITH GRANT OPTION;
数据库数据导入ES
新建logstash-mysql-products.conf
input {
jdbc {
# jdbc驱动包的位置
jdbc_driver_library => "/opt/soft/mysql-connector-java-8.0.25.jar"
# 要使用的驱动包
jdbc_driver_class => "com.mysql.cj.jdbc.Driver"
# mysql数据库的连接信息
jdbc_connection_string => "jdbc:mysql://192.168.186.209:3306/xiaomi?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true"
# mysql用户
jdbc_user => "root"
# mysql密码
jdbc_password => "root"
# 定时任务,多久查询一次,表示每一分钟同步一次
schedule => "* * * * *"
# 清空上次的sql_last_value记录
clean_run => true
jdbc_paging_enabled => "true"
jdbc_page_size => "20"
# 要执行的sql语句
statement => "select * from product where tags is not null"
}
}
filter {
mutate {
split => {"tags" => ","}
}
mutate {
remove_field => ["@timestamp","@version"]
}
}
output {
elasticsearch {
hosts => ["192.168.130.30:9200"]
index => "products"
document_id => "%{product_id}"
}
stdout {
codec => json_lines
}
}
新建index索引
PUT products
{
"settings": {
"analysis": {
"analyzer": {
"products_tags_analyzer": {
"char_filter": ["html_strip"],
"tokenizer": "keyword",
"filter": "products_tags_filter"
}
},
"filter": {
"products_tags_filter": {
"type": "pinyin",
"keep_full_pinyin": true,
"keep_joined_ful_pinyin": true,
"keep_original": true
}
}
}
},
"mappings": {
"properties": {
"product_id": {
"type": "long"
},
"product_number": {
"type": "text"
},
"product_name": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"price": {
"type": "float"
},
"introduce": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"pic_img": {
"type": "keyword"
},
"status": {
"type": "boolean"
},
"category_id": {
"type": "long"
},
"so": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"tags": {
"type": "completion",
"analyzer": "products_tags_analyzer",
"search_analyzer": "keyword"
}
}
}
}
运行bin/logstash -f ./logstash-mysql-products.conf
GET products/_search
{
"_source": false,
"suggest": {
"product_tags_suggest": {
"prefix":"s",
"completion":{
"field":"tags",
"size":10,
"skip_duplicates":true
}
}
}
}
修复bug版
PUT products
{
"settings": {
"analysis": {
"analyzer": {
"products_tags_analyzer": {
"char_filter": ["html_strip"],
"tokenizer": "keyword",
"filter": "products_tags_filter"
}
},
"filter": {
"products_tags_filter": {
"type": "pinyin",
"keep_full_pinyin": true,
"keep_joined_ful_pinyin": true,
"keep_original": true
}
}
}
},
"mappings": {
"properties": {
"product_id": {
"type": "long"
},
"product_number": {
"type": "text"
},
"product_name": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"price": {
"type": "float"
},
"introduce": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"pic_img": {
"type": "keyword"
},
"status": {
"type": "boolean"
},
"category_id": {
"type": "long"
},
"so": {
"type": "text"
},
"tags": {
"type": "completion",
"analyzer": "products_tags_analyzer",
"search_analyzer": "keyword",
"fields": {
"suggest":{
"type":"completion",
"analyzer":"ik_max_word"
}
}
}
}
}
}
GET products/_search
{
"_source": false,
"suggest": {
"product_tags_suggest": {
"prefix":"手",
"completion":{
"field":"tags.suggest",
"size":20,
"skip_duplicates":true
}
},
"product_tags_suggest1": {
"prefix":"x",
"completion":{
"field":"tags",
"size":20,
"skip_duplicates":true
}
}
}
}
GET products/_search
{
"_source": ["product_name","so"]
, "query": {
"multi_match": {
"query": "笔",
"fields": ["product_name","so"]
}
}
}