一、相关概念
(1)索引库:存储索引保存在磁盘上的一系列文件,里面存储了建立好的索引信息以及文档对象。(类似于关系型数据可中的数据库)
(2)documnet对象:document对象中包括一个一个的域,每一个域中存储内容,每一个document对象抖音一个唯一的编号,就是document对象的id。(类似于关系型数据可中的一行数据)
(3)field对象:field是索引库中存储数据的最小单位。如果我们把document对象看成关系型数据库的一条数据,那么field相当于数据库中的字段。field的数据类型大致可以分为数值类型和文本类型。field具有的属性:是否分词、是否存储、是否索引。(在创建mappings的时候需要注明)
(4)term对象:从⽂档对象中拆分出来的每个单词叫做⼀个Term,不同的域中拆分出来的相同的单词是不同的term。term中包含两部分⼀部分是⽂档的域名,另⼀部分是单词的内容。term是创建索引的关键词对象。
二、ElasticSearch相关概述
(1)ElasticSearch对比传统关系型数据库:
- Relational DB(关系型数据库) ---> ElasticSearch
- Databases(数据库)---> Indices (索引)
- Tables(数据库表名)---> Types (类型)
- Rows(行) ---> Documents
- Columns(列) ---> field (域)
(2)ElasticSearch核心概念
- index(索引)
- teyp(类型)
- field(域)
- mapping(映射):mapping是处理数据的方式和规则方面做的限制。设置某个域的数据类型、默认值、分词器、是否存储、是否索引等信息
- document(文档)
- NRT(接近实时):索引一个文档直到这个文档能够被搜索到有一个轻微的延迟(通常在1s以内)
- cluster(集群):一个集群由一个唯一的名字标识,默认为"elasticsearch"。一个节点只能通过指定某个集群的名字来加入这个集群。
- node(节点)
- shards&replicas(分片和复制)
三、ElasticSearch安装
(1)在docker下面安装elasticsearch
(2)在启动容器的时候注意暴露端口 9200端口(web管理平台端口) 9300(服务默认端口)
(3)按照文档安装即可
三、ElasticSearch的客户端操作
(1)有以下方式作为elasticsearch服务的客户端:
- elasticsearch-head插件
- 使用elasticsearch提供的Restful接口访问
- 使用elasticsearch提供的API进行访问
- 使用kibana
(2)使用postman工具镜像restful接口访问
创建索引和mapping:请求的url
PUT 192.168.56.112:9200/blog1
body下的raw内容
{
"mappings": { # 创建mappings
"article": { # 创建type 相当于表名
"properties": {
"id": { # 创建field
"type": "long", # 指定御的类型 long类型不允许分词
"store": true, # 是否存储
"index":"not_analyzed" # 不分词
},
"title": {
"type": "text",
"store": true,
"index":"analyzed", # 分词
"analyzer":"standard" # 分词类型为standard
},
"content": {
"type": "text",
"store": true,
"index":"analyzed",
"analyzer":"standard"
}
}
}
}
}
分词器:
- standard:将中文按照一个词一个词的分
- ik_smart:粗粒度分词
- ik_max_word:细粒度分词
(3)先创建索引后设置mapping
请求的url:
POST http://192.168.56.112:9200/blog2/hello/_mapping
# 该模式使用的是POST请求
# blog2为索引名称 hello为index、_mapping为默认接口
body下的raw内容
{
"hello": { # 创建type 相当于表名
"properties": {
"id": { # 创建field
"type": "long", # 指定御的类型 long类型不允许分词
"store": true, # 是否存储
"index":"not_analyzed" # 不分词
},
"title": {
"type": "text",
"store": true,
"index":"analyzed", # 分词
"analyzer":"standard" # 分词类型为standard
},
"content": {
"type": "text",
"store": true,
"index":"analyzed",
"analyzer":"standard"
}
}
}
}
(4)删除索引
请求url:
DELETE http://192.168.56.112:9200/blog2
# 使用DELETE模式的Restful接口
(5)创建文档(document)
POST 192.168.220.100:9200/blog1/article/1
# 使用POST请求,在索引为blog1下,type为article创建一个id为1的document
请求体:
{
"id":1,
"title":"ElasticSearch是⼀个基于Lucene的搜索服务器",
"content":"它提供了⼀个分布式多⽤户能⼒的全⽂搜索引擎,基于RESTful web接⼝。Elasticsearch是⽤Java开发的,并作为Apache许可条款下的开放源码发布,是当前流⾏的企业级搜索引擎。设计⽤于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使⽤⽅便。 "
}
# 就是一个简单的json格式,"FIELD":"VALUE"
(6)修改文档(document)
根据document的id进行修改,与添加文档一样url一样,只是请求头内容发生变化。
(7)删除文档(document)
DELETE 192.168.220.100:9200/blog1/article/1
# 使用DELETE请求即可,是根据document的id进行删除
(8)查询文档---根据documentid查询
请求url:
GET http://192.168.56.112:9200/blog1/article/1
# 使用GET请求,服务器地址和端口号后面加上索引、类型(表名)和document的id
(9)查询文档---query_string
请求url:
POST http://192.168.56.112:9200/blog1/article/_search
# 使用POST请求 该接口实际上是查询除了article下的所有信息。与请求体的内容结合,查询出所需信息
请求体:
{
"query":{
"query_string":{
"default_field": "title", #查询的字段
"query": "搜索内容"
}
}
}
# 查询出title字段中符合条件的document对象
query_string对搜索关键字分词,在不指定分词器的情况下,将搜索关键字一个一个分。
(10)查询文档---term查询
请求url:
POST http://192.168.56.112:9200/blog1/article/_search
请求体:
{
"query": {
"term":{
"title": "搜索"
}
}
}
term对搜索关键字不分词
四、IK分词器
(1)IK分词器的版本必须与elasticsearch版本一样,否则会报错。
(2)IK分词器下载地址:https://github.com/medcl/elasticsearch-analysis-ik/releases
(3)IK分词器分类:
- ik_smart:粗粒度的拆分
- ik_max_word:细粒度拆分
五、Kibana使用
(1)Kibana的下载安装
使用docker安装:
docker pull docker.io/kibana:5.6.8 #版本号与elasticsearch相同即可
启动kibana容器
docker run -it -d -e ELASTICSEARCH_URL=http://192.168.56.112:9200 --name=kibana -p 5601:5601 kibana:5.6.8
ELASTICSEARCH_URL=http://192.168.56.112:9200:是指链接的ES地址,web访问端口5601
(2)配置索引
访问:http://192.168.56.112:5601进入可视化界面
要使⽤Kibana,您必须⾄少配置⼀个索引。(建议使用通配符的方式)
(3)DSL语句使用
查询所有索引
GET /_cat/indices?v
删除某个索引
DELETE /索引名
新增索引
PUT /索引名
创建映射
PUT /user/userinfo/_mapping
{
"properties": {
"name":{
"type": "text",
"analyzer": "ik_smart",
"search_analyzer": "ik_smart"
},
"city":{
"type": "text",
"analyzer": "ik_smart",
"search_analyzer": "ik_smart"
},
"age":{
"type": "long"
},
"description":{
"type": "text",
"analyzer": "ik_smart",
"search_analyzer": "ik_smart"
}
}
}
新增文档数据 (使用postman工具接口时,用的是POST请求)
PUT /user/userinfo/1
{
"name":"李四",
"age":22,
"city":"深圳",
"description":"李四来⾃湖北武汉! "
}
更新文档数据(使用的是POST请求)
#使⽤POST更新某个域的数据
POST /user/userinfo/4/_update
{
"doc":{
"name":"张三丰",
"description":"在武汉读书,家在武汉!在深圳⼯作! "
}
}
(4)数据查询
查询所有数据
GET /user/_search # 查询index为user下的所有document对象
根据document对象ID查询
GET /user/userinfo/4
sort排序
GET /user/userinfo/_search # 查询出userinfo下的所有document对象
{
"query": {
"match_all":{} # 查询所有,这里可以不写
},
"sort": {
"age":{
"order": "desc" # 降序
}
}
}
# 对查询出来的安装年龄降序排列
分页查询
GET /user/userinfo/_search # 查询出userinfo下的所有document对象
{
"query": {
"match_all":{} # 查询所有,这里可以不写
},
"sort": {
"age":{
"order": "desc" # 降序
}
},
"from":0,
"size":2
}
# 对查询出来的安装年龄降序排列
# from:从N的记录开始查询
# size:每页显示的条数
(5)查询模式
term查询
#查询-term
GET _search
{
"query":{
"term":{
"city":"武汉"
}
}
}
#term查询:搜索关键字不分词
terms查询
terms 跟 term 有点类似,但 terms 允许指定多个匹配条件。 如果某个字段指定了多个值,那么⽂档需要⼀起去做匹配 。
#查询-terms 允许多个Term
GET _search
{
"query":{
"terms":{
"city":
[
"武汉",
"⼴州"
]
}
}
}
# 查询出city字段中有武汉或者广州的数据
match查询
GET _search
{
"query": {
"match": {
"city": "⼴州武汉"
}
}
}
# match查询会分词,根据该字段在设置mapping的时候指定search_analyzer的类型
query_string查询
GET _search
{
"query": {
"query_string": {
"default_field": "city",
"query": "⼴州武汉"
}
}
}
# query_string会分词,根据设置mapping的时候指定的search_analyzer类型
range查询
# -range 范围过滤
# gt表示> gte表示=>
# lt表示< lte表示<=
GET _search
{
"query":{
"range": {
"age": {
"gte": 30,
"lte": 57
}
}
}
}
# 查询出年龄在30至57之间的数据
exists查询:可以用于查找拥有某个域的数据
#搜索 exists:是指包含某个域的数据检索
GET _search
{
"query": {
"exists":{
"field":"address"
}
}
}
# 查询出拥有‘address’这个field的数据
bool查询
- must : 多个查询条件的完全匹配,相当于 and
- must_not : 多个查询条件的相反匹配,相当于 not
- should : ⾄少有⼀个查询条件匹配, 相当于 or
GET _search
{
"query": {
"bool": {
"must": [
{
"term": {
"city": {
"value": "深圳"
}
}
},
{
"range":{
"age":{
"gte":20,
"lte":99
}
}
}
]
}
}
}
# must:首先是必须满足city字段有深圳,然后年龄在20至90之间的数据
# must_not: 首先是city字段不能出现深圳,然后年龄小于20或者大于90的数据
# should:有一个条件满足 是city字段中出现深圳或者年龄在20至90之间的数据
match_all 查询 :可以查询到所有⽂档,是没有查询条件下的默认语句。
#查询所有 match_all
GET _search
{
"query": {
"match_all": {}
}
}
match 查询:match查询是⼀个标准查询,不管你需要全⽂本查询还是精确查询基本上都要⽤到它。
#
字符串匹配
GET _search
{
"query": {
"match": {
"description": "武汉"
}
}
}
prefix 查询:以什么字符开头的,可以更简单地⽤ prefix ,例如查询所有以张开始的⽤户描述
#前缀匹配 prefix
GET _search
{
"query": {
"prefix": {
"name": {
"value": "张"
}
}
}
}
multi_match 查询:允许你做match查询的基础上同时搜索多个字段,在多个字段中同时查⼀个
#多个域匹配搜索
GET _search
{
"query": {
"multi_match": {
"query": "深圳",
"fields": [
"city",
"description"
]
}
}
}
# 在字段"city"和"description"有搜索值"深圳"的document对象
filter过滤查询:只能用在bool查询下
GET _search
{
"query": {
"bool": {
"must": [
{"match": {
"city": "深圳武汉"
}}
],
"filter": {
"range": {
"age": {
"gte": 20,
"lte": 60
}
}
}
}
}
}