查询所有:查询出所有数据,一般测试用。例如:match_all
全文检索(full text)查询:利用分词器对用户输入内容分词,然后去倒排索引库中匹配。例如:
精确查询:根据精确词条值查找数据,一般是查找keyword、数值、日期、boolean等类型字段。例如:
地理(geo)查询:根据经纬度查询。例如:
复合(compound)查询:复合查询可以将上述各种查询条件组合起来,合并查询条件。例如:
本文根据张虎翼老师的SpringCloud中ES的案例进行举例
查询索引库中文档的语句如下
GET /hotel/_search
{
"query": {
"match_all": {}
}
}
GET /hotel/_search
{
"query": {
"match": {
"name": "如家"
}
}
}
GET /hotel/_search
{
"query": {
"multi_match": {
"query": "外滩如家",
"fields": ["brand","name","business"]
}
}
}
GET /hotel/_search
{
"query": {
"term": {
"city.keyword": {
"value": "北京"
}
}
}
}
gte 大于等于 lte 小于等于
GET /hotel/_search
{
"query": {
"range": {
"price": {
"gte": 100,
"lte": 500
}
}
}
}
GET /hotel/_search
{
"query": {
"geo_distance": {
"distance": "2km",
"location": "31.21, 121.5"
}
}
}
GET /hotel/_search
{
"query": {
"function_score": {
"query": {
"match": {
"all": "外滩"
}
},
"functions": [
{
"filter": {
"term": {
"brand": "如家"
}
},
"weight": 10
}
],
"boost_mode": "sum"
}
}
}
Get /hotel/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "如家"
}
}
],
"must_not": [
{
"range": {
"price": {
"gt": 400
}
}
}
],
"filter": {
"geo_distance": {
"distance": "10km",
"location": {
"lat": 31.24,
"lon": 121.5
}
}
}
}
}
}
找到"39.76875, 116.339199"附近的酒店
GET /hotel2/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"_geo_distance": {
"location": {
"lat": 39.76,
"lon": 116.34
},
"order": "asc",
"unit": "km"
}
}
]
}
GET /hotel2/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"price": {
"order": "asc"
}
}
],
"from": 0,
"size": 5
}
GET /hotel2/_search
{
"query": {
"match": {
"all": "如家"
}
},
"highlight": {
"fields": {
"name": {
"require_field_match": "false"
}
}
}
}
GET /hotel2/_search
{
"query": {
"match": {
"all": "如家"
}
},
"from": 0,
"size": 10,
"sort": [
{
"price": {
"order": "asc"
},
"_geo_distance": {
"location": {
"lat": 40.04,
"lon": 116.33
},
"order": "asc",
"unit": "km"
}
}
],
"highlight": {
"fields": {
"name": {
"require_field_match": "false"
}
}
}
}