Elasticsearch是搜索引擎,是常见的搜索工具之一。
Kibana 是一个开源的分析和可视化平台,旨在与 Elasticsearch 合作。Kibana 提供搜索、查看和与存储在 Elasticsearch 索引中的数据进行交互的功能。开发者或运维人员可以轻松地执行高级数据分析,并在各种图表、表格和地图中可视化数据。
其它可视化还有elasticsearch-head(轻量级,有对应的Chrome插件),本文不会详细介绍。
Elasticsearch和Kibana的版本采用7.17.0,环境搭建采用Docker,docker-compose.yml
文件如下:
version: "3.1"
# 服务配置
services:
elasticsearch:
container_name: elasticsearch-7.17.0
image: elasticsearch:7.17.0
environment:
- "ES_JAVA_OPTS=-Xms1024m -Xmx1024m"
- "http.host=0.0.0.0"
- "node.name=elastic01"
- "cluster.name=cluster_elasticsearch"
- "discovery.type=single-node"
ports:
- "9200:9200"
- "9300:9300"
volumes:
- ./es/plugins:/usr/share/elasticsearch/plugins
- ./es/data:/usr/share/elasticsearch/data
networks:
- elastic_net
kibana:
container_name: kibana-7.17.0
image: kibana:7.17.0
ports:
- "5601:5601"
networks:
- elastic_net
# 网络配置
networks:
elastic_net:
driver: bridge
curl http://IP:9200
curl http://IP:9200/_cat/health?v
curl http://IP:9200/_cat/indices
curl http://IP:9200/_cat/count?v
curl http://IP:9200/_cat/count/some_index_name?v
curl http://IP:9200/_cat/plugins?v&s=component&h=name,component,version,description
curl -H 'Content-Type: application/json' -XGET 'http://IP:9200/_analyze?pretty' -d '{"analyzer":"ik_max_word","text":"美国留给伊拉克的是个烂摊子吗"}'
curl http://IP:9200/some_index_name/_mapping
curl http://IP:9200/some_index_name/_search
curl -X GET http://IP:9200/索引名称/文档类型/ID
curl http://IP:9200/索引名称/_search?pretty
curl -X POST http://IP:9200/索引名称/_search?pretty -d "{\"query\": {\"match_all\": {} }}"
curl -XPOST IP:9200/索引名称/_search?pretty -d "{\"query\": {\"match_all\": {} }, \"size\" : 2}"
curl -XPOST IP:9200/索引名称/_search?pretty -d "{\"query\": {\"match_all\": {} }, \"from\" : 10, \"size\" : 10}}"
curl -XPOST IP:9200/索引名称/_search?pretty -d "{\"query\": {\"match_all\": {} }, \"_source\": [\"context\"]}"
curl -XDELETE 'IP:9200/index_name'
$ curl 'localhost:9200/索引名称/文档类型/_search' -d '
{
"query": {
"bool": {
"must": [
{ "match": { "content": "软件" } },
{ "match": { "content": "系统" } }
]
}
}
}'
SQL语句:
select * from test_index where name='tom' or (hired =true and (personality ='good' and rude != true ))
DSL语句:
GET /test_index/_search
{
"query": {
"bool": {
"must": { "match":{ "name": "tom" }},
"should": [
{ "match":{ "hired": true }},
{ "bool": {
"must":{ "match": { "personality": "good" }},
"must_not": { "match": { "rude": true }}
}}
],
"minimum_should_match": 1
}
}
}
ik分词器是Elasticsearch的中文分词器插件,对中文分词支持较好。ik版本要与Elasticsearch保持一致。
ik 7.17.0下载地址为:https://github.com/medcl/elasticsearch-analysis-ik/releases/tag/v7.17.0 ,下载后将其重名为ik,将其放至Elasticsearch的plugins文件夹下。
ik分词器的使用命令(Kibana环境):
POST _analyze
{
"text": "戚发轫是哪里人",
"analyzer": "ik_smart"
}
输出结果为:
{
"tokens" : [
{
"token" : "戚",
"start_offset" : 0,
"end_offset" : 1,
"type" : "CN_CHAR",
"position" : 0
},
{
"token" : "发轫",
"start_offset" : 1,
"end_offset" : 3,
"type" : "CN_WORD",
"position" : 1
},
{
"token" : "是",
"start_offset" : 3,
"end_offset" : 4,
"type" : "CN_CHAR",
"position" : 2
},
{
"token" : "哪里人",
"start_offset" : 4,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 3
}
]
}
ik支持加载用户词典和停用词。ik 提供了配置文件 IKAnalyzer.cfg.xml(将其放在ik/config路径下),可以用来配置自己的扩展用户词典、停用词词典和远程扩展用户词典,都可以配置多个。
配置完扩展用户词典和远程扩展用户词典都需要重启ES,后续对用户词典进行更新的话,需要重启ES,远程扩展用户词典配置完后支持热更新,每60秒检查更新。两个扩展词典都是添加到ik的主词典中,对所有索引生效。
DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>IK Analyzer 扩展配置comment>
<entry key="ext_dict">custom/mydict.dicentry>
<entry key="ext_stopwords">custom/ext_stopword.dicentry>
properties>
用户词典文件路径为:custom/mydict.dic,停用词词典路径为:custom/ext_stopword.dic,将它们放在ik/config/custom路径下。
用户词典文件中加入’戚发轫’,停用词词典加入’是’,对原来文本进行分词:
POST _analyze
{
"text": "戚发轫是哪里人",
"analyzer": "ik_smart"
}
输出结果如下:
{
"tokens" : [
{
"token" : "戚发轫",
"start_offset" : 0,
"end_offset" : 3,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "哪里人",
"start_offset" : 4,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 1
}
]
}
如果’analyzer’选择ik_smart,则会将文本做最粗粒度的拆分;选择ik_max_word,则会将文本做最细粒度的拆分。测试如下:
POST _analyze
{
"text": "戚发轫是哪里人",
"analyzer": "ik_max_word"
}
输出结果如下:
{
"tokens" : [
{
"token" : "戚发轫",
"start_offset" : 0,
"end_offset" : 3,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "发轫",
"start_offset" : 1,
"end_offset" : 3,
"type" : "CN_WORD",
"position" : 1
},
{
"token" : "哪里人",
"start_offset" : 4,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 2
},
{
"token" : "哪里",
"start_offset" : 4,
"end_offset" : 6,
"type" : "CN_WORD",
"position" : 3
},
{
"token" : "里人",
"start_offset" : 5,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 4
}
]
}
本文主要介绍了Elasticsearch一些基础命令和用法,是笔者的Elasticsearch学习笔记第一篇,后续将持续更新。
本文代码已放至Github,网址为:https://github.com/percent4/ES_Learning .