elasticsearch 插件 kibana查询
1、查询集群健康
GET _cat/health
2、查询所有索引
GET _cat/indices
3、查询指定索引(product)指定type(yagao)的所有数据
这里的_search是restful的api
GET product/yagao/_search
{
"query": {
"match_all": {}
}
}
返回结果:
{
"took": 3, # 耗时3毫秒
"timed_out": false,
"_shards": {
"total": 5, # 分片五个,成功5个
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 5, # 命中总数5个
"max_score": 1,
"hits": [ # 记录组成数组返回
{
"_index": "product",
"_type": "yagao",
"_id": "5",
"_score": 1,
"_source": {
"id": 5,
"name": "huawei yagao",
"price": 100,
"desc": "shi he nanren",
"tags": [
"jiankang",
"meibai"
]
}
},
{
"_index": "product",
"_type": "yagao",
"_id": "2",
"_score": 1,
"_source": {
"id": 2,
"name": "jiajieshi yagao",
"price": 30,
"desc": "shi he laonian",
"tags": [
"jiankang",
"meibai",
"gaoxiao"
]
}
},
......
]
}
}
4、插入数据,这里的文档id可以选择指定也可以不指定,不指定会自动随机生成字符串id
PUT product/yagao/5
{
"id":5,
"name":"huawei yagao",
"price":100,
"desc":"shi he nanren",
"tags":["jiankang","meibai"]
}
5、根据文档id查询
GET product/yagao/1?pretty
带上pretty指的的用易读的方式显示,这里因为是用的kibana所以不用加也是易读的
6、修改,如下即可
PUT product/yagao/5
{
"id":5,
"name":"huawei yagao",
"price":100,
"desc":"shi he nanren",
"tags":["jiankang","meibai"]
}
7、删除记录
DELETE product/yagao/1
8、查询指定条件的数据,Elastic 默认一次返回10条结果,可以通过size字段改变这个设置。
GET product/yagao/_search
{
"query": {
"match": {
"tags": "meibai"
}
},
"from":2,
"size": 2
}
还可以通过from字段,指定位移。
9、查询可搜索多个关键字,如meibai yiyong,es默认他们之间是or的关系
只要是带meibai或者yiyong的都返回
GET product/yagao/_search
{
"query": {
"match": {
"tags": "meibai yiyong"
}
}
}
如果需要and,必须使用bool查询
GET product/yagao/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"tags": "meibai"
}
},
{
"match": {
"tags": "jiankang"
}
}
]
}
}
}
10、查询并排序
GET product/yagao/_search
{
"query": {
"match": {
"name": "yagao"
}
},
"sort": [
{
"price": {
"order": "desc"
}
}
]
}
11、可以只查询指定字段
GET /product/yagao/_search
{
"query": {
"match_all": {}
},
"_source": ["name","price"]
}
12、查询过滤 filter
GET /product/yagao/_search
{
"query": {
"bool": {
"must": [
{"match": {
"name": "yagao"
}},
{"match": {
"name": "xiaomi"
}}
]
}
},
"post_filter": {
"range": {
"price": {
"gt": 50
}
}
}
}
13、短语搜索,高亮name字段
GET product/yagao/_search
{
"query": {
"match_phrase": {
"name": "heiren yagao"
}
},
"highlight": {
"fields": {
"name": {}
}
}
}
只匹配heiren yagao
14、计算每一个tags下的牙膏的数量
PUT /product/_mapping/yagao
{
"properties": {
"tags":{
"type": "text",
"fielddata": true
}
}
}
GET /product/yagao/_search
{
"aggs": {
"group_by_tag": {
"terms": {
"field": "tags",
"size": 10
}
}
}
}
结果:
"aggregations": {
"group_by_tag": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "jiankang",
"doc_count": 5
},
{
"key": "meibai",
"doc_count": 4
},
{
"key": "gaoxiao",
"doc_count": 2
},
{
"key": "xingjiabigao",
"doc_count": 1
},
{
"key": "yiyong",
"doc_count": 1
}
]
}
}
1、
// 索引创建,mapping映射,解决大写单词查询不到的问题
// 例如查询性别为F的,查询不到
PUT tkdw
{
"mappings": {
"student": {
"properties": {
"sex": {
"type": "keyword"
}
}
}
}
}
2、
// ik分词插件安装
下载 https://github.com/medcl/elasticsearch-analysis-ik,找到对应版本
在plugins下创建ik-analyzer
解压到ik-analyzer下
重启es集群。
测试:
POST _analyze
{
"analyzer": "ik_smart"
"text": "王者荣耀真好玩"
}
--(1)
ik_max_word: 会将文本做最细粒度的拆分,比如会将“中华人民共和国国歌”拆分为
“中华人民共和国,中华人民,中华,华人,人民共和国,人民,人,民,共和国,共和,和,国国,国歌”
会穷尽各种可能的组合;
--(2)
ik_smart: 会做最粗粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,国歌”。
同时也可以
./bin/elasticsearch-plugin install file:///opt/elasticsearch-analysis-ik-7.2.0.zip
IK 官网下载地址
https://github.com/medcl/elasticsearch-analysis-ik/releases