logo头像

待到风起时,扬帆济沧海

elasticsearch query 语法

本文于304天之前发表,文中内容可能已经过时。

SQL语法

查询所有

GET /Product/cpu/_search

1
2
3
{
"query": { "match_all": {} }
}

单条件匹配和排序

GET /Product/cpu/_search

1
2
3
4
5
6
7
8
9
10
{
"query": {
"match": {
"name":"amd"
}
},
"sort":[
{"price":"desc"}
]
}

查询指定的字段

GET /Product/cpu/_search

1
2
3
4
{
"query": { "match_all": {} },
"_source":["name","price"]
}

filter数据过滤

GET /Product/cpu/_search
例如:cpu名称包含amd,且价格大于1000

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
"query": {
"bool": {
"must":{
"match":{
"name": "amd"
}
},
"filter":{
"range":{
"price": {"gt":1000}
}
}
}
}
}

full-text search 全文检索

GET /Product/cpu/_search
例如:desc字段内容去被倒排索引一一检索,包含任意词条的数据会被反驳

1
2
3
4
5
6
7
{
"query": {
"match": {
"desc": "cpu chaopin pianyi"
}
}
}

phrase search 短语检索

GET /Product/cpu/_search
例如:desc字段内容必须全匹配短语chaopin pianyi

1
2
3
4
5
6
7
{
"query": {
"match_phrase": {
"desc": "chaopin pianyi"
}
}
}

highlight search 高亮检索

GET /Product/cpu/_search
例如:desc字段内容必须全匹配短语chaopin pianyi

1
2
3
4
5
6
7
{
"query": {
"match_phrase": {
"desc": "chaopin pianyi"
}
}
}

复合查询

aggs 聚合查询

GET /Product/cpu/_search

1
2
3
4
5
6
7
8
9
{
"aggs": {
"demo_tags": {
"terms": {
"field": "tag"
}
}
}
}

设置字段为true
PUT /product/cpu/_mapping

1
2
3
4
5
6
7
8
{
"properties": {
"tag":{
"type": "text",
"fielddata": true
}
}
}

aggs 聚合分组求平均

GET /Product/cpu/_search

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
"aggs": {
"demo_tags": {
"terms": {
"field": "tag"
},
"aggs": {
"avg_demo_price": {
"avg": {
"field": "price"
}
}
}
}
}
}

aggs 聚合分组求平均后排序

GET /Product/cpu/_search

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
"size": 0,
"aggs": {
"demo_tags": {
"terms": {
"field": "tag",
"order": {
"avg_price": "desc"
}
},
"aggs": {
"avg_demo_price": {
"avg": {
"field": "price"
}
}
}
}
}
}

aggs 范围区间聚合排序

GET /Product/cpu/_search

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{
"size": 0,
"aggs": {
"group_tags": {
"range": {
"field": "",
"ranges": [
{
"from": 50,
"to": 100
}
]
},
"aggs": {
"NAME": {
"AGG_TYPE": {}
}
}
}
}
}

评论系统未开启,无法评论!