這是我參與8月更文挑戰的第9天,活動詳情查看:8月更文挑戰html
若是❤️個人文章有幫助,歡迎點贊、關注。這是對我繼續技術創做最大的鼓勵。更多往期文章在個人我的專欄git
Elasticsearch 中查詢使用的 DSL(特定查詢語言) 相信你們都使用過. 除此以外 Elasticsearch 還能夠經過 uri 進行查詢條件指定.github
雖然功能不及DSL強大,但也不妨多多瞭解一下.bash
故事從一條查詢開始:markdown
GET /movies/_search?q=2012&df=title&sort=year:desc&from=0&size=10&timeout=1sapp
從上面能夠看出,包含如下參數:elasticsearch
# 基本查詢
GET /movies/_search?q=2012&df=title&sort=year:desc&from=0&size=10&timeout=1s
# 帶profile, 展現執行計劃
GET /movies/_search?q=2012&df=title
{
"profile":"true"
}
================== 返回結果 ==================
{
"took" : 76,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 4.1047716,
"hits" : [
{
"_index" : "tag",
"_type" : "_doc",
"_id" : "hFpZH3sBgpUFltoIW7vi",
"_score" : 4.1047716,
"_source" : {
"memberId" : 618,
"phone" : "15321761517",
"sex" : "1",
"channel" : 4,
"subOpenId" : "gcik-4nwr9lsy7pv",
"address" : 618,
"regTime" : "2019-11-22",
"orderCount" : 3,
"orderTime" : "2019-11-22",
"orderMoney" : 7546.0,
"favGoods" : [
2,
11,
16
],
"couponTimes" : [
"2019-11-25",
"2019-11-27",
"2019-11-13"
],
"chargeMoney" : 25.0,
"overTime" : 2100
}
}
]
},
"profile" : {
"shards" : [
{
"id" : "[0tHjuZtUQMeFyRCWlNgK4w][tag][0]",
"searches" : [
{
"query" : [
{
"type" : "TermQuery",
"description" : "subOpenId:gcik",
"time_in_nanos" : 237449,
"breakdown" : {
"score" : 0,
"build_scorer_count" : 8,
"match_count" : 0,
"create_weight" : 231939,
"next_doc" : 0,
"match" : 0,
"create_weight_count" : 1,
"next_doc_count" : 0,
"score_count" : 0,
"build_scorer" : 5501,
"advance" : 0,
"advance_count" : 0
}
}
],
"rewrite_time" : 3399,
"collector" : [
{
"name" : "CancellableCollector",
"reason" : "search_cancelled",
"time_in_nanos" : 81522,
"children" : [
{
"name" : "SimpleTopScoreDocCollector",
"reason" : "search_top_hits",
"time_in_nanos" : 64422
}
]
}
]
}
],
"aggregations" : [ ]
},
{
"id" : "[0tHjuZtUQMeFyRCWlNgK4w][tag][1]",
"searches" : [
{
"query" : [
......
],
"rewrite_time" : 3546,
"collector" : [
......
]
}
],
"aggregations" : [ ]
},
{
"id" : "[0tHjuZtUQMeFyRCWlNgK4w][tag][2]",
"searches" : [
{
"query" : [
......
}
],
"rewrite_time" : 4308,
"collector" : [
......
]
}
],
"aggregations" : [ ]
},
{
"id" : "[0tHjuZtUQMeFyRCWlNgK4w][tag][3]",
"searches" : [
{
"query" : [
......
],
"rewrite_time" : 3412,
"collector" : [
......
]
}
],
"aggregations" : [ ]
},
{
"id" : "[0tHjuZtUQMeFyRCWlNgK4w][tag][4]",
"searches" : [
{
"query" : [
......
],
"rewrite_time" : 22285,
"collector" : [
......
]
}
]
}
],
"aggregations" : [ ]
}
]
}
}
複製代碼
# 泛查詢,正對_all,全部字段
GET /movies/_search?q=2012
{
"profile":"true"
}
# 指定字段
GET /movies/_search?q=title:2012&sort=year:desc&from=0&size=10&timeout=1s
{
"profile":"true"
}
# 查找美麗心靈, Mind爲泛查詢
GET /movies/_search?q=title:Beautiful Mind
{
"profile":"true"
}
# 泛查詢
GET /movies/_search?q=title:2012
{
"profile":"true"
}
# 使用引號,Phrase查詢
GET /movies/_search?q=title:"Beautiful Mind"
{
"profile":"true"
}
# 分組,Bool查詢
GET /movies/_search?q=title:(Beautiful Mind)
{
"profile":"true"
}
# 布爾操做符
# 查找美麗心靈
GET /movies/_search?q=title:(Beautiful AND Mind)
{
"profile":"true"
}
# 查找美麗心靈
GET /movies/_search?q=title:(Beautiful NOT Mind)
{
"profile":"true"
}
# 查找美麗心靈
GET /movies/_search?q=title:(Beautiful %2BMind)
{
"profile":"true"
}
# 範圍查詢 ,區間寫法
GET /movies/_search?q=title:beautiful AND year:[2002 TO 2018%7D
{
"profile":"true"
}
# 通配符查詢
GET /movies/_search?q=title:b*
{
"profile":"true"
}
# 模糊匹配&近似度匹配
GET /movies/_search?q=title:beautifl~1
{
"profile":"true"
}
GET /movies/_search?q=title:"Lord Rings"~2
{
"profile":"true"
}
複製代碼