[toc]
實踐007-elasticsearch查詢之2-Request Body與DSL查詢
1. 查詢語句通過HTTP Request Body傳送給ES
POST movies/_search?ignore_unavailable=true
{
"profile": "true",
"query": {
"match_all": {}
}
}
2. 分頁:from+size
POST movies/_search?ignore_unavailable=true
{
"profile": "true",
"from": 0,
"size": 2,
"query": {
"match_all": {}
}
}
3. 排序:sort
POST movies/_search?ignore_unavailable=true
{
"profile": "true",
"from": 0,
"size": 5,
"sort": [
{
"year": "desc"
},
{
"title.keyword": "desc"
}
],
"query": {
"match_all": {}
}
}
4. source欄位過濾:只返回需要的列
POST movies/_search?ignore_unavailable=true
{
"profile": "true",
"_source": [
"id",
"title",
"year"
],
"from": 0,
"size": 5,
"sort": [
{
"year": "desc"
},
{
"title.keyword": "desc"
}
],
"query": {
"match_all": {}
}
}
支援萬用字元*: _"source": [ "name*", "desc\*" ]
5. script_fields指令碼欄位:自加工結果
POST movies/_search
{
"from": 0,
"size": 5,
"sort": [
{
"year": {
"order": "desc"
}
},
{
"title.keyword": {
"order": "desc"
}
}
],
"script_fields": {
"year_and_title": {
"script": {
"lang": "painless",
"source": "doc['year'].value + '->' + doc['title.keyword'].value"
}
}
},
"query": {
"match_all": {}
}
}
title=When We First Met; year=2018;
year_and_title=year->title
但是好像_source資料沒了:
{
"took" : 10,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 9743,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "187717",
"_score" : null,
"fields" : {
"year_and_title" : [
"2018->Won't You Be My Neighbor?"
]
},
"sort" : [
2018,
"Won't You Be My Neighbor?"
]
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "184015",
"_score" : null,
"fields" : {
"year_and_title" : [
"2018->When We First Met"
]
},
"sort" : [
2018,
"When We First Met"
]
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "184471",
"_score" : null,
"fields" : {
"year_and_title" : [
"2018->Tomb Raider"
]
},
"sort" : [
2018,
"Tomb Raider"
]
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "183959",
"_score" : null,
"fields" : {
"year_and_title" : [
"2018->Tom Segura: Disgraceful"
]
},
"sort" : [
2018,
"Tom Segura: Disgraceful"
]
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "188833",
"_score" : null,
"fields" : {
"year_and_title" : [
"2018->The Man Who Killed Don Quixote"
]
},
"sort" : [
2018,
"The Man Who Killed Don Quixote"
]
}
]
}
}
6. match查詢表示式: TermQuery
6.1 TermQuery
POST movies/_search
{
"query": {
"match": {
"title": "beautiful mind"
}
}
}
結果: 包含 beautiful 的(16條), 或 包含 mind 的(5條),共(20條):
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 20,
"relation" : "eq"
},
"max_score" : 13.687479,
"hits" : [
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "4995",
"_score" : 13.687479,
"_source" : {
"id" : "4995",
"genre" : [
"Drama",
"Romance"
],
"title" : "Beautiful Mind, A",
"@version" : "1",
"year" : 2001
}
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "3912",
"_score" : 8.723258,
"_source" : {
"id" : "3912",
"genre" : [
"Comedy",
"Drama"
],
"title" : "Beautiful",
"@version" : "1",
"year" : 2000
}
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "47404",
"_score" : 8.576847,
"_source" : {
"id" : "47404",
"genre" : [
"Adventure",
"Animation",
"Comedy",
"Fantasy",
"Romance",
"Sci-Fi"
],
"title" : "Mind Game",
"@version" : "1",
"year" : 2004
}
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "1046",
"_score" : 7.317063,
"_source" : {
"id" : "1046",
"genre" : [
"Drama",
"Romance"
],
"title" : "Beautiful Thing",
"@version" : "1",
"year" : 1996
}
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "94",
"_score" : 7.317063,
"_source" : {
"id" : "94",
"genre" : [
"Comedy",
"Drama",
"Romance"
],
"title" : "Beautiful Girls",
"@version" : "1",
"year" : 1996
}
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "4242",
"_score" : 7.317063,
"_source" : {
"id" : "4242",
"genre" : [
"Comedy",
"Crime",
"Drama",
"Thriller"
],
"title" : "Beautiful Creatures",
"@version" : "1",
"year" : 2000
}
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "4372",
"_score" : 7.317063,
"_source" : {
"id" : "4372",
"genre" : [
"Drama",
"Romance"
],
"title" : "Crazy/Beautiful",
"@version" : "1",
"year" : 2001
}
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "3302",
"_score" : 7.317063,
"_source" : {
"id" : "3302",
"genre" : [
"Comedy"
],
"title" : "Beautiful People",
"@version" : "1",
"year" : 1999
}
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "90353",
"_score" : 7.317063,
"_source" : {
"id" : "90353",
"genre" : [
"Drama"
],
"title" : "Beautiful Boy",
"@version" : "1",
"year" : 2010
}
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "100487",
"_score" : 7.317063,
"_source" : {
"id" : "100487",
"genre" : [
"Drama",
"Fantasy",
"Romance"
],
"title" : "Beautiful Creatures",
"@version" : "1",
"year" : 2013
}
}
]
}
}
6.2 TermQuery+AND
POST movies/_search
{
"query": {
"match": {
"title": {
"query": "beautiful mind",
"operator": "and"
}
}
}
}
- operator: and/or/not 對"query"的詞條做限制邏輯關係,加上
and
後,便只有一個記錄:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 13.687479,
"hits" : [
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "4995",
"_score" : 13.687479,
"_source" : {
"id" : "4995",
"genre" : [
"Drama",
"Romance"
],
"title" : "Beautiful Mind, A",
"@version" : "1",
"year" : 2001
}
}
]
}
}
7.match_phrase查詢表示式:PhraseQuery
7.1 PhraseQuery
POST movies/_search
{
"query": {
"match_phrase": {
"title": {
"query": "beautiful mind"
}
}
}
}
#// 或者下面形式都可
POST movies/_search
{
"query": {
"match_phrase": {
"title": "beautiful mind"
}
}
}
結果就只有一條, 因為是PhraseQuery: 既要求分詞,又要求順序;
7.2 PhraseQuery+slop
- slop /sl>p/ n.髒水,淡而無味的半流質食物;泔水
POST movies/_search
{
"query": {
"match_phrase": {
"title": {
"query": "friday part",
"slop": 2
}
}
}
}
slop=2, 就是允許 friday
和 part
中間 摻和 2個雜詞(不相關的任意詞)