Elasticsearch基本查询语法

先造点数据

基于elasticsearch7.10.0

POST /hotel
{
	"mappings":{
		"properties":{
			"title":{
				"type":"text"
			},
			"city":{
				"type":"keyword"
			},
			"price":{
				"type":"double"
			},
			"create_time":{
				"type":"date",
				"format":"yyyy-MM-dd HH:mm:ss"
			},
			"amenities":{
				"type":"text"
			},
			"full_room":{
				"type":"boolean"
			},
			"location":{
				"type":"geo_point"
			},
			"prarse":{
				"type":"integer"
			}
		}
		
	}
}


POST /_bulk
{"index":{"_index":"hotel","_id":"001"}}
{"title":"文雅酒店","city":"青岛","price":556.00,"create_time":"2020-04-18 12:00:00","amenities":"浴池,普通停车场/充电停车场","full_room":false,"location":{"lat":36.083078,"lon":120.37566}}

{"index":{"_index":"hotel","_id":"002"}}
{"title":"金都嘉义假日酒店","city":"北京","price":337.00,"create_time":"2020-04-18 12:00:00","amenities":"浴池,普通停车场/充电停车场","full_room":false,"location":{"lat":39.083078,"lon":116.37566}}

{"index":{"_index":"hotel","_id":"003"}}
{"title":"金都欣欣酒店","city":"天津","price":200.00,"create_time":"2020-04-18 12:00:00","amenities":"浴池,普通停车场/充电停车场","full_room":false,"location":{"lat":36.083078,"lon":120.37566}}

{"index":{"_index":"hotel","_id":"004"}}
{"title":"金都酒店","city":"北京","price":500.00,"create_time":"2020-04-18 12:00:00","amenities":"浴池,普通停车场/充电停车场","full_room":false,"location":{"lat":36.083078,"lon":120.37566}}

{"index":{"_index":"hotel","_id":"005"}}
{"title":"文雅精选酒店","city":"北京","price":800.00,"create_time":"2020-04-18 12:00:00","amenities":"浴池,普通停车场/充电停车场","full_room":false,"location":{"lat":36.083078,"lon":120.37566}}

查询返回指定字段

POST /hotel/_search
{
	"_source":["title","city"],
	"query":{
		"term":{
			"city":{
				"value":"北京"
			}
		}
	}
}

计数


POST /hotel/_count

{
	"query":{
		"term":{
			"city":{
				"value":"北京"
			}
		}
	}
}

结果分页


POST hotel/_search

{
	"from":1,
	"size":2,
	"query":{
		"term":{
			"city":{
				"value":"北京"
			}
		}
	}
}

查询所有文档

POST hotel/_search
{
	"_source":["title","city"],
	"query":{
		"match_all":{
			"boost":2.0 //设定所有分为2.0
		}
	}
}

term完全匹配查询


POST hotel/_search

{
	"query":{
		"term":{
			"${FIELD}":{	//FIELD的数据类型可以是 数值 布尔 日期 数组及关键字等
				"value":"${VALUE}"
			}
		}
	}
}

terms 用于查询一个或多个值是否完全匹配,或的关系

POST hotel/_search

{
	"query":{
		"terms":{
			"${FIELD}":[	//FIELD的数据类型可以是 数值 布尔 日期 数组及关键字等
				"${VALUE1}","${VALUE2}"
			]
		}
	}
}

range 范围查询

POST hotel/_search

{
	"query":{
		"range":{
			"price":{	//一般是数值和日期类型
				"gte",300,
				"lte",500
			}
		}
	}
}

exists查询


POST hotel/_search

{
	"query":{
		"exists":{ //查询指定字段不为空的文档
			"field":"price"
		}
	}
}

布尔查询

must:必须匹配该查询条件,相当于逻辑查询中的“与”
should:可以匹配该查询条件,相当于逻辑查询中的“或”
must not必须不匹配该查询条件,相当于逻辑查询中的“非”
filter:必须匹配过滤条件,不进行打分计算

城市为北京,且价格在350-400之间

POST hotel/_search
{
	"query":{
		"bool":{
			"must":[ 
			{
				"term":{
					"city":{
						"value":"北京"
					}
				}
			},
			{
				"range":{
					"price":{
						"gte":350,
						"lte":400
					}
				}
			}	
			]
		}
	}
}

查询城市为北京或天津的酒店

POST hotel/_search
{
	"query":{
		"bool":{
			"should":[
				{
					"term":{
						"city":{
							"value":"北京"
						}
					}	
				},
				{
					"term":{
						"city":{
							"value":"天津"
						}
					}	
				}
				
			]
		}
	}
}

查询城市不是北京也不是天津的酒店

POST hotel/_search
{
	"query":{
		"bool":{
			"must_not":[
				{
					"term":{
						"city":{
							"value":"北京"
						}
					}	
				},
				{
					"term":{
						"city":{
							"value":"天津"
						}
					}	
				}
				
			]
		}
	}
}

查询城市为北京且不满房的酒店,不打分

POST hotel/_search
{
	"query":{
		"bool":{
			"filter":[
				{
					"term":{
						"city":{
							"value":"北京"
						}
					}	
				},
				{
					"term":{
						"full_room":false
					}	
				}
				
			]
		}
	}
}

全文搜索(文本类型text的数据)

match查询

POST hotel/_search
{
	"query":{
		"match":{
			"title":{
				"query":"金都酒店",
				"operator":"and"  //查询词之间的匹配结果为”与“的关系
			}
		}
	}
}

multi_match多字段查询


POST hotel/_search
{
	"query":{
		"multi_match":{
			"query":"假日",
			"fields":[
				"title",
				"amenties"
			]
		}
	}
}

geo_point地理点查询

对应于geo_point字段类型的查询方式有3种,分别为geo_distance查询、geo_bounding_box查询和geo_polygon查询

经纬度(39.91513,116.4039)附近5km的酒店

POST hotel/_search
{
	"query":{
		"geo_distance":{
			"distance":"5km",	
			"location":{
				"lat":"39.91513",
				"lon":"116.4039"
			}
		}
	}
}

geo_bounding_box查询提供的是矩形内的搜索,需要用户给出左上角的顶点地理坐标和右下角的顶点地理坐标。
假设定义国贸商圈为一个矩形,其左上角顶点的经纬度为[116.457044,39.922821],
右下角顶点的经纬度为[116.479466,39.907104],则在国贸商圈内搜索酒店的DSL如下:


POST hotel/_search
{
	"query":{
		"geo_bounding_box":{	
			"location":{
				"top_left":{
					"lat":"39.922821",
					"lon":"116.457004"
				},
				"bottom_right":{
					"lat":"39.907104",
					"lon":"116.479466"
				}
			}
		}
	}
}

geo_polygon查询为多边形跟geo_bounding_box差不多。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jwt_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值