DSL查询
1.查询所有文档
POST /my_index/_search
{
"query": {
"match_all": {}
}
}
2.匹配查询(match)
POST /my_index/_search
{
"query": {
"match": {
"title": "华为智能手机"
}
}
}
3.多字段匹配
POST /my_index/_search
{
"query": {
"multi_match": {
"query": "华为智能手机",
"fields": ["title","brand"]
}
}
}
4 关键字精确查询
POST /my_index/_search
{
"query": {
"term": {
"title": {
"value": "华为手机"
}
}
}
}
5.多关键字精确查询
POST /my_index/_search
{
"query": {
"terms": {
"title": [
"华为手机",
"华为"
]
}
}
}
6.范围查询
gte | lte | gt | lt |
---|---|---|---|
大于等于 | 小于等于 | 大于 | 小于 |
POST /my_index/_search
{
"query": {
"range": {
"price": {
"gte": 3000,
"lte": 5000
}
}
}
}
7.指定返回字段
query同级增加_source进行过滤
POST /my_index/_search
{
"query": {
"terms": {
"title": [
"华为手机",
"华为"
]
}
},
"_source": ["title","brand"]
}
8.组合查询
bool 各条件之间有and,or或not的关系
- must: 各个条件都必须满足,所有条件是and的关系
- should: 各个条件有一个满足即可,即各条件是or的关系
- must_not: 不满足所有条件,即各条件是not的关系
- filter: 与must效果等同,但是它不计算得分,效率更高点。
POST /my_index/_search
{
"query": {
"bool": {
"must": [ ☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆
{
"match": {
"title": "华为"
}
},
{
"range": {
"price": {
"gte": 3000,
"lte": 5400
}
}
}
]
}
}
}
结果:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.2923405, ☆☆☆"filter的时候为0.0"☆☆☆
"hits": [
{
"_index": "my_index",
"_id": "1",
"_score": 1.2923405,
"_source": {
"id": 1,
"title": "华为笔记本电脑",
"brand": "华为",
"images": "http://www.com/xm.jpg",
"price": 5388
}
}
]
}
}
如果should和must同时存在,他们之间是and关系:
POST /my_index/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"title": "华为"
}
},
{
"range": {
"price": {
"gte": 3000,
"lte": 5000
}
}
}
],
"must": [
{
"match": {
"title": "华为"
}
},
{
"range": {
"price": {
"gte": 3000,
"lte": 5000
}
}
}
]
}
}
}
9.聚合查询
select course_id,avg(score) from tab where … group by course_id
聚合允许使用者对es文档进行统计分析,类似与关系型数据库中的group by,当然还有很多其他的聚合,例如取最大值、平均值等等。
聚合三要素:聚合名称(给不同聚合业务其名称-用于解析结果)、聚合字段(对哪个字段进行分组)、聚合类型(如何聚合-常见:字段值相同放在一组)
max /min /avg /sum /stats(前四个)
POST /my_index/_search
{
"query": {
"match_all": {}
},
"size": 0,
"aggs": {
"max_price": {
"max": { ☆☆☆☆☆☆"聚合名称"☆☆☆☆☆☆
"field": "price" ☆☆☆☆☆☆"聚合字段"☆☆☆☆☆☆
}
}
}
}
结果:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"max_price" : {
"value" : 5500.0
}
}
}
/terms(桶聚合相当于sql中的group by语句)
POST /my_index/_search
{
"query": {
"match_all": {}
},
"size": 0,
"aggs": {
"groupby_brand": {
"terms": {
"field": "brand",
"size": 10
}
}
}
}
还可以对桶继续下钻:
POST /my_index/_search
{
"query": {
"match_all": {}
},
"size": 0,
"aggs": {
"groupby_brand": {
"terms": {
"field": "brand",
"size": 10
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
10.排序
POST /my_index/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "华为"
}
}
]
}
},
"sort": [
{
"price": {
"order": "asc"
}
},
{
"_score": {
"order": "desc"
}
}
]
}
11.分页查询
分页的两个关键属性:from、size。
- from: 当前页的起始索引,默认从0开始。 from = (pageNum - 1) * size
- size: 每页显示多少条
POST /my_index/_search
{
"query": {
"match_all": {}
},
"from": 0,
"size": 2
}
12.高亮
高亮三要素:
- 高亮字段
- 高亮前置标签(HTML标签)
- 高亮后置标签(HTML标签)
根据关键词查询商品
#高亮 必须要求用户录入关键字
GET my_index/_search
{
"query": {
"match": {
"title": "华为 手机"
}
},
"highlight": {
"fields": {"title": {}},
"pre_tags": "<font style='color:red'>",
"post_tags": "</font>"
}
}