ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTfulweb接口。ElasticSearch是用Java开发的,并做为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,可以达到实时搜索,稳定,可靠,快速,安装使用方便。构建在全文检索开源软件Lucene之上的Elasticsearch,不只能对海量规模的数据完成分布式索引与检索,还能提供数据聚合分析。据国际权威的数据库产品评测机构DBEngines的统计,在2016年1月,Elasticsearch已超过Solr等,成为排名第一的搜索引擎类应用
归纳:基于Restful标准的高扩展高可用的实时数据分析的全文搜索工具java
ElasticSearch的基本概念
Indexmysql
相似于mysql数据库中的databaselinux
Typegit
相似于mysql数据库中的table表,es中能够在Index中创建type(table),经过mapping进行映射。github
Documentweb
因为es存储的数据是文档型的,一条数据对应一篇文档即至关于mysql数据库中的一行数据row,一个文档中能够有多个字段也就是mysql数据库一行能够有多列。
Field
es中一个文档中对应的多个列与mysql数据库中每一列对应
sql
Mapping数据库
能够理解为mysql或者solr中对应的schema,只不过有些时候es中的mapping增长了动态识别功能,感受很强大的样子,其实实际生产环境上不建议使用,最好仍是开始制定好了对应的schema为主。json
indexedvim
就是名义上的创建索引。mysql中通常会对常常使用的列增长相应的索引用于提升查询速度,而在es中默认都是会加上索引的,除非你特殊制定不创建索引只是进行存储用于展现,这个须要看你具体的需求和业务进行设定了。
Query DSL
相似于mysql的sql语句,只不过在es中是使用的json格式的查询语句,专业术语就叫:QueryDSL
GET/PUT/POST/DELETE
分别相似与mysql中的select/update/delete…
win下安装ElasticSearch
前提:已经安装java-jdk环境(必须)
下载地址:https://www.elastic.co/cn/downloads/elasticsearch
解压以后进入bin 启动 elasticSearch 脚本
linux下安装ElasticSearch
下载地址:https://www.elastic.co/cn/downloads/elasticsearch
解压以后进入es根目录/config 配置 network.host: 0.0.0.0
会报错:
解决方式:
bin/elasticsearch -Des.insecure.allow.root=true
或者修改bin/elasticsearch,加上ES_JAVA_OPTS属性:
ES_JAVA_OPTS="-Des.insecure.allow.root=true"
再次启动:
这是出于系统安全考虑设置的条件。因为ElasticSearch能够接收用户输入的脚本而且执行,为了系统安全考 虑,建议建立一个单独的用户用来运行ElasticSearch。
建立用户组和用户:
groupadd esgroup
useradd esuser -g esgroup -p espassword
更改elasticsearch文件夹及内部文件的所属用户及组:
cd /opt
chown -R esuser:esgroup elasticsearch-6.2.4
切换用户并运行:
su esuser
而后进入./bin下 使用./elasticSearch
脚本启动服务,
后台启动:./elasticSearch -d
若是你是win的话直接整个win的本地玩一下就行,mac也同样,不必搞个远程hou麻烦的
安装Kibana
Kibana是一个针对Elasticsearch的开源分析及可视化平台,使用Kibana能够查询、查看并与存储在ES索引的数据进行交互操做,使用Kibana能执行高级的数据分析,并能以图表、表格和地图的形式查看数据
(1)下载Kibana
https://www.elastic.co/downloads/kibana 注意:要和你的es版本一致
(2)把下载好的压缩包拷贝到/soft目录下
(3)解压缩,并把解压后的目录移动到/user/local/kibana
(4)编辑kibana配置文件
[root@localhost /]# vim /usr/local/kibana/config/kibana.yml
将server.host,elasticsearch.url修改为所在服务器的ip地址
(5)开启5601端口
Kibana的默认端口是5601
开启防火墙:systemctl start firewalld.service
开启5601端口:firewall-cmd --permanent --zone=public --add-port=5601/tcp
重启防火墙:firewall-cmd –reload
(6)启动Kibana
[root@localhost /]# /usr/local/kibana/bin/kibana
浏览器访问:http://ip:5601
在这里写dsl:
点这个执行:
安装中文分词器
(1)下载中文分词器
https://github.com/medcl/elasticsearch-analysis-ik
下载elasticsearch-analysis-ik-master.zip
(2)解压elasticsearch-analysis-ik-master.zip
unzip elasticsearch-analysis-ik-master.zip
(3)进入elasticsearch-analysis-ik-master,编译源码
mvn clean install -Dmaven.test.skip=true
(4)在es的plugins文件夹下建立目录ik
(5)将编译后生成的elasticsearch-analysis-ik-版本.zip移动到ik下,并解压
(6)解压后的内容移动到ik目录下
基本DSL:
重复一遍:相似于mysql的sql语句,只不过在es中是使用的json格式的查询语句,专业术语就叫:QueryDSL
GET/PUT/POST/DELETE
分别相似与mysql中的select/update/delete…
下面正式开始:
#建立索引 PUT /test { "settings": { "number_of_shards": 3, "number_of_replicas": 0 }, "mappings": { "user":{ "properties":{ "name":{ "type":"text" }, "address":{ "type":"text" }, "age":{ "type":"integer" }, "interests":{ "type":"text" }, "birthday":{ "type":"date" } } } } } #插入数据 PUT /test/user/1 { "name":"张三", "address":"朱雀大街", "age":2, "interests":"吃饭,睡觉,打豆豆", "birthday":"2001-09-08" } PUT /test/user/2 { "name":"李四", "address":"北京市", "age":16, "interests":"吃饭,踢球,打游戏", "birthday":"2011-09-08" } PUT /test/user/3 { "name":"王五", "address":"南京市", "age":12, "interests":"敲代码,捉迷藏,打游戏", "birthday":"2021-09-08" } PUT /test/user/4 { "name":"赵六", "address":"山东省", "age":133, "interests":"敲代码,学习,看电视", "birthday":"2031-09-08" } PUT /test/user/5 { "name":"张三丰", "address":"青岛市", "age":99, "interests":"打太极,练内功,教徒弟", "birthday":"2099-09-08" } PUT /test/user/6 { "name":"李白", "address":"西安市", "age":32, "interests":"舞剑,写诗,喝酒", "birthday":"1529-09-08" } PUT /test/user/7 { "name":"过滤空测试", "address":"", "age":0, "interests":"", "birthday":"9999-12-31" } # 删除索引 易碎勿碰 DELETE test #查询全部 GET test/user/_search #term:查询某个字段里含有某个关键词的文档 GET test/user/_search { "query": { "term": { "name": "李" } } } # match查询(重要) #match query知道分词器的存在,会对filed进行分词操做,而后再查询 GET test/user/_search { "query": { "match": { "name": "赵" } } } #match_all:查询全部文档 GET test/user/_search { "query": { "match_all": {} } } #multi_match:能够指定多个字段 GET test/user/_search { "query": { "multi_match": { "query": "打豆豆", "fields": ["name","interests"] } } } #match_phrase:短语匹配查询ElasticSearch引擎首先分析(analyze)查询字符串, #从分析后的文本中构建短语查询,这意味着必须匹配短语中的全部分词, #而且保证各个分词的相对位置不变 GET test/user/_search { "query": { "match_phrase": { "interests": "吃饭,睡觉" } } } # _source 返回指定字段 GET test/user/_search { "_source": ["name","interests"], "query": { "multi_match": { "query": "李", "fields": ["name","interests"] } } } #排序 desc:降序,asc升序 GET test/user/_search { "query": { "match": { "name":"李" } }, "sort": [ { "birthday": { "order": "asc" } } ] } #前缀匹配查询 GET test/user/_search { "query": { "match_phrase_prefix": { "name": "张" } } } # 范围查询 range:实现范围查询 # 默认包头包尾 经过参数设置为false表示关闭,默认true GET test/user/_search { "query": { "range": { "birthday": { "from": "2011-09-08", "to": "2021-09-08", "include_lower":true, "include_upper":true } } } } GET test/user/_search { "query": { "range": { "age": { "gte": 10, "lte": 20 } } } } # fuzzy实现模糊查询 GET test/user/_search { "query": { "fuzzy": { "name": "赵" } } } #高亮搜索结果(重要) GET test/user/_search { "query": { "fuzzy": { "name": "赵" } }, "highlight": { "fields": { "name": {} } } } #filter是不计算相关性的,同时能够cache。所以,filter速度要快于query。(重要) GET test/user/_search { "query": { "bool": { "filter": { "term": { "name": "李" } } } } } # bool过滤查询(重要) # 能够实现组合过滤查询 # 格式: # { # "bool": { # "must": [], # "should": [], # "must_not": [] # } # } # must :必须知足的条件---and # should:能够知足也能够不知足的条件--or # must_not:不须要知足的条件--not GET test/user/_search { "query": { "bool": { "must": [ {"term": {"age":133}}, {"term": {"birthday" : "2031-09-08"}} ], "must_not": [ {"term": {"_id" : "1"}} ] } } } GET test/user/_search { "query": { "bool": { "should": [ {"term": {"_id" : "1"}}, {"term": {"_id" : "2"}} ] } } } # 范围过滤 # gt: > # lt: < # gte: >= # lte: <= GET test/user/_search { "query": { "bool": { "filter": { "range": { "age": { "gte": 90, "lte": 300 } } } } } } # 过滤非空 exists GET test/user/_search { "query": { "bool": { "filter": { "exists": { "field": "address" } } } } } # 聚合查询 aggs (重要) #sum 求和 #好比:求年龄的总和 GET test/user/_search { "aggs": { "ages_sum": { "sum": {"field": "age"} } } } #ages_sum是自定义的返回结果的名称 #返回结果: # 上面是文档的数据看底部的aggregations, # "aggregations" : { # "ages_sum" : { # "value" : 294.0 # } # } #min最小值 年龄最小 size:长度 GET test/user/_search { "size": 0, "aggs": { "min_age": { "min": { "field": "age" } } } } #max 最大值 GET test/user/_search { "size": 0, "aggs": { "max_age": { "max": { "field": "age" } } } } #avg 平均值 GET test/user/_search { "size": 0, "aggs": { "avg_age": { "avg": { "field": "age" } } } } #cardinality 求基数 #什么是基数? 互不相同的数的个数 GET test/user/_search { "size": 0, "aggs": { "card_ages": { "cardinality": { "field": "age" } } } } #返回结果: # "aggregations" : { # "card_ages" : { # "value" : 7 # } # } # 分组 terms GET test/user/_search { "size": 0, "aggs": { "group_ages": { "terms": { "field": "age" } } } } #返回结果: #"aggregations" : { # "group_ages" : { # "doc_count_error_upper_bound" : 0, # "sum_other_doc_count" : 0, # "buckets" : [ # { # "key" : 0, # "doc_count" : 1 # }, # { # "key" : 2, # "doc_count" : 1 # }, # { # "key" : 12, # "doc_count" : 1 # }, # { # "key" : 16, # "doc_count" : 1 # }, # { # "key" : 32, # "doc_count" : 1 # }, # { # "key" : 99, # "doc_count" : 1 # }, # { # "key" : 133, # "doc_count" : 1 # } # ] # } # } #来个好玩的:将行李的user按年龄分组 GET test/user/_search { "size": 0, "query": { "match": { "name": "李" } }, "aggs": { "li_ages": { "terms": { "field": "age" } } } } # 结果: #"aggregations" : { # "li_ages" : { # "doc_count_error_upper_bound" : 0, # "sum_other_doc_count" : 0, # "buckets" : [ # { # "key" : 16, # "doc_count" : 1 # }, # { # "key" : 32, # "doc_count" : 1 # } # ] # } # } #再来个好玩的:求每组分的平均年龄,看我操做 GET test/user/_search { "size": 0, "query": { "match": { "name": "李" } }, "aggs": { "li_ages": { "terms": { "field": "age" }, "aggs": { "li_avg_age": { "avg": { "field": "age" } } } } } } #结果: # "aggregations" : { # "li_ages" : { # "doc_count_error_upper_bound" : 0, # "sum_other_doc_count" : 0, # "buckets" : [ # { # "key" : 16, # "doc_count" : 1, # "li_avg_age" : { # "value" : 16.0 # } # }, # { # "key" : 32, # "doc_count" : 1, # "li_avg_age" : { # "value" : 32.0 # } # } # ] # } # } #再再来个好玩的:求每组分的平均年龄,而后我要排个序 GET test/user/_search { "size": 0, "query": { "match": { "name": "李" } }, "aggs": { "li_ages": { "terms": { "field": "age", "order": { "li_avg_age": "desc" } }, "aggs": { "li_avg_age": { "avg": { "field": "age" } } } } } } #结果: #"aggregations" : { # "li_ages" : { # "doc_count_error_upper_bound" : 0, # "sum_other_doc_count" : 0, # "buckets" : [ # { # "key" : 32, # "doc_count" : 1, # "li_avg_age" : { # "value" : 32.0 # } # }, # { # "key" : 16, # "doc_count" : 1, # "li_avg_age" : { # "value" : 16.0 # } # } # ] # } # }
java操做有空写。 有用记得点在关注,感谢!