插入测试查询数据,注意这里已安装ik中文分词器
PUT mytest
{
"mappings": {
"properties": {
"join_field": {
"type": "join",
"relations": {
"parent": "child"
}
},
"name": {
"type": "keyword"
},
"age": {
"type": "integer"
},
"city": {
"type": "text",
"analyzer": "ik_smart"
},
"hobbies":{
"type": "keyword"
},
"desc": {
"type": "text",
"analyzer": "ik_smart"
},
"family":{
"properties": {
"father":{
"type":"keyword"
},
"mather":{
"type":"keyword"
}
}
}
}
}
}
POST mytest/_bulk
{"index":{}}
{"name":"张三","age":"20","city":"广州","desc":"我是程序员,毕业于清华大学","hobbies":["唱歌","跳舞","rape"],"join_field": "parent"}
{"index":{}}
{"name":"李四","age":"27","city":"北京","desc":"我是宅男平时喜欢看动漫","hobbies":["动漫","cosplay","游戏"],"join_field": "parent"}
{"index":{}}
{"name":"王五","age":"31","city":"广州","desc":"我是快递员,每天都上门送快递","join": "parent","family":{"father":"王爸","mather":"王妈"}}
{"index":{}}
{"name":"小七","age":"40","city":"昆明","desc":"我是名医生,每天救死扶伤","join": "parent","family":{"father":"小爸","mather":"小妈"}}
// 子文档
POST /mytest/_doc/1?routing=1
{
"join_field": {
"name" : "child",
"parent": "BYELg4MBLEd9Buf4tGhT" // 父文档id
},
"friends": "tom"
}
POST /mytest/_doc/2?routing=1
{
"join_field": {
"name" : "child",
"parent": "BoELg4MBLEd9Buf4tGhT"// 父文档id
},
"friends": "king"
}
查询所有
GET /mytest/_doc/_search
{
"query":{
"match_all":{}
}
}
一、单条件查询
1、模糊匹配
1.1 match
模糊查询,类似数据库的like ‘%三%’,查询出名字带有三的数据
GET /mytest/_doc/_search
{
"query":{
"match":{
"name":"三"
}
}
}
1.2 prefix
前缀查询,查询前缀为王的数据
GET /mytest/_doc/_search
{
"query":{
"prefix":{
"name":"王"
}
}
}
1.3 regexp
正则匹配,使用正则表达式去匹配,例子是匹配年龄小于二十岁的
GET /mytest/_doc/_search
{
"query":{
"regexp":{
"age":"[0-2][0-9]"
}
}
}
2、精确匹配
2.1 term
精确匹配相当于=,需要与字段的值完全匹配,注意搜索的字段必须type=keyword,否则效果跟match一样
GET /mytest/_doc/_search
{
"query":{
"term":{
"name":"张三"
}
}
}
2.2 terms
精确匹配多个值
GET /mytest/_doc/_search
{
"query":{
"terms":{
"name":["张三","李四"]
}
}
}
2.3 range
字段属于某个范围内的值
GET /mytest/_doc/_search
{
"query":{
"range": {
"age": {
"gte": 20,
"lt": 30
}
}
}
}
2.4 exists
某个字段的值是否存在,相当于mysql的is not null
GET /mytest/_doc/_search
{
"query":{
"exists": {
"field":"hobbies"
}
}
}
二、多条件查询
1、bool 布尔查询
1.1 must
各个条件都必须满足,即各条件是and的 关系
GET /mytest/_doc/_search
{
"query":{
"bool": {
"must": [
{
"match":{
"city":"广州"
}
},
{ "range":{
"age":{
"lt":"30"
}
}
}
]
}
}
}
1.2 should
各个条件有一个满足即可,即各条件 是or的关系
GET /mytest/_doc/_search
{
"query":{
"bool": {
"should": [
{
"match":{
"city":"广州"
}
},
{ "range":{
"age":{
"lt":"30"
}
}
}
]
}
}
}
1.2 must_not
不满足所有条件,即各条件是not的 关系
GET /mytest/_doc/_search
{
"query":{
"bool": {
"must_not": [
{
"match":{
"city":"广州"
}
},
{ "range":{
"age":{
"lt":"30"
}
}
}
]
}
}
}
1.3 filter
不计算相关度评分,它不计算_score 即相关度评分,效率更高
GET /mytest/_doc/_search
{
"query":{
"bool": {
"filter": [
{
"match":{
"city":"广州"
}
},
{ "range":{
"age":{
"lt":"30"
}
}
}
]
}
}
}
2、constant_score
忽略TF/IDF打分,给搜索固定分数
GET /mytest/_doc/_search
{
"query":{
"bool": {
"should": [
{
"constant_score": {
"filter": { "match": { "desc": "wifi" }},
"boost": 2
}
},
{ "range":{
"age":{
"lt":"30"
}
}
}
]
}
}
}
3、取分数最高的,不考虑其他项的分数
取分数最高的,不考虑其他项的分数
GET /mytest/_doc/_search
{
"query":{
"dis_max": {
"queries": [
{ "match": { "city": "广州" }},
{ "match": { "desc": "我是" }}
]
}
}
}
三、链接查询
1、父子文档
查询子文档有某个字段
GET /mytest/_doc/_search
{
"query": {
"has_child": {
"type" : "child",
"query": {
"match": {
"friends": "tom"
}
}
}
}
}
2、嵌套文档
GET /mytest/_doc/_search
{
"query": {
"match": {
"family.father" : "王"
}
}
}
四、聚合
1、查询平均年龄
GET /mytest/_doc/_search
{
"query": {
"match_all": {
}
},
"aggs": {
"my-agg-name": {
"avg": {
"field": "age"
}
}
}
}
2、查询最小年龄
GET /mytest/_doc/_search
{
"query": {
"match_all": {
}
},
"aggs": {
"my-agg-name": {
"min": {
"field": "age"
}
}
}
}
3、最大年龄文章来源:https://www.toymoban.com/news/detail-411904.html
GET /mytest/_doc/_search
{
"query": {
"match_all": {
}
},
"aggs": {
"my-agg-name": {
"max": {
"field": "age"
}
}
}
}
4、统计总年龄文章来源地址https://www.toymoban.com/news/detail-411904.html
GET /mytest/_doc/_search
{
"query": {
"match_all": {
}
},
"aggs": {
"my-agg-name": {
"sum": {
"field": "age"
}
}
}
}
到了这里,关于es使用教程-常用搜索语法的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!