Kibana Web页面操作Elastic Search

一 准备工作

es版本6.3.1 kibana 版本6.3.1
启动三台ES节点,组建成Es集群
启动kibana 连接上Es
访问kibana提供的Web页面
http://hadoop102:5601
kibana dev-Tools面板

二 接口Restfull Api

2.1 查看es中有哪些索引

GET /_cat/indices?v

查询结果
表头含义:
health green(集群完整) yellow(单点正常、集群不完整) red(单点不正常)
status 是否能使用
index 索引名
uuid 索引统一编号
pri 主节点几个
rep 从节点几个
docs.count 文档数
docs.deleted 文档被删了多少
store.size 整体占空间大小
pri.store.size 主节点占

2.2 创建索引(index)类似于数据库中的database

PUT /index_movie

2.3 删除索引(index)

DELETE /index_movie

2.4 新增文档

PUT index_movie/movie/1
{
  "title":"zhanlang",
  "score":9.2,
  "author":[
    {"name":"wujing"},
    {"sex":"man"}
  ]
}

注意:

  • 如果之前没建过 index 或者 type,es 会自动创建。
  • type类似于数据库中表,6.x一个Index下只允许创建一个type
  • Id可以不指定,不指定会默认生成

2.5 搜索 type 全部数据

Get index_movie/movie/_search

2.6 根据指定的Id进行搜索

GET index_movie/movie/1

2.7 修改 document

2.7.1 覆盖修改(id 相同)

使用PUT方式ID相同,将原数据覆盖

PUT index_movie/movie/1
{
  "title":"zhanlang"
  
}

2.7.2 修改单个字段

POST index_movie/movie/1/_update
{
  "doc": {
  "title":"zhanlang",
  "score":9.2,
  "author":[
    {"name":"wujing"},
    {"sex":"man"}
  ]  
  }
}

注意:

  • 修改后面需要加上 _update
  • 修改的数据需要被 “doc”:{}包裹
  • 使用POST方式

2.8 删除一个 document

DELETE index_movie/movie/1

2.9 按条件查询全部

GET index_movie/movie/_search
{
  "query": {
    "match_all": {} 
  }
}

2.10 按照字段的分词查询

再插入两条数据:

POST index_movie/movie/
{
  "title":"honghai  xingdong ",
  "score":9.2,
  "author":[
    {"name":"zhangsan sange","sex":"man"},
     {"name":"lisi sige","sex":"man"}
  ]
}
POST index_movie/movie/
{
  "title":"honghai1  xingdong1 ",
  "score":9.2,
  "author":[
    {"name":"zhangsan sange","sex":"man"},
     {"name":"lisi sige","sex":"man"}
  ]
}
GET index_movie/movie/_search
{
  "query": {
    "match": {
      "title": "honghai"
    }
    
  }
  
}

注意:

  • es中默认会为字段添加索引
  • 字段类型为text时会进行分词
  • 英文分词,按照非单词字符以及语气词进行切分
  • 中文分词,需安装对应的分词器
  • index_movie/movie/_search 指定查询的对应的Index和type
  • 发送json 格式进行查询 条件在query:{}对象中

2.11 按照分词子属性查询

查询出演员中包含zhangsan 字样的Document

GET index_movie/movie/_search
{
  "query": {
    "match": {
      "author.name": "zhangsan"
    }
  }
}

2.12 按照短语查询

按照短语查询的意思是指, 匹配某个 field 的整个内容, 不再利用分词技术

GET index_movie/movie/_search
{
  "query": {
    "match_phrase": {
      "title": "honghai"
    }
  }
}

注意:

  • 会当成一个整体进行匹配,也会匹配出结果,只是匹配度不高
  • 完全匹配,匹配度为1 局部匹配的时候分数会小

2.13 模糊查询

校正匹配分词,当一个单词都无法准确匹配,es 通过一种算法对非常接近的单词也给与一定的评分,能够查询出来,但是消耗更多的性能.

GET index_movie/movie/_search
{
  "query": {
    "fuzzy": {
      "title":"honghai"
    }
    
  }
}

2.14 过滤(查询后过滤)

查询出主题包含honghai 且演员为man的Document

GET index_movie/movie/_search
{
  "query": {
    "match": {
     "title": "honghai"
    }
    
  }
  , "post_filter": {
    "term": {
      "author.sex": "man"
    }
    
  }
}

2.15 先过滤再查询

查询演员包含wujing 电影名字为wujing 且 分数为9.2分的数据

GET index_movie/movie/_search
{
  "query": {
    "bool": {
      "filter": [
        {"term": 
          {"title": "zhanlang"}
        },
        {
          "term":
            {"score": 9.2}
        }
        ]
      
     , "must": [
       {
        "match": {
          "author.name": "wujing"
        } 
       }
     ]
      
    }
    
  }

}

2.16 范围查询

gt大于
lt小于
gte大于等于
lte小于等于
GET /index_movie/movie/_search
{
  "query": {
    "bool": {
      "filter": {
        "range": {
          "score": {
            "gte": 9,
            "lte": 10
          }
        }
        
      }
      
    }
  }
}

2.17 排序查询

查询title包含honghai 且按照评分升序

GET /index_movie/movie/_search
{
  "query": {
    
    "match": {
      "title": "honghai"
    }
  }
  , "sort": [
    {
      "score": {
        "order": "asc"
      }
    }
  ]
}

2.18 分页查询

match_all 匹配全部 from 从第几条开始查询 size每页多少条记录
实际查询时,应该动态计算(currentPage-1)*size

GET /index_movie/movie/_search
{
  "from": 1,
  "size": 2
  , "query": {
    "match_all": {}
  }
}

2.18 指定查询的字段

GET /index_movie/movie/_search
{
  "query": {"match_all": {}}
    , "_source": ["title", "score"]
  
}
  • 通过_source指定需要查询的字段
  • 如果是单个字段只需要 “_source”: “title”

2.19 聚合

插入多条数据

GET /index_movie/movie/_search
{
 
  
  "aggs": {
    "groupby_author_name": {
      "terms": {
        "field": "author.name.keyword"
        , "size": 4
      }
    }
  }
}

  • groupby_author_name 分组的名字任意起的
  • field 指定分组字段(分组字段不能是TEXT类型)
  • size 最大分为几组

2.20 分组集合求平均值,并按照平均值升序

GET /index_movie/movie/_search
{
  "aggs": {
    "groupby_author_name": {
      "terms": {
        "field": "author.name.keyword"
        , "size": 4,
        "order": {
          "avg_score": "asc"
        }
      },
     "aggs": {
       "avg_score": {
         "avg": {
           "field": "score"
         }
       }
     }
    }
  }
}

注:

  • 指定分组的名字 groupby_author_name
  • field 指定分组字段
  • size 指定最大分组数为4
  • order 指定排序
  • avg_score 为平均值的名
  • 指定avg_score 平均值的别名
  • avg 聚合行为
  • field 指定对哪里求平均值

三 ElasticSearch 中的分词器

Elasticsearch自带的分词器
自带的分词器
中文分析器
中文分词器介绍

3.1 安装IK中文分词器

官方下载地址:
https://github.com/medcl/elasticsearch-analysis-ik/releases
解压下载后的jar包
官方使用文档:
https://github.com/medcl/elasticsearch-analysis-ik

unzip  elasticsearch-analysis-ik-6.3.1.zip -d /opt/module/elasticsearch-6.3.1/plugins/ik

注:需解压到ElasticSearch安装目录的plugins目录,分发文件到其它es节点,重新启动es集群

3.2 测试

3.2.1 使用默认分词器

GET /index_person/_analyze
{
  "analyzer": "ik_smart",
  "text": "大数据"
}

结果:
默认分词器

3.2.2 使用Ik分词器

ik_smart

GET /index_person/_analyze
{
  "analyzer": "ik_smart",
  "text": "大数据"
}

结果:
使用Ik分词器
ik_max_word:

GET /index_person/_analyze
{
  "analyzer": "ik_max_word",
  "text": "大数据"
}

查询结果:
在这里插入图片描述
分词类型
ik_max_word:最大分词
ik_smart:最小分词

四 查看mappings手动指定mapping

4.1 查看mapping

GET /index_person/person/_mapping
{
  "index_person": { // Index名
    "mappings": { 
      "person": { // type 名称
        "properties": { // 属性名
          "addr": {  // addr字段
            "type": "text",  // 类型text
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "text": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}

支持的类型
默认类型推断

true/false → boolean
1020  →  long
20.1 → double
“2018-02-01” → date
“hello world” → text +keyword

在一些特殊情况下,不需要对列进行分词,所以可以手动只当mapping

4.2 手动指定mapping

PUT movie_chn
{
  "mappings": {
    "movie":{
      "properties": {
        "id":{
          "type": "long"
        },
        "name":{
          "type": "text"
          , "analyzer": "ik_smart"
        },
        "doubanScore":{
          "type": "double"
        },
        "actorList":{
          "properties": {
            "id":{
              "type":"long"
            },
            "name":{
              "type":"keyword"
            }
          }
        }
      }
    }
  }
}

插入数据:

PUT /movie_chn/movie/1
{ "id":1,
  "name":"红海行动",
  "doubanScore":8.5,
  "actorList":[  
    {"id":1,"name":"张译"},
    {"id":2,"name":"海清"},
    {"id":3,"name":"张涵予"}
   ]
}
PUT /movie_chn/movie/2
{
  "id":2,
  "name":"湄公河行动",
  "doubanScore":8.0,
  "actorList":[  
    {"id":3,"name":"张涵予"}
  ]
}

PUT /movie_chn/movie/3
{
  "id":3,
  "name":"红海事件",
  "doubanScore":5.0,
  "actorList":[  
    {"id":4,"name":"张晨"}
  ]
}

查询数据:

GET /movie_chn/movie/_search
{
  "query": {
    "match": {
      "name": "红海"
    }
  }
  
}

手动指定mapping,指定分词器进行查询

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Master_slaves

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值