DSL⭐
- 查询所有:默认match_all
- 全文检索:根据分词器规则分词后进行倒排索引匹配内容
- match_query:单个字段匹配
- multi_math_query:多个字段匹配
- 精确查询:一般根据
keyword
,数值
,日期
,布尔
类型查询- range:范围查询
- term:精确查询
- 地理查询
- geo_distance
- geo_bounding_box
- 复合查询
- bool
- function_score
- 搜集结果处理
- 排序
- 分页
- 高亮
查询所有
GET /hotel/_search
{
"query": {
"match_all": {}
}
}
全文检索
- match:相当于字段模糊查询
# match查询
GET /hotel/_search
{
"query": {
"match": {
"all": "如家"
}
}
}
GET /hotel/_search
{
"query": {
"match": {
"city": "上海"
}
}
}
- multi_mathc:查询关键词,对指定字段去检索关键词
GET /hotel/_search
{
"query": {
"multi_match": {
"query": "如家",
"fields": ["name","brand","business"]
}
}
}
精确查询
term
- 基本语法
GET /hotel/_search
{
"query": {
"term": {
"FIELD": {
"value": "VALUE"
}
}
}
}
- 示例
GET /hotel/_search
{
"query": {
"term": {
"city": {
"value": "上海"
}
}
}
}
range
- 基本语法
GET /hotel/_search
{
"query": {
"range": {
"FIELD": {
"gte": 10,
"lte": 20
}
}
}
}
- 示例
GET /hotel/_search
{
"query": {
"range": {
"price": {
"gte": 100,
"lte": 300
}
}
}
}
地理查询
geo_distance
- distance:距离,相当于半径
- location:当前你的地理位置
- 相当于就是以
location
为圆心,distance
为半径的圆内去匹配
GET /hotel/_search
{
"query": {
"geo_distance":{
"distance":"2km",
"location":"31.21, 121.5"
}
}
}
geo_bounding_box
- top_left:左上角地理位置
- bottom_right:右下角地理位置
- 相当于矩形内匹配
GET /hotel/_search
{
"query": {
"geo_bounding_box": {
"location": {
"top_left": {
"lat": 31.1,
"lon": 121.5
},
"bottom_right": {
"lat": 30.9,
"lon": 121.7
}
}
}
}
}
复合查询
function_score
- 基本语法
- query:原始查询条件
- functions:过滤条件
- boost_mode:加权模式(默认multiply):query得分和functions得分进行的操作
- min
- max
- avg
- sum
- multiply
- replace:functinos得分替换query得分
GET /hotel/_search
{
"query": {
"function_score": {
"query": {},
"functions": [
{}
],
"boost_mode":"multiply"
}
}
}
- 示例
GET /hotel/_search
{
"query": {
"function_score": {
"query": {
"match": {
"all": "外滩"
}
},
"functions": [
{
"filter": {
"term": {
"brand": "如家"
}
},
"weight": 10
}
],
"boost_mode": "multiply"
}
}
}
bool
- must:必须匹配
- should:选择性匹配
- must_not:必须不匹配,不参与算分
- filter:必须匹配,不参与算分
GET /hotel/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"city": "上海"
}
}
],
"should": [
{
"term": {
"brand": {
"value": "华美达"
}
}
}
],
"must_not": [
{
"range": {
"price": {
"lte": 500
}
}
}
],
"filter": [
{
"range": {
"score": {
"gte": 45
}
}
}
]
}
}
}
搜集结果处理
排序
GET /hotel/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"price": {
"order": "asc"
},
"score": {
"order": "desc"
}
}
]
}
分页
- size默认10页
GET /hotel/_search
{
"query": {
"match_all": {}
},
"from": 0,
"size": 20
}
高亮
- 如果匹配字段是
all
则需要设置require_field_match为false
,反之则不需要
GET /hotel/_search
{
"query": {
"match": {
"all": "如家"
}
},
"highlight": {
"fields": {
"name": {
"require_field_match": "false",
"pre_tags": "<h1>",
"post_tags": "</h1>"
}
}
}
}