沈阳网站建设沈阳,安庆商城网站开发,韩国seocaso,枣庄高端网站定制环境搭建 Elasticsearch是搜索引擎#xff0c;是常见的搜索工具之一。 Kibana 是一个开源的分析和可视化平台#xff0c;旨在与 Elasticsearch 合作。Kibana 提供搜索、查看和与存储在 Elasticsearch 索引中的数据进行交互的功能。开发者或运维人员可以轻松地执行高级数据分析…环境搭建 Elasticsearch是搜索引擎是常见的搜索工具之一。 Kibana 是一个开源的分析和可视化平台旨在与 Elasticsearch 合作。Kibana 提供搜索、查看和与存储在 Elasticsearch 索引中的数据进行交互的功能。开发者或运维人员可以轻松地执行高级数据分析并在各种图表、表格和地图中可视化数据。 其它可视化还有elasticsearch-head(轻量级有对应的Chrome插件)本文不会详细介绍。 Elasticsearch和Kibana的版本采用7.17.0环境搭建采用Dockerdocker-compose.yml文件如下
version: 3.1
# 服务配置
services:elasticsearch:container_name: elasticsearch-7.17.0image: elasticsearch:7.17.0environment:- ES_JAVA_OPTS-Xms1024m -Xmx1024m- http.host0.0.0.0- node.nameelastic01- cluster.namecluster_elasticsearch- discovery.typesingle-nodeports:- 9200:9200- 9300:9300volumes:- ./es/plugins:/usr/share/elasticsearch/plugins- ./es/data:/usr/share/elasticsearch/datanetworks:- elastic_netkibana:container_name: kibana-7.17.0image: kibana:7.17.0ports:- 5601:5601networks:- elastic_net# 网络配置
networks:elastic_net:driver: bridge基础命令
查看ElasticSearch是否启动成功
curl http://IP:9200查看集群是否健康
curl http://IP:9200/_cat/health?v查看ElasticSearch所有的index
curl http://IP:9200/_cat/indices查看ElasticSearch所有indices或者某个index的文档数量
curl http://IP:9200/_cat/count?v
curl http://IP:9200/_cat/count/some_index_name?v查看每个节点正在运行的插件信息
curl http://IP:9200/_cat/plugins?vscomponenthname,component,version,description查看ik插件的分词结果
curl -H Content-Type: application/json -XGET http://IP:9200/_analyze?pretty -d {analyzer:ik_max_word,text:美国留给伊拉克的是个烂摊子吗}index操作
查看某个index的mapping
curl http://IP:9200/some_index_name/_mapping查看某个index的所有数据
curl http://IP:9200/some_index_name/_search按ID进行查询
curl -X GET http://IP:9200/索引名称/文档类型/ID检索某个index的全部数据
curl http://IP:9200/索引名称/_search?pretty
curl -X POST http://IP:9200/索引名称/_search?pretty -d {\query\: {\match_all\: {} }}检索某个index的前几条数据(如果不指定size,则默认为10条)
curl -XPOST IP:9200/索引名称/_search?pretty -d {\query\: {\match_all\: {} }, \size\ : 2}检索某个index的中间几条数据(比如第11-20条数据)
curl -XPOST IP:9200/索引名称/_search?pretty -d {\query\: {\match_all\: {} }, \from\ : 10, \size\ : 10}}检索某个index, 只返回context字段
curl -XPOST IP:9200/索引名称/_search?pretty -d {\query\: {\match_all\: {} }, \_source\: [\context\]}删除某个index
curl -XDELETE IP:9200/index_nameES搜索
如果有多个搜索关键字 Elastic 认为它们是or关系。如果要执行多个关键词的and搜索必须使用布尔查询。
$ curl localhost:9200/索引名称/文档类型/_search -d
{query: {bool: {must: [{ match: { content: 软件 } },{ match: { content: 系统 } }]}}
}复杂搜索
SQL语句
select * from test_index where nametom 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分词器 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的主词典中对所有索引生效。
?xml version1.0 encodingUTF-8?
!DOCTYPE properties SYSTEM http://java.sun.com/dtd/properties.dtd
propertiescommentIK Analyzer 扩展配置/comment!--用户可以在这里配置自己的扩展字典 --entry keyext_dictcustom/mydict.dic/entry!--用户可以在这里配置自己的扩展停止词字典--entry keyext_stopwordscustom/ext_stopword.dic/entry!--用户可以在这里配置远程扩展字典 --!-- entry keyremote_ext_dictwords_location/entry --!--用户可以在这里配置远程扩展停止词字典--!-- entry keyremote_ext_stopwordswords_location/entry --
/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 .