这是我参与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"
}
复制代码