Elastic Search的简单操作
Posted shine-rainbow
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Elastic Search的简单操作相关的知识,希望对你有一定的参考价值。
前言
启动一个节点和kibana
接下来的一切操作都在kiana
中Dev Tools
下的Console
完成。
创建一篇文档。
现在,我们试图插入插入数据。
PUT zhifou/doc/1
{
"name":"顾老二",
"age":30,
"from":"gu",
"desc":"皮肤黑、武器长、性格直",
"tags":["黑","长","直"]
}
PUT zhifou/doc/2
{
"name":"大娘子",
"age":18,
"from":"sheng",
"desc":"貌美肤白、娇憨可爱",
"tags":["白","富","美"]
}
PUT zhifou/doc/3
{
"name":"龙套偏房",
"age":22,
"from":"gu",
"desc":"mmp,没怎么看清,不知道怎么形容",
"tags":["造数据","真","难"]
}
PUT zhifou/doc/4
{
"name":"石头",
"age":29,
"from":"gu",
"desc":"粗中有细、狐假虎威",
"tags":["粗","大","猛"]
}
PUT zhifou/doc/5
{
"name":"魏行者",
"age":25,
"from":"广云台",
"desc":"后悔没能嫁给顾老二~",
"tags":["闭月","羞花"]
}
PUT表示创建命令,虽然命令可以小写,但我们推荐大写。以RESTFul 风格返回结果。
返回结果的信息如下:
{
"_index" : "zhifou",
"_type" : "doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
查询所有索引
现在学习一条命令
GET _cat/indices?v
展示当前集群中索引情况,包括索引的健康状态、UUID、主副分片个数、大小等信息。
查询指定的索引信息
单独查看zhifu索引:
GET zhifou
返回的结果如下:
{
"zhifou" : {
"aliases" : { },
"mappings" : {
"properties" : {
"age" : {
"type" : "long"
},
"desc" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"from" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"tags" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
},
"settings" : {
"index" : {
"creation_date" : "1579267138792",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "thVUwigXSNu__npB0NM9Gg",
"version" : {
"created" : "7030099"
},
"provided_name" : "zhifou"
}
}
}
}
返回了zhifou
索引的创建信心。
查询文档信息
查看某个特定的文档信息
GET zhifou/doc/1
返回的文档信息如下:
{
"_index" : "zhifou",
"_type" : "doc",
"_id" : "1",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "顾老二",
"age" : 30,
"from" : "gu",
"desc" : "皮肤黑、武器长、性格直",
"tags" : [
"黑",
"长",
"直"
]
}
}
查询所有文档信息
GET zhifou/doc/_search
返回的信息如下:
{
"took" : 116,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "zhifou",
"_type" : "doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "顾老二",
"age" : 30,
"from" : "gu",
"desc" : "皮肤黑、武器长、性格直",
"tags" : [
"黑",
"长",
"直"
]
}
},
{
"_index" : "zhifou",
"_type" : "doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "大娘子",
"age" : 18,
"from" : "sheng",
"desc" : "貌美肤白、娇憨可爱",
"tags" : [
"白",
"富",
"美"
]
}
},
{
"_index" : "zhifou",
"_type" : "doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"name" : "龙套偏房",
"age" : 22,
"from" : "gu",
"desc" : "mmp,没怎么看清,不知道怎么形容",
"tags" : [
"造数据",
"真",
"难"
]
}
},
{
"_index" : "zhifou",
"_type" : "doc",
"_id" : "4",
"_score" : 1.0,
"_source" : {
"name" : "石头",
"age" : 29,
"from" : "gu",
"desc" : "粗中有细、狐假虎威",
"tags" : [
"粗",
"大",
"猛"
]
}
},
{
"_index" : "zhifou",
"_type" : "doc",
"_id" : "5",
"_score" : 1.0,
"_source" : {
"name" : "魏行者",
"age" : 25,
"from" : "广云台",
"desc" : "后悔没能嫁给顾老二~",
"tags" : [
"闭月",
"羞花"
]
}
}
]
}
}
删除指定索引
DELETE zhifou
DELETE zhifou/doc/5
删除指定的文档
{
"_index" : "zhifou",
"_type" : "doc",
"_id" : "5",
"_version" : 2,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 5,
"_primary_term" : 1
}
此时在存储文档,会发现5已经不存在了。
{
"took" : 297,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "zhifou",
"_type" : "doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "顾老二",
"age" : 30,
"from" : "gu",
"desc" : "皮肤黑、武器长、性格直",
"tags" : [
"黑",
"长",
"直"
]
}
},
{
"_index" : "zhifou",
"_type" : "doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "大娘子",
"age" : 18,
"from" : "sheng",
"desc" : "貌美肤白、娇憨可爱",
"tags" : [
"白",
"富",
"美"
]
}
},
{
"_index" : "zhifou",
"_type" : "doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"name" : "龙套偏房",
"age" : 22,
"from" : "gu",
"desc" : "mmp,没怎么看清,不知道怎么形容",
"tags" : [
"造数据",
"真",
"难"
]
}
},
{
"_index" : "zhifou",
"_type" : "doc",
"_id" : "4",
"_score" : 1.0,
"_source" : {
"name" : "石头",
"age" : 29,
"from" : "gu",
"desc" : "粗中有细、狐假虎威",
"tags" : [
"粗",
"大",
"猛"
]
}
}
]
}
}
match查询
match系列之match(按照条件查询)
查看来自顾家的都有那些人。
GET zhifou/doc/_search
{
"query":{
"match": {
"from": "gu"
}
}
}
上例,查询条件是一步步构建出来的,将查询条件添加到match
中即可。
返回结果如下:
{
"took" : 82,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 0.82876295,
"hits" : [
{
"_index" : "zhifou",
"_type" : "doc",
"_id" : "1",
"_score" : 0.82876295,
"_source" : {
"name" : "顾老二",
"age" : 30,
"from" : "gu",
"desc" : "皮肤黑、武器长、性格直",
"tags" : [
"黑",
"长",
"直"
]
}
},
{
"_index" : "zhifou",
"_type" : "doc",
"_id" : "3",
"_score" : 0.82876295,
"_source" : {
"name" : "龙套偏房",
"age" : 22,
"from" : "gu",
"desc" : "mmp,没怎么看清,不知道怎么形容",
"tags" : [
"造数据",
"真",
"难"
]
}
},
{
"_index" : "zhifou",
"_type" : "doc",
"_id" : "4",
"_score" : 0.82876295,
"_source" : {
"name" : "石头",
"age" : 29,
"from" : "gu",
"desc" : "粗中有细、狐假虎威",
"tags" : [
"粗",
"大",
"猛"
]
}
}
]
}
}
match系列之match_all(查询全部)
GET zhifou/doc/_search
{
"query":{
"match_all": {}
}
}
match_all
条件为空,表示没有查询条件,那就是查询全部。就像select * from table_name
一样。
查询结果如下:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "zhifou",
"_type" : "doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "顾老二",
"age" : 30,
"from" : "gu",
"desc" : "皮肤黑、武器长、性格直",
"tags" : [
"黑",
"长",
"直"
]
}
},
{
"_index" : "zhifou",
"_type" : "doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "大娘子",
"age" : 18,
"from" : "sheng",
"desc" : "貌美肤白、娇憨可爱",
"tags" : [
"白",
"富",
"美"
]
}
},
{
"_index" : "zhifou",
"_type" : "doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"name" : "龙套偏房",
"age" : 22,
"from" : "gu",
"desc" : "mmp,没怎么看清,不知道怎么形容",
"tags" : [
"造数据",
"真",
"难"
]
}
},
{
"_index" : "zhifou",
"_type" : "doc",
"_id" : "4",
"_score" : 1.0,
"_source" : {
"name" : "石头",
"age" : 29,
"from" : "gu",
"desc" : "粗中有细、狐假虎威",
"tags" : [
"粗",
"大",
"猛"
]
}
},
{
"_index" : "zhifou",
"_type" : "doc",
"_id" : "5",
"_score" : 1.0,
"_source" : {
"name" : "魏行者",
"age" : 25,
"from" : "广云台",
"desc" : "后悔没能嫁给顾老二~",
"tags" : [
"闭月",
"羞花"
]
}
}
]
}
}
match系列之match_phrase(短语查询)
我们现在已经对match有了基础的了解。match查询的是散列映射,包含了我们希望搜索的字段和字符串。但也带来了一些问题。
首先来创建一些数据
PUT t1/doc/1
{
"title":"中国是世界上人口最多的国家"
}
PUT t1/doc/2
{
"title":"美国是世界上军事实力最强大的国家"
}
PUT t1/doc/3
{
"title":"北京市中国的首都"
}
现在,当我们以中国
作为搜索条件,我们希望只返回和中国
相关的文档。我们首先使用match
进行查询:
GET t1/doc/_search
{
"query":{
"match": {
"title": "中国"
}
}
}
结果如下:
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 0.7048458,
"hits" : [
{
"_index" : "t1",
"_type" : "doc",
"_id" : "3",
"_score" : 0.7048458,
"_source" : {
"title" : "北京市中国的首都"
}
},
{
"_index" : "t1",
"_type" : "doc",
"_id" : "1",
"_score" : 0.6406914,
"_source" : {
"title" : "中国是世界上人口最多的国家"
}
},
{
"_index" : "t1",
"_type" : "doc",
"_id" : "2",
"_score" : 0.16943811,
"_source" : {
"title" : "美国是世界上军事实力最强大的国家"
}
}
]
}
}
虽然如期返回了中国的文档
,但是把美国的文档
也返回了。主要是由于Elastic Search在内部对文档进行分词的时候,对于中文来说,是一个字一个字进行切分的。
可以使用短语查询:
GET t1/doc/_search
{
"query":{
"match_phrase": {
"title": "中国"
}
}
}
返回结果:
{
"took" : 7,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.70484585,
"hits" : [
{
"_index" : "t1",
"_type" : "doc",
"_id" : "3",
"_score" : 0.70484585,
"_source" : {
"title" : "北京市中国的首都"
}
},
{
"_index" : "t1",
"_type" : "doc",
"_id" : "1",
"_score" : 0.59047776,
"_source" : {
"title" : "中国是世界上人口最多的国家"
}
}
]
}
}
这里match_pharse
是在文档中搜索指定的词组,而中国
正是一个词组,可以返回。
那么,现在想搜索中国
和世界
相关的文档,但是有忘记其余部分内容,就可以使用slop来解决了。
GET t1/doc/_search
{
"query":{
"match_phrase": {
"title": {
"query": "中国世界",
"slop": 2
}
}
}
}
在两个词组之间有了2个词的间隔,这个时候,就可以查询到结果了:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.9709763,
"hits" : [
{
"_index" : "t1",
"_type" : "doc",
"_id" : "1",
"_score" : 0.9709763,
"_source" : {
"title" : "中国是世界上人口最多的国家"
}
}
]
}
}
slop
间隔可以根据需要进行适当的改动。
match系列之match_pharse_prefix(最左前缀查询)
如果遇到拼写beautiful
拼到bea
就不知道该往下怎么拼写了。这个时候,搜索引擎就看自己的词库有啥以bea
开头的词。
PUT t3/doc/1
{
"title":"maggie",
"desc":"beautiful girl you are beautiful so "
}
PUT t3/doc/2
{
"title":"sun and beach",
"desc":"I like basking on the beach"
}
在这里用match
和match_phrase
都不太合适,因为输入的是不完整的词,可以用match_phrase_prefix
GET t3/doc/_search
{
"query":{
"match_phrase_prefix": {
"desc": "bea"
}
}
}
结果如下:
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.9530774,
"hits" : [
{
"_index" : "t3",
"_type" : "doc",
"_id" : "1",
"_score" : 0.9530774,
"_source" : {
"title" : "maggie",
"desc" : "beautiful girl you are beautiful so "
}
},
{
"_index" : "t3",
"_type" : "doc",
"_id" : "2",
"_score" : 0.6931472,
"_source" : {
"title" : "sun and beach",
"desc" : "I like basking on the beach"
}
}
]
}
}
前缀查询与短语查询类似,但前缀查询可以进一步的搜索词组,只不过它是和词组中的最后一个词进行匹配,当使用这种行为进行搜索的时候,最好通过max_expansions
来设置最大前缀扩展数量,因为产生的结果可能是一个很大的集合,不加限制的话,影响查询性能。
GET t3/doc/_search
{
"query":{
"match_phrase_prefix": {
"desc": {
"query": "bea",
"max_expansions": 1
}
}
}
}
结果返回:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.9530774,
"hits" : [
{
"_index" : "t3",
"_type" : "doc",
"_id" : "1",
"_score" : 0.9530774,
"_source" : {
"title" : "maggie",
"desc" : "beautiful girl you are beautiful so "
}
}
]
}
}
如果设置2,则两篇都返回。
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.9530774,
"hits" : [
{
"_index" : "t3",
"_type" : "doc",
"_id" : "1",
"_score" : 0.9530774,
"_source" : {
"title" : "maggie",
"desc" : "beautiful girl you are beautiful so "
}
},
{
"_index" : "t3",
"_type" : "doc",
"_id" : "2",
"_score" : 0.6931472,
"_source" : {
"title" : "sun and beach",
"desc" : "I like basking on the beach"
}
}
]
}
}
match 系列之multi_match(多字段查询)
现在我们有一个50个字段的索引,我们需要在多个字段中查询同一个关键字。
GET t3/doc/_search
{
"query":{
"multi_match": {
"query": "beach"
, "fields": ["title","desc"]
}
}
}
我们可以将多个字段放到field
列表中即可。以达到匹配多个字段的目的。
以上是关于Elastic Search的简单操作的主要内容,如果未能解决你的问题,请参考以下文章
Elastic Search | Head插件的安装与简单使用
Golang 用Olivere库操作Elastic Search