ES kibana常用语法---增删改查_es 空字符串值查询

GET index名称/_count
{
“query”: {
“bool”: {
“must_not”: [
{
“exists”: {
“field”: “字段名称”
}
}
]
}
}
}

字段值为空字符串

GET index名称/_count
{
“query”: {
“bool”: {
“must_not”: [
{
“wildcard”: {
“字段名称”: {
“value”: “*”
}
}
}
]
}
}
}

字段值不为空字符串的文档

GET index名称/_count
{
“query”: {
“wildcard”: {
“字段名称”: {
“value”: “*”
}
}
}
}

1. Match Query:用于搜索指定字段中包含指定词条的文档。

GET /_search
{
“query”: {
“match”: {
“title”: “Elasticsearch”
}
}
}

2. Term Query:用于搜索指定字段中包含指定词条的文档,不会对词条进行分词。

GET /_search
{
“query”: {
“term”: {
“title”: “Elasticsearch”
}
}
}

3. Range Query:用于搜索指定字段中符合指定范围的文档。

GET /_search
{
“query”: {
“range”: {
“age”: {
“gte”: 18,
“lte”: 30
}
}
}
}

4. Bool Query:用于组合多个查询条件,支持must、should、must_not和filter四种子查询。

GET /_search
{
“query”: {
“bool”: {
“must”: [
{ “match”: { “title”: “Elasticsearch” }},
{ “match”: { “content”: “Java” }}
],
“should”: [
{ “match”: { “author”: “John” }},
{ “match”: { “author”: “Jane” }}
],
“must_not”: [
{ “match”: { “status”: “deleted” }}
],
“filter”: [
{ “range”: { “date”: { “gte”: “2020-01-01” }}}
]
}
}
}

5. Wildcard Query:用于搜索指定字段中符合通配符表达式的文档。

GET /_search
{
“query”: {
“wildcard”: {
“title”: “Elast*csearch”
}
}
}

6. Fuzzy Query:用于搜索指定字段中与指定词条相似的文档。

GET /_search
{
“query”: {
“fuzzy”: {
“title”: {
“value”: “Elastiksearch”,
“fuzziness”: “2”
}
}
}
}

7. Prefix Query:用于搜索指定字段中以指定前缀开头的文档。

GET /_search
{
“query”: {
“prefix”: {
“title”: “Elast”
}
}
}

8. Match Phrase Query:用于搜索指定字段中包含指定短语的文档。

GET /_search
{
“query”: {
“match_phrase”: {
“title”: “Elasticsearch tutorial”
}
}
}

基本查询

查询某个索引所有的文档

GET /your_index_name/_search
{
“query”: {
“match_all”: {}
}
}

GET /your_index_name/_search

查询字符串搜索

GET test/doc/_search?q=name:wangfei

结构化查询(单字段查询,不能多字段组合查询)

GET test/doc/_search
{
“query”:{
“match”:{
“name”:“wang”
}
}
}

match 系列

1、match:返回所有匹配的分词。

2、match_all:查询全部。

3、match_phrase:短语查询,在match的基础上进一步查询词组,可以指定slop分词间隔。

4、match_phrase_prefix:前缀查询,根据短语中最后一个词组做前缀匹配,可以应用于搜索提示,但注意和max_expanions搭配。其实默认是50…

5、multi_match:多字段查询,使用相当的灵活,可以完成 match_phrase 和 match_phrase_prefix 的工作。

match_all (查询全部)

GET test/doc/_search
{
“query”:{
“match_all”: {
}
}
}

match_phrase(短语查询)

匹配一整个词语

只用match,会单个字匹配

GET test1/doc/_search { “query”:{ “match”:{ “title”:“中国” } } }

match_phrase_prefix(最左前缀查询)智能搜索–以什么开头
multi_match(多字段查询)
  • multi_match是要在多个字段中查询同一个关键字 除此之外,mulit_match甚至可以当做match_phrase和match_phrase_prefix使用,只需要指定type类型即可

GET test2/doc/_search
{
“query”: {
“multi_match”: {
“query”: “beautiful”,
“fields”: [
“title”,
“desc”
]
}
}
}

  • 当设置属性 type:phrase 时 等同于 短语查询
    GET test1/doc/_search
    {
    “query”: {
    “multi_match”: {
    “query”: “中国”,
    “fields”: [
    “title”
    ],
    “type”: “phrase”
    }
    }
    }
  • 当设置属性 type:phrase_prefix时 等同于 最左前缀查询
    GET test2/doc/_search
    {
    “query”: {
    “multi_match”: {
    “query”: “bea”,
    “fields”: [
    “desc”
    ],
    “type”: “phrase_prefix”
    }
    }
    }

排序查询

倒排
GET test/doc/_search
{
“query”: {
“match_all”: {}
},
“sort”: [
{
“age”: {
“order”: “desc”
}
}
]
}
升序
GET test/doc/_search
{
“query”: {
“match_all”: {}
},
“sort”: [
{
“age”: {
“order”: “asc”
}
}
]
}

分页查询

GET test/doc/_search
{
“query”: {
“match_phrase_prefix”: {
“name”: “wang”
}
},
“from”: 0,
“size”: 1
}

bool 查询 (must、should)

bool 查询总结
must:与关系,相当于关系型数据库中的 and。
should:或关系,相当于关系型数据库中的 or。
must_not:非关系,相当于关系型数据库中的 not。
filter:过滤条件。
range:条件筛选范围。
gt:大于,相当于关系型数据库中的 >。
gte:大于等于,相当于关系型数据库中的 >=。
lt:小于,相当于关系型数据库中的 <。
lte:小于等于,相当于关系型数据库中的 <=。

must

(must字段对应的是个列表,也就是说可以有多个并列的查询条件,一个文档满足各个子条件后才最终返回)

单条件查询

GET test/doc/_search
{
“query”: {
“bool”: {
“must”: [
{
“match”: {
“name”: “wangfei”
}
}
]
}
}
}

多条件组合查询

GET test/doc/_search
{
“query”: {
“bool”: {
“must”: [
{
“match”: {
“name”: “wanggfei”
}
},
{
“match”: {
“age”: 25
}
}
]
}
}
}

should

(只要符合其中一个条件就返回)

GET test/doc/_search
{
“query”: {
“bool”: {
“should”: [
{
“match”: {
“name”: “wangjifei”
}
},
{
“match”: {
“age”: 27
}
}
]
}
}
}

must_not

GET test/doc/_search
{
“query”: {
“bool”: {
“must_not”: [
{
“match”: {
“name”: “wangjifei”
}
},
{
“match”: {
“age”: 27
}
}
]
}
}
}

filter

(条件过滤查询,过滤条件的范围用range表示gt表示大于、lt表示小于、gte表示大于等于、lte表示小于等于)

GET test/doc/_search
{
“query”: {
“bool”: {
“must”: [
{
“match”: {
“name”: “wangjifei”
}
}
],
“filter”: {
“range”: {
“age”: {
“gte”: 10,
“lt”: 27
}
}
}
}
}
}

查询结果过滤

GET test3/doc/_search
{
“query”: {
“match”: {
“name”: “顾”
}
},
“_source”: [“name”,“age”]
}

查询结果
{
“took” : 58,
“timed_out” : false,
“_shards” : {
“total” : 5,
“successful” : 5,
“skipped” : 0,
“failed” : 0
},
“hits” : {
“total” : 1,
“max_score” : 0.2876821,
“hits” : [
{
“_index” : “test3”,
“_type” : “doc”,
“_id” : “1”,
“_score” : 0.2876821,
“_source” : {
“name” : “顾老二”,
“age” : 30
}
}
]
}
}

查询结果高亮显示 (默认高亮显示)

GET test3/doc/_search
{
“query”: {
“match”: {
“name”: “顾老二”
}
},
“highlight”: {
“fields”: {
“name”: {}
}
}
}

查询结果
{
“took” : 216,
“timed_out” : false,
“_shards” : {
“total” : 5,
“successful” : 5,
“skipped” : 0,
“failed” : 0
},
“hits” : {
“total” : 1,
“max_score” : 0.8630463,
“hits” : [
{
“_index” : “test3”,
“_type” : “doc”,
“_id” : “1”,
“_score” : 0.8630463,
“_source” : {
“name” : “顾老二”,
“age” : 30,
“from” : “gu”,
“desc” : “皮肤黑、武器长、性格直”,
“tags” : [
“黑”,
“长”,
“直”
]
},
“highlight” : {
“name” : [

]
}
}
]
}
}

精确查询与模糊查询

term 和 match 的区别是:
match 经过分词,用于模糊查询,可以查看某个字段值中的部分词
term 不经过分词,只能匹配到完整的字段值,用于精确查询

准备数据

PUT w1
{
“mappings”: {
“doc”: {
“properties”:{
“t1”:{
“type”: “text”
},
“t2”: {
“type”: “keyword”
}
}
}
}
}

PUT w1/doc/1
{
“t1”: “hi single dog”,
“t2”: “hi single dog”
}

t1类型为text,会经过分词,match查询时条件也会经过分词,所以下面两种查询都能查到结果

GET w1/doc/_search
{
“query”: {
“match”: {
“t1”: “hi single dog”
}
}
}

GET w1/doc/_search
{
“query”: {
“match”: {
“t1”: “hi”
}
}
}

t2类型为keyword类型,不会经过分词,match查询时条件会经过分词,所以只能当值为"hi single dog"时能查询到

GET w1/doc/_search
{
“query”: {
“match”: {
“t2”: “hi”
}
}
}

GET w1/doc/_search
{
“query”: {
“match”: {
“t2”: “hi single dog”
}
}
}

t1类型为text,会经过分词,term查询时条件不会经过分词,所以只有当值为"hi"时能查询到

GET w1/doc/_search
{
“query”: {
“term”: {
“t1”: “hi single dog”
}
}
}

GET w1/doc/_search
{
“query”: {
“term”: {
“t1”: “hi”
}
}
}

t2类型为keyword类型,不会经过分词,term查询时条件不会经过分词,所以只能当值为"hi single dog"时能查询到

GET w1/doc/_search
{
“query”: {
“term”: {
“t2”: “hi single dog”
}
}
}

GET w1/doc/_search
{
“query”: {
“term”: {
“t2”: “hi”
}
}
}

  • 查找多个精确值(terms)
第一个查询方式

GET test/doc/_search
{
“query”: {
“bool”: {
“should”: [
{
“term”: {
“age”:27
}
},{
“term”:{
“age”:28
}
}
]
}
}
}

第二个查询方式

GET test/doc/_search
{
“query”: {
“terms”: {
“age”: [
“27”,
“28”
]
}
}
}

聚合查询 avg、max、min、sum

数据准备

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”:“仿佛兮若轻云之蔽月,飘飘兮若流风之回雪,mmp,最后竟然没有嫁给顾老二!”,
“tags”:[“闭月”,“羞花”]
}

GET zhifou/doc/_search
{
“query”: {
“match_all”: {}
}
}

  • 需求1、查询from是gu的人的平均年龄。
    GET zhifou/doc/_search
    {
    “query”: {
    “match”: {
    “from”: “gu”
    }
    },
    “aggs”: {
    “my_avg”: {
    “avg”: {
    “field”: “age”
    }
    }
    },
    “_source”: [“name”, “age”]
    }

查询结果
{
“took” : 83,
“timed_out” : false,
“_shards” : {
“total” : 5,
“successful” : 5,
“skipped” : 0,
“failed” : 0
},
“hits” : {
“total” : 3,
“max_score” : 0.6931472,
“hits” : [
{
“_index” : “zhifou”,
“_type” : “doc”,
“_id” : “4”,
“_score” : 0.6931472,
“_source” : {
“name” : “石头”,
“age” : 29
}
},
{
“_index” : “zhifou”,
“_type” : “doc”,
“_id” : “1”,
“_score” : 0.2876821,
“_source” : {
“name” : “顾老二”,
“age” : 30
}
},
{
“_index” : “zhifou”,
“_type” : “doc”,
“_id” : “3”,
“_score” : 0.2876821,
“_source” : {
“name” : “龙套偏房”,
“age” : 22
}
}
]
},
“aggregations” : {
“my_avg” : {
“value” : 27.0
}
}
}

上例中,首先匹配查询from是gu的数据。在此基础上做查询平均值的操作,这里就用到了聚合函数,其语法被封装在aggs中,而my_avg则是为查询结果起个别名,封装了计算出的平均值。那么,要以什么属性作为条件呢?是age年龄,查年龄的什么呢?是avg,查平均年龄。
如果只想看输出的值,而不关心输出的文档的话可以通过size=0来控制

GET zhifou/doc/_search
{
“query”: {
“match”: {
“from”: “gu”
}
},
“aggs”:{
“my_avg”:{
“avg”: {
“field”: “age”
}
}
},
“size”:0,
“_source”:[“name”,“age”]
}

查询结果
{
“took” : 35,
“timed_out” : false,
“_shards” : {
“total” : 5,
“successful” : 5,
“skipped” : 0,
“failed” : 0
},
“hits” : {
“total” : 3,
“max_score” : 0.0,
“hits” : [ ]
},
“aggregations” : {
“my_avg” : {
“value” : 27.0
}
}
}

  • 需求2、查询年龄的最大值

GET zhifou/doc/_search
{
“query”: {
“match_all”: {}
},
“aggs”: {
“my_max”: {
“max”: {
“field”: “age”
}
}
},
“size”: 0,
“_source”: [“name”,“age”,“from”]
}

查询结果
{
“took” : 10,
“timed_out” : false,
“_shards” : {
“total” : 5,
“successful” : 5,
“skipped” : 0,
“failed” : 0
},
“hits” : {
“total” : 5,
“max_score” : 0.0,
“hits” : [ ]
},
“aggregations” : {
“my_max” : {
“value” : 30.0
}
}
}

  • 需求3、查询年龄的最小值

GET zhifou/doc/_search
{
“query”: {
“match_all”: {}
},
“aggs”: {
“my_min”: {
“min”: {
“field”: “age”
}
}
},
“size”: 0,
“_source”: [“name”,“age”,“from”]
}

查询结果
{
“took” : 2,
“timed_out” : false,
“_shards” : {
“total” : 5,
“successful” : 5,
“skipped” : 0,
“failed” : 0
},
“hits” : {
“total” : 5,
“max_score” : 0.0,
“hits” : [ ]
},
“aggregations” : {
“my_min” : {
“value” : 18.0
}
}
}

  • 需求4、查询符合条件的年龄之和

GET zhifou/doc/_search
{
“query”: {
“match”: {
“from”: “gu”
}
},
“aggs”: {
“my_sum”: {
“sum”: {
“field”: “age”
}
}
},
“size”: 0,
“_source”: [“name”,“age”,“from”]
}

查询结果
{
“took” : 4,
“timed_out” : false,
“_shards” : {
“total” : 5,
“successful” : 5,
“skipped” : 0,
“failed” : 0
},
“hits” : {
“total” : 3,
“max_score” : 0.0,
“hits” : [ ]
},
“aggregations” : {
“my_sum” : {
“value” : 81.0
}
}
}

分组查询

  • 需求: 要查询所有人的年龄段,并且按照1520,2025,25~30分组,并且算出每组的平均年龄。

GET zhifou/doc/_search
{
“size”: 0,
“query”: {
“match_all”: {}
},
“aggs”: {
“age_group”: {
“range”: {
“field”: “age”,
“ranges”: [
{
“from”: 15,
“to”: 20
},
{
“from”: 20,
“to”: 25
},
{
“from”: 25,
“to”: 30
}
]
}
}
}
}

查询结果
{
“took” : 9,
“timed_out” : false,
“_shards” : {
“total” : 5,
“successful” : 5,
“skipped” : 0,
“failed” : 0
},
“hits” : {
“total” : 5,
“max_score” : 0.0,
“hits” : [ ]
},
“aggregations” : {
“age_group” : {
“buckets” : [
{
“key” : “15.0-20.0”,
“from” : 15.0,
“to” : 20.0,
“doc_count” : 1
},
{
“key” : “20.0-25.0”,
“from” : 20.0,
“to” : 25.0,
“doc_count” : 1
},
{
“key” : “25.0-30.0”,
“from” : 25.0,
“to” : 30.0,
“doc_count” : 2
}
]
}
}
}

上例中,在aggs的自定义别名age_group中,使用range来做分组,field是以age为分组,分组使用ranges来做,from和to是范围

  • 接下来,我们就要对每个小组内的数据做平均年龄处理。

GET zhifou/doc/_search
{
“size”: 0,
“query”: {
“match_all”: {}
},
“aggs”: {
“age_group”: {
“range”: {
“field”: “age”,
“ranges”: [
{
“from”: 15,
“to”: 20
},
{
“from”: 20,
“to”: 25
},
{
“from”: 25,
“to”: 30
}
]
},
“aggs”: {
“my_avg”: {
“avg”: {
“field”: “age”
}
}
}
}
}
}

查询结果
{
“took” : 1,
“timed_out” : false,
“_shards” : {
“total” : 5,
“successful” : 5,
“skipped” : 0,
“failed” : 0
},
“hits” : {
“total” : 5,
“max_score” : 0.0,
“hits” : [ ]
},
“aggregations” : {
“age_group” : {
“buckets” : [
{
“key” : “15.0-20.0”,
“from” : 15.0,
“to” : 20.0,
“doc_count” : 1,
“my_avg” : {
“value” : 18.0
}
},
{
“key” : “20.0-25.0”,
“from” : 20.0,
“to” : 25.0,
“doc_count” : 1,
“my_avg” : {
“value” : 22.0
}
},
{
“key” : “25.0-30.0”,
“from” : 25.0,
“to” : 30.0,
“doc_count” : 2,
“my_avg” : {
“value” : 27.0
}
}
]
}
}
}

ES的聚合查询的总结:聚合函数的使用,一定是先查出结果,然后对结果使用聚合函数做处理

avg:求平均

max:最大值

min:最小值

sum:求和

mapping

mapping 是什么

映射,用于定义一个文档及其中的字段如何存储和索引

字段的数据类型

简单类型如文本(text)、关键字(keyword)、日期(data)、整形(long)、双精度>、(double)、布尔(boolean)或 ip。
可以是支持 JSON 的层次结构性质的类型,如对象或嵌套。
或者一种特殊类型,如 geo_point、geo_shape 或 completion。为了不同的目的,
以不同的方式索引相同的字段通常是有用的。例如,字符串字段可以作为全文搜索的文本字段进行索引,
也可以作为排序或聚合的关键字字段进行索引。或者,可以使用标准分析器、英语分析器和
法语分析器索引字符串字段。这就是多字段的目的。大多数数据类型通过 fields 参数支持多字段。

mappings 之 dynamic 的三种状态

ES 中的 mapping 用来定义索引中字段和字段属性。mapping 中的 dynamic 属性决定了新增字段的处理方式,它有三种状态:

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数大数据工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年大数据全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上大数据开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加VX:vip204888 (备注大数据获取)
img

ES的聚合查询的总结:聚合函数的使用,一定是先查出结果,然后对结果使用聚合函数做处理

avg:求平均

max:最大值

min:最小值

sum:求和

mapping

mapping 是什么

映射,用于定义一个文档及其中的字段如何存储和索引

字段的数据类型

简单类型如文本(text)、关键字(keyword)、日期(data)、整形(long)、双精度>、(double)、布尔(boolean)或 ip。
可以是支持 JSON 的层次结构性质的类型,如对象或嵌套。
或者一种特殊类型,如 geo_point、geo_shape 或 completion。为了不同的目的,
以不同的方式索引相同的字段通常是有用的。例如,字符串字段可以作为全文搜索的文本字段进行索引,
也可以作为排序或聚合的关键字字段进行索引。或者,可以使用标准分析器、英语分析器和
法语分析器索引字符串字段。这就是多字段的目的。大多数数据类型通过 fields 参数支持多字段。

mappings 之 dynamic 的三种状态

ES 中的 mapping 用来定义索引中字段和字段属性。mapping 中的 dynamic 属性决定了新增字段的处理方式,它有三种状态:

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数大数据工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年大数据全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
[外链图片转存中…(img-renxf2Ty-1712516947716)]
[外链图片转存中…(img-At1AIRlC-1712516947716)]
[外链图片转存中…(img-TxMqdiCp-1712516947717)]
[外链图片转存中…(img-LhLbjvgh-1712516947717)]
[外链图片转存中…(img-7By2eV0q-1712516947717)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上大数据开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加VX:vip204888 (备注大数据获取)
[外链图片转存中…(img-GI0IRQeb-1712516947718)]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值