kibana对es的一些基本操作:
1、查看index
点击如图菜单可以到es的index 管理界面:
点击一条记录可以看到Index的具体信息如下:
如上图所示,可以看到索引的基本信息,也可以对删除、关闭索引等。
2、执行命令行
点击Dev Tools 菜单进入到console界面可以执行命令
3、常用命令
create index:
命令:
PUT index_1
运行结果:
#! Deprecation: the default number of shards will change from [5] to [1] in 7.0.0; if you wish to continue using the default of [5] shards, you must manage this on the create index request or with an index template
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "index_1"
}
delete index:
命令:
DELETE index_1
运行结果:
{
"acknowledged" : true
}
create document with id:
命令:
PUT /index_1/log/123
{
"title":"圣诞节惊喜",
"text":"这个圣诞节很特殊",
"data":"2019/03/22"
}
运行结果:
{
"_index" : "index_1",
"_type" : "log",
"_id" : "123",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
查询:
命令:
GET index_1/_search
{
"query":{
"match":{
"title":"圣"
}
}
}
运行结果:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "index_1",
"_type" : "log",
"_id" : "123",
"_score" : 0.2876821,
"_source" : {
"title" : "圣诞节惊喜",
"text" : "这个圣诞节很特殊",
"data" : "2019/03/22"
}
}
]
}
}
设置Ik分词:
index:index1
type:fulltext
doc:content
命令:
PUT index1
{
"mappings": {
"fulltext":{
"properties":{
"content":{
"type":"text",
"analyzer":"ik_max_word",
"search_analyzer":"ik_max_word"
}
}
}
}
}
如果出现以下错误:
{
"error": {
"root_cause": [
{
"type": "resource_already_exists_exception",
"reason": "index [index1/P6h5Hz4pQ4mhQfetR9wh8A] already exists",
"index_uuid": "P6h5Hz4pQ4mhQfetR9wh8A",
"index": "index1"
}
],
"type": "resource_already_exists_exception",
"reason": "index [index1/P6h5Hz4pQ4mhQfetR9wh8A] already exists",
"index_uuid": "P6h5Hz4pQ4mhQfetR9wh8A",
"index": "index1"
},
"status": 400
}
需要删除索引,再重新建立。
测试分词效果:
命令:
GET index1/_analyze
{
"text":"我们的使命、愿景、价值观",
"tokenizer": "ik_max_word"
}
Ik分词器,会把 我们的使命、愿景、价值观,分成以下词,当匹配以下词组时才会匹配出结查
运行结果:
{
"tokens" : [
{
"token" : "我们",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "的",
"start_offset" : 2,
"end_offset" : 3,
"type" : "CN_CHAR",
"position" : 1
},
{
"token" : "使命",
"start_offset" : 3,
"end_offset" : 5,
"type" : "CN_WORD",
"position" : 2
},
{
"token" : "愿",
"start_offset" : 6,
"end_offset" : 7,
"type" : "CN_CHAR",
"position" : 3
},
{
"token" : "景",
"start_offset" : 7,
"end_offset" : 8,
"type" : "CN_CHAR",
"position" : 4
},
{
"token" : "价值观",
"start_offset" : 9,
"end_offset" : 12,
"type" : "CN_WORD",
"position" : 5
},
{
"token" : "价值",
"start_offset" : 9,
"end_offset" : 11,
"type" : "CN_WORD",
"position" : 6
},
{
"token" : "观",
"start_offset" : 11,
"end_offset" : 12,
"type" : "CN_CHAR",
"position" : 7
}
]
}
如果不用ik分词器,用默认的分词器,则会匹配每个汉字,这不是我们想要的结查。
GET index1/_analyze
{
"text":"我们的使命、愿景、价值观"
}
运行结果:
{
"tokens" : [
{
"token" : "我",
"start_offset" : 0,
"end_offset" : 1,
"type" : "<IDEOGRAPHIC>",
"position" : 0
},
{
"token" : "们",
"start_offset" : 1,
"end_offset" : 2,
"type" : "<IDEOGRAPHIC>",
"position" : 1
},
{
"token" : "的",
"start_offset" : 2,
"end_offset" : 3,
"type" : "<IDEOGRAPHIC>",
"position" : 2
},
{
"token" : "使",
"start_offset" : 3,
"end_offset" : 4,
"type" : "<IDEOGRAPHIC>",
"position" : 3
},
{
"token" : "命",
"start_offset" : 4,
"end_offset" : 5,
"type" : "<IDEOGRAPHIC>",
"position" : 4
},
{
"token" : "愿",
"start_offset" : 6,
"end_offset" : 7,
"type" : "<IDEOGRAPHIC>",
"position" : 5
},
{
"token" : "景",
"start_offset" : 7,
"end_offset" : 8,
"type" : "<IDEOGRAPHIC>",
"position" : 6
},
{
"token" : "价",
"start_offset" : 9,
"end_offset" : 10,
"type" : "<IDEOGRAPHIC>",
"position" : 7
},
{
"token" : "值",
"start_offset" : 10,
"end_offset" : 11,
"type" : "<IDEOGRAPHIC>",
"position" : 8
},
{
"token" : "观",
"start_offset" : 11,
"end_offset" : 12,
"type" : "<IDEOGRAPHIC>",
"position" : 9
}
]
}
ik_smart:
安装Ik分词器后,如果想要最粗粒度的拆分,那么可以设置
"analyzer":"ik_smart",
"search_analyzer":"ik_smart"
测试效果:
GET index1/_analyze
{
"text":"我们的使命、愿景、价值观",
"tokenizer": "ik_smart"
}
{
"tokens" : [
{
"token" : "我们",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "的",
"start_offset" : 2,
"end_offset" : 3,
"type" : "CN_CHAR",
"position" : 1
},
{
"token" : "使命",
"start_offset" : 3,
"end_offset" : 5,
"type" : "CN_WORD",
"position" : 2
},
{
"token" : "愿",
"start_offset" : 6,
"end_offset" : 7,
"type" : "CN_CHAR",
"position" : 3
},
{
"token" : "景",
"start_offset" : 7,
"end_offset" : 8,
"type" : "CN_CHAR",
"position" : 4
},
{
"token" : "价值观",
"start_offset" : 9,
"end_offset" : 12,
"type" : "CN_WORD",
"position" : 5
}
]
}
ik_max_word 和 ik_smart 什么区别?
ik_max_word: 会将文本做最细粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,中华人民,中华,华人,人民共和国,人民,人,民,共和国,共和,和,国国,国歌”,会穷尽各种可能的组合;
ik_smart: 会做最粗粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,国歌”。
ik分词器设置示列可参考:
https://github.com/medcl/elasticsearch-analysis-ik
ES中match和term命令
match在匹配时会对所查找的关键词进行分词,然后按分词匹配查找,而term会直接对关键词进行查找。一般模糊查找的时候,多用match,而精确查找时可以使用term。
命令:
GET index1/_search
{
"query":{
"match":{
"title":"们的"
}
}
}
match 会将关键词进行分词分成“们”和“的”,查找时包含其中任一均可被匹配到。
同下面term查询是一样的
GET /index1/_search
{
"query": {
"bool": {
"should": [
{
"term": {
"title": {
"value": "们"
}
}
},
{
"term": {
"title": {
"value": "的"
}
}
}
]
}
}
}
如果我们想精确匹配们的
GET index1/_search
{
"query":{
"term":{
"title":"们的"
}
}
}
或
GET /index1/_search
{
"query": {
"bool": {
"should": [
{
"term": {
"title": {
"value": "们的"
}
}
}
]
}
}
}