栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Es的进阶检索(Query DSL)

Es的进阶检索(Query DSL)

GET bank/_search
{
  "query": {
    "match_all": {}
  },
  "from": 0,
  "size": 5,
  "sort": [
    {
      "account_number": {
        "order": "desc"
      },
      "balance": {
      	"order": "asc"
      }
    }
  ]
}

# match_all 查询类型【代表查询所有的所有】,es中可以在query中组合非常多的查询类型完成复杂查询;
# from+size 限定,完成分页功能;从第几条数据开始,每页有多少数据
# sort 排序,多字段排序,会在前序字段相等时后续字段内部排序,否则以前序为准;
GET bank/_search
{
  "query": {
    "match_all": {}
  },
  "from": 0,
  "size": 5,
  "sort": [
    {
      "account_number": {
        "order": "desc"
      }
    }
  ],
  "_source": ["balance","firstname"]
}

# _source 指定返回结果中包含的字段名
match匹配查询
GET bank/_search
{
  "query": {
    "match": {
      "account_number": 20
    }
  }
}
# 查找匹配 account_number 为 20 的数据 非文本推荐使用 term
模糊查询
GET bank/_search
{
  "query": {
    "match": {
      "address": "mill lane"
    }
  }
}
# 查找匹配 address 包含 mill 或 lane 的数据
精确查询
GET bank/_search
{
  "query": {
    "match": {
      "address.keyword": "288 Mill Street"
    }
  }
}
# 查找 address 为 288 Mill Street 的数据。
# 这里的查找是精确查找,只有完全匹配时才会查找出存在的记录,
# 如果想模糊查询应该使用match_phrase 短语匹配
match_phrase短语匹配
GET bank/_search
{
  "query": {
    "match_phrase": {
      "address": "mill lane"
    }
  }
}
GET bank/_search
{
  "query": {
    "match": {
      "name.keyword": "xx xxx"
    }
  }
}
每一个文本查询都可以加.keyword  代表不分词  直接匹配
# 这里会检索 address 匹配包含短语 mill lane 的数据 和address.keyword的区别是 phrase是包含这个短语即可 后者是必须是这个
multi_match多字段匹配
GET bank/_search
{
  "query": {
    "multi_match": {
      "query": "mill",
      "fields": [
        "city",
        "address"
      ]
    }
  }
}
# 检索 city 或 address 匹配包含 mill 的数据,会对查询条件分词
bool符合复合查询
GET bank/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "gender": "m"
        }
        },{
          "match": {
            "address": "mill"
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "age": "38"
          }
        }
      ],
      "should": [
        {
          "match": {
            "FIELD": "TEXT"
          }
        }
      ]
    }
  }
}
must:必须满足
must:必须不满足
should:最好是怎么样  可以不满足 满足只是会提高相关性得分
filter结果过滤
GET bank/_search
{
  "query": {
    "bool": {
      "must": [
        {"range": {
          "age": {
            "gte": 10,
            "lte": 20
          }
        }},
        {
          "match": {
            "addr": "mill"
          }
        }
      ]
    }
  }
}
range:满足区间  比如age在10-20之间同时还可以进行别的匹配
GET bank/_search
{
  "query": {
    "bool": {
      "filter": {
        "range": {
          "age": {
            "gte": 10,
            "lte": 20
          }
        }
      }
    }
  }
}
filter做过滤 而且不会计算相关性得分
term 数值检索
GET bank/_search
{
  "query": {
    "term": {
      "age": "12"
    }
  }
}
专门做确定数值检索,match专门查text 

aggregations(执行聚合)

聚合提供了从数据中分组和提取数据的能力。最简单的聚合方法大致等于 SQL GROUP
BY 和 SQL 聚合函数。在 Elasticsearch 中,您有执行搜索返回 hits(命中结果),并且同时返
回聚合结果,把一个响应中的所有 hits(命中结果)分隔开的能力。这是非常强大且有效的,
您可以执行查询和多个聚合,并且在一次使用中得到各自的(任何一个的)返回结果,使用
一次简洁和简化的 API 来避免网络往返。

搜索address中包含mill的所有人的年龄分布以及平均年龄
GET bank/_search
{
  "query": {
    "match": {
      "address": "mill"
    }}
  ,"aggs": {
    "ageAgg": {
      "terms": {
        "field": "age",
        "size": 10
      }
    },
    "ageAvg":{
      "avg": {
        "field": "age"
      }
    }
  }
}
先查询出地址 包含mill的 然后 agg进行聚合 
ageAgg(聚合别名自己起的)
terms 看值有多少种可能 
field看属性 size取出分析的可能数量
avg看平均值就可

//聚合可以无限写在aggs中
查出所有年龄分布,并且这些年龄段中的m的平均薪资和f的平均薪资以及这个年龄段的总体平均薪资
{
	"query": {
		"match_all": {}
	},
	"aggs": {
		"age_agg": {
			"terms": {
				"field": "age",
				"size": 100
			},
			"aggs": {
				"gender_agg": {
					"terms": {
						"field": "gender.keyword",
						"size": 100
					},
					"aggs": {
						"balance_avg": {
							"avg": {
								"field": "balance"
							}
						}
					}
				},
				"balance_avg": {
					"avg": {
						"field": "balance"
					}
				}
			}
		}
	},
	"size": 1000
}
先查出所有年龄段 然后对年龄段的性别进行统计聚合 然后根据这个算平均值  然后根据之前所有年龄段算整体平均值

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/748075.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号