> 文章列表 > elasticsearch DSL Query

elasticsearch DSL Query

elasticsearch DSL Query

在前一篇中,我们学习了uri query的用法,接下来我们开始学习es的DSL Query的使用。
DSL表示的是domain-specific language,即领域特定语言的意思,详细的解释可以参考官方网站的
DSL官方解释
在这里我还要强调一遍,DSL其实就是一种查询的方式(相对于URI查询来讲),后面的我们的其他种类的查询,比如复合查询,join查询,全文检索等都是基于DSL来查询的。
其中ES查询方式基本就分为两种,一种就是URI查询,另一种就是DSL查询,URI查询可以满足我们基本的一些查询需求,但是对于很高级的查询需求就不能满足了,此时就只能使用DSL查询了。比如刚刚提到的符合查询,join查询,地理信息查询等等,这些查询都是在DSL查询基础上实现的。
甚至连URI查询其实都可以转化为DSL形式。如下例子所示:

GET bank/_search
{"query": {"query_string": {"fields": ["firstname"], "query": "Dena"}}
}

DSL查询的基本形式

一个查询语句的典型结构

{QUERY_NAME: {ARGUMENT: VALUE,ARGUMENT: VALUE,...}
}

如果是针对某个字段,那么它的结构如下:

{QUERY_NAME: {FIELD_NAME: {ARGUMENT: VALUE,ARGUMENT: VALUE,...}}
}

下面我们将介绍Query Context和Filter Context,并且我们也将利用刚刚讲述过的DSL的结构来学习一下Query Context和Filter Contex。当然官方有更详细的解释区别
本质区别就是query context的查询不仅需要值匹配,还要进行相关度打分,而filter context则不需要打分直接进行值匹配。所以filter context类型的查询,效率相对要高一些。

Query Context

例子例子,我们学习的镜子,
举个栗子:

GET bank/_search
{"query": {"match": {"firstname": "Hattie"}},"profile": "true"
}

结果:

{"took" : 1,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 1,"relation" : "eq"},"max_score" : 6.5042877,"hits" : [{"_index" : "bank","_type" : "account","_id" : "6","_score" : 6.5042877,"_source" : {"account_number" : 6,"balance" : 5686,"firstname" : "Hattie","lastname" : "Bond","age" : 36,"gender" : "M","address" : "671 Bristol Street","employer" : "Netagy","email" : "hattiebond@netagy.com","city" : "Dante","state" : "TN"}}]},"profile" : {"shards" : [{"id" : "[jW8PbSdhTOOpESX13DRBJQ][bank][0]","searches" : [{"query" : [{"type" : "TermQuery","description" : "firstname:hattie","time_in_nanos" : 50956,"breakdown" : {"set_min_competitive_score_count" : 0,"match_count" : 0,"shallow_advance_count" : 0,"set_min_competitive_score" : 0,"next_doc" : 1193,"match" : 0,"next_doc_count" : 1,"score_count" : 1,"compute_max_score_count" : 0,"compute_max_score" : 0,"advance" : 994,"advance_count" : 1,"score" : 2075,"build_scorer_count" : 3,"create_weight" : 35506,"shallow_advance" : 0,"create_weight_count" : 1,"build_scorer" : 11188}}],"rewrite_time" : 1362,"collector" : [{"name" : "SimpleTopScoreDocCollector","reason" : "search_top_hits","time_in_nanos" : 9461}]}],"aggregations" : [ ]}]}
}

从结果中我们可以看到,
“relation” : “eq”
“max_score” : 6.5042877
“_score” : 6.5042877
说明当前查询就是首先通过值是否匹配,然后再计算分数的,其实查询如果有多个值,就会按照这个分数进行排序。

Filter Context

查询语句:

GET bank/_search
{"query": {"constant_score": {"filter": {"range": {"age": {"gte": 27}}}}},"profile": "true"
}

返回结果:

{"took" : 2,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 674,"relation" : "eq"},"max_score" : 1.0,"hits" : [{"_index" : "bank","_type" : "account","_id" : "6","_score" : 1.0,"_source" : {"account_number" : 6,"balance" : 5686,"firstname" : "Hattie","lastname" : "Bond","age" : 36,"gender" : "M","address" : "671 Bristol Street","employer" : "Netagy","email" : "hattiebond@netagy.com","city" : "Dante","state" : "TN"}},{"_index" : "bank","_type" : "account","_id" : "13","_score" : 1.0,"_source" : {"account_number" : 13,"balance" : 32838,"firstname" : "Nanette","lastname" : "Bates","age" : 28,"gender" : "F","address" : "789 Madison Street","employer" : "Quility","email" : "nanettebates@quility.com","city" : "Nogal","state" : "VA"}},{"_index" : "bank","_type" : "account","_id" : "18","_score" : 1.0,"_source" : {"account_number" : 18,"balance" : 4180,"firstname" : "Dale","lastname" : "Adams","age" : 33,"gender" : "M","address" : "467 Hutchinson Court","employer" : "Boink","email" : "daleadams@boink.com","city" : "Orick","state" : "MD"}},{"_index" : "bank","_type" : "account","_id" : "20","_score" : 1.0,"_source" : {"account_number" : 20,"balance" : 16418,"firstname" : "Elinor","lastname" : "Ratliff","age" : 36,"gender" : "M","address" : "282 Kings Place","employer" : "Scentric","email" : "elinorratliff@scentric.com","city" : "Ribera","state" : "WA"}},{"_index" : "bank","_type" : "account","_id" : "25","_score" : 1.0,"_source" : {"account_number" : 25,"balance" : 40540,"firstname" : "Virginia","lastname" : "Ayala","age" : 39,"gender" : "F","address" : "171 Putnam Avenue","employer" : "Filodyne","email" : "virginiaayala@filodyne.com","city" : "Nicholson","state" : "PA"}},{"_index" : "bank","_type" : "account","_id" : "32","_score" : 1.0,"_source" : {"account_number" : 32,"balance" : 48086,"firstname" : "Dillard","lastname" : "Mcpherson","age" : 34,"gender" : "F","address" : "702 Quentin Street","employer" : "Quailcom","email" : "dillardmcpherson@quailcom.com","city" : "Veguita","state" : "IN"}},{"_index" : "bank","_type" : "account","_id" : "37","_score" : 1.0,"_source" : {"account_number" : 37,"balance" : 18612,"firstname" : "Mcgee","lastname" : "Mooney","age" : 39,"gender" : "M","address" : "826 Fillmore Place","employer" : "Reversus","email" : "mcgeemooney@reversus.com","city" : "Tooleville","state" : "OK"}},{"_index" : "bank","_type" : "account","_id" : "44","_score" : 1.0,"_source" : {"account_number" : 44,"balance" : 34487,"firstname" : "Aurelia","lastname" : "Harding","age" : 37,"gender" : "M","address" : "502 Baycliff Terrace","employer" : "Orbalix","email" : "aureliaharding@orbalix.com","city" : "Yardville","state" : "DE"}},{"_index" : "bank","_type" : "account","_id" : "51","_score" : 1.0,"_source" : {"account_number" : 51,"balance" : 14097,"firstname" : "Burton","lastname" : "Meyers","age" : 31,"gender" : "F","address" : "334 River Street","employer" : "Bezal","email" : "burtonmeyers@bezal.com","city" : "Jacksonburg","state" : "MO"}},{"_index" : "bank","_type" : "account","_id" : "56","_score" : 1.0,"_source" : {"account_number" : 56,"balance" : 14992,"firstname" : "Josie","lastname" : "Nelson","age" : 32,"gender" : "M","address" : "857 Tabor Court","employer" : "Emtrac","email" : "josienelson@emtrac.com","city" : "Sunnyside","state" : "UT"}}]},"profile" : {"shards" : [{"id" : "[jW8PbSdhTOOpESX13DRBJQ][bank][0]","searches" : [{"query" : [{"type" : "ConstantScoreQuery","description" : "ConstantScore(age:[27 TO 9223372036854775807])","time_in_nanos" : 660062,"breakdown" : {"set_min_competitive_score_count" : 0,"match_count" : 0,"shallow_advance_count" : 0,"set_min_competitive_score" : 0,"next_doc" : 381671,"match" : 0,"next_doc_count" : 675,"score_count" : 674,"compute_max_score_count" : 0,"compute_max_score" : 0,"advance" : 3214,"advance_count" : 2,"score" : 83932,"build_scorer_count" : 4,"create_weight" : 109558,"shallow_advance" : 0,"create_weight_count" : 1,"build_scorer" : 81687},"children" : [{"type" : "IndexOrDocValuesQuery","description" : "age:[27 TO 9223372036854775807]","time_in_nanos" : 156882,"breakdown" : {"set_min_competitive_score_count" : 0,"match_count" : 0,"shallow_advance_count" : 0,"set_min_competitive_score" : 0,"next_doc" : 109465,"match" : 0,"next_doc_count" : 675,"score_count" : 0,"compute_max_score_count" : 0,"compute_max_score" : 0,"advance" : 1909,"advance_count" : 2,"score" : 0,"build_scorer_count" : 4,"create_weight" : 14099,"shallow_advance" : 0,"create_weight_count" : 1,"build_scorer" : 31409}}]}],"rewrite_time" : 1689,"collector" : [{"name" : "SimpleTopScoreDocCollector","reason" : "search_top_hits","time_in_nanos" : 251329}]}],"aggregations" : [ ]}]}
}

从上面的记过我们看到,分值都是1,所以不需要考虑使用分值进行排序了,直接进行值匹配,只要大于27即匹配成功。

从上面例子我们可以看出filter context和query context的明显的差别。这两个的区别将贯穿后面的所有的查询中。