1. 程式人生 > 其它 >ES elasticsearch should失效及正確寫法

ES elasticsearch should失效及正確寫法

##預設資料

POST  test/_doc/1
{
"name":"張三",
"sex":"",
"score":70
}
POST  test/_doc/2
{
"name":"李四",
"sex":"",
"score":70
}
POST  test/_doc/3
{
"name":"王五",
"sex":"",
"score":80
}

## 查詢 男性並且成績是70或者80的人

ES7之前失效,7之後的版本支援該寫法

GET test/_search
{
"query": {
"bool": {
"must": [
{"term": {
"sex": {
"value":"男"
}
}}
],
"should": [
{"term": {
"score": {
"value": 70
}
}},
{"term": {
"score": {
"value": 80
}
}}
]
}
}
}

##第二種寫法

GET test/_search
{
"query": {
"bool": {
"must": [
{"term": {
"sex": {
"value": "男"
}
}},
{
"bool": {
"should": [
{"term": {
"score": {
"value": 70
}
}},
{"term": {
"score": {
"value": 80
}
}}
]
}
}
]

}
}
}

螃蟹在剝我的殼,筆記本在寫我,漫天的我落在楓葉上雪花上,而你在想我。 --章懷柔