Mysql和es概念对比
PUT /库名/_mapping
字段名必须是新字段名
GET /hotel/_search
{
"query": {
"match_all": {}
}
}
实际上是一种相关度的匹配
match
GET /hotel/_search
{
"query": {
"match": {
"all": "如家"
}
}
}
multi_match:与match查询相似,只不过允许同时擦汗寻多个字段
GET /hotel/_search
{
"query": {
"multi_match": {
"query": "外滩如家"
, "fields": ["brand","name","business"]
}
}
}
推荐使用match字段来查询
GET /hotel/_search
{
"query": {
"term": {
"city": {
"value":"上海"
}
}
}
}
GET /hotel/_search
{
"query": {
"range": {
"price": {
"gte": 100,
"lte": 300
}
}
}
}
# distance查询
GET /hotel/_search
{
"query": {
"geo_distance":{
"distance":"2km",
"location":"31.21,121.5"
}
}
}
适用于经纬度的信息
# function score查询
GET /hotel/_search
{
"query": {
"function_score": {
"query": {
"match": {
"all": "外滩"
}
}
, "functions": [
{
"filter": {
"term": {
"brand": "如家"
}
},
"weight": 10
}
]
, "boost_mode": "sum"
}
}
}
要求名字包含如家,价格不高于400的,在坐标31.21.121.5周围10km范围内的酒店
GET /hotel/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "如家"
}
}
],
"must_not": [
{
"range": {
"price": {
"gt": 400
}
}
}
],
"filter": [
{
"geo_distance": {
"distance": "10km",
"location": {
"lat": 31.21,
"lon": 121.5
}
}
}
]
}
}
}
对酒店数据按照用户评价降序排序,评价相同的按照价格升序排列
# sort 排序
GET /hotel/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"score": "desc"
},
{
"price": "asc"
}
]
}
找到周围的酒店
GET /hotel/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"_geo_distance": {
"location": {
"lat": 31.11111,
"lon": 121.11111
},
"order": "asc"
, "unit": "km"
}
}
]
}
# 分页查询
GET /hotel/_search
{
"query": {
"match_all": {}
},
"from": 10,
"size": 10,
"sort": [
{
"price": {
"order": "asc"
}
}
]
}
aggs代表数据的聚合
# 聚合功能,限制聚合范围
GET /hotel/_search
{
"query": {
"range": {
"price": {
"lte": 200
}
}
},
"size": 0,
"aggs":{
"brandAgg":{
"terms": {
"field": "brand",
"size": 20,
"order": {
"_count": "asc"
}
}
}
}
}
聚合的三要素
size指定聚合结果的数量
order指定聚合的排序方式
field指定聚合字段