结构化查询(Query DSL)和结构化过滤(Filter DSL)

爱被打了一巴掌 2022-07-12 08:15 320阅读 0赞

常用的查询过滤语句
(1)term 过滤:主要用于精确匹配,比如数字,日期,布尔值或 not_analyzed的字符串(未经分析的文本数据类型):

DEMO1:
{ “term”: { “age”: 26 }}
DEMO2:
{ “term”: { “date”: “2014-09-01” }}
DEMO3:
{ “term”: { “public”: true }}
DEMO4:
{ “term”: { “tag”: “full_text” }}

(2)terms 过滤:跟 term 有点类似,但 terms 允许指定多个匹配条件

  1. DEMO:
  2. {
  3. "terms": {
  4. "tag": [ "search", "full_text", "nosql" ]
  5. }
  6. }

(3)range 过滤:指定范围查找一批数据
范围操作符包含:
gt :: 大于
gte:: 大于等于
lt :: 小于
lte:: 小于等于

  1. DEMO:
  2. {
  3. "range": {
  4. "age": {
  5. "gte": 20,
  6. "lt": 30
  7. }
  8. }
  9. }

(4)exists 和 missing 过滤:用于查找文档中是否包含指定字段或没有某个字段,类似于SQL语句中的IS_NULL条件

  1. DEMO:
  2. {
  3. "exists": {
  4. "field": "title"
  5. }
  6. }

(5)bool 过滤:用来合并多个过滤条件查询结果的布尔逻辑,它包含一下操作符:
must :: 多个查询条件的完全匹配,相当于 and。
must_not :: 多个查询条件的相反匹配,相当于 not。
should :: 至少有一个查询条件匹配, 相当于 or。

  1. DEMO:
  2. {
  3. "bool": {
  4. "must": { "term": { "folder": "inbox" }},
  5. "must_not": { "term": { "tag": "spam" }},
  6. "should": [
  7. { "term": { "starred": true }},
  8. { "term": { "unread": true }}
  9. ]
  10. }
  11. }

(6)match_all 查询:查询到所有文档,是没有查询条件下的默认语句

  1. DEMO:
  2. {
  3. "match_all": {}
  4. }

(7)match 查询:是一个标准查询,不管你需要全文本查询还是精确查询基本上都要用到它
如果用match下指定了一个确切值,在遇到数字,日期,布尔值或者not_analyzed 的字符串时,它将为你搜索你给定的值:
DEMO1:
{
“match”: {
“tweet”: “About Search”
}
}
DEMO2:
{ “match”: { “age”: 26 }}
DEMO3:
{ “match”: { “date”: “2014-09-01” }}
DEMO4:
{ “match”: { “public”: true }}
DEMO5:
{ “match”: { “tag”: “full_text” }}

(8)multi_match 查询:允许你做match查询的基础上同时搜索多个字段

  1. DEMO:
  2. {
  3. "multi_match": {
  4. "query": "full text search",
  5. "fields": [ "title", "body" ]
  6. }
  7. }

(9)bool 查询:bool 查询与 bool 过滤相似,用于合并多个查询子句。不同的是,bool 过滤可以直接给出是否匹配成功, 而bool 查询要计算每一个查询子句的 _score (相关性分值)。

  1. DEMO
  2. {
  3. "bool": {
  4. "must": { "match": { "title": "how to make millions" }},
  5. "must_not": { "match": { "tag": "spam" }},
  6. "should": [
  7. { "match": { "tag": "starred" }},
  8. { "range": { "date": { "gte": "2014-01-01" }}}
  9. ]
  10. }
  11. }

DSL的综合使用

–空查询,查询所有记录

  1. GET /testindex/testtable/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. }
  6. }

–查询指定字段demo字段包含指定值的记录

  1. GET /testindex/testtable/_search
  2. {
  3. "query": {
  4. "match": {
  5. "demo": "kimchy"
  6. }
  7. }
  8. }

以下查询将会找到 title 字段中包含 “how to make millions”,并且 “tag” 字段没有被标为 spam。 如果有标识为 “starred” 或者发布日期为2014年之前,那么这些匹配的文档将比同类网站等级高:
注: 如果bool 查询下没有must子句,那至少应该有一个should子句。但是 如果有must子句,那么没有should子句也可以进行查询。

  1. GET /testindex/testtable/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": { "match": { "title": "how to make millions" }},
  6. "must_not": { "match": { "tag": "spam" }},
  7. "should": [
  8. { "match": { "tag": "starred" }},
  9. { "range": { "date": { "gte": "2014-01-01" }}}
  10. ]
  11. }
  12. }
  13. }

–分页查询

  1. GET /testindex/testtable/_search
  2. {
  3. "from": 2,
  4. "size": 2
  5. }

–查询指定ID的记录

  1. GET /testindex/testtable/_mget
  2. {
  3. "ids" : [ "2", "3" ]
  4. }

–统计记录数量

  1. GET testindex/testtable/_count
  2. {
  3. "query" : {
  4. "term" : { "demo" : "kimchy" }
  5. }
  6. }

–单条过滤语句:

  1. GET /testindex/testtable/_search
  2. {
  3. "query": {
  4. "filtered": {
  5. "filter": { "term": { "folder": "inbox" }}
  6. }
  7. }
  8. }

–查询语句中的过滤

  1. GET /testindex/testtable/_search
  2. {
  3. "query": {
  4. "filtered": {
  5. "filter": {
  6. "bool": {
  7. "must": { "term": { "folder": "inbox" }},
  8. "must_not": {
  9. "query": {
  10. "match": { "email": "urgent business proposal" }
  11. }
  12. }
  13. }
  14. }
  15. }
  16. }
  17. }

–按照指定字段排序

(注:可以只指定要排序的字段名称,字段值默认以顺序排列,而 _score 默认以倒序排列。)

  1. GET /testindex/testtable/_search
  2. {
  3. "query" : {
  4. "filtered" : {
  5. "filter" : { "term" : { "user_id" : 1 }}
  6. }
  7. },
  8. "sort": { "date": { "order": "desc" }}
  9. }

–按照多个字段排序

  1. GET /testindex/testtable/_search
  2. {
  3. "query" : {
  4. "filtered" : {
  5. "query": { "match": { "tweet": "manage text search" }},
  6. "filter" : { "term" : { "user_id" : 2 }}
  7. }
  8. },
  9. "sort": [
  10. { "date": { "order": "desc" }},
  11. { "_score": { "order": "desc" }}
  12. ]
  13. }

–为多值字段排序
一个拥有多值的字段就是一个集合,你可以从多个值中取出一个来进行排序,你可以使用min, max, avg 或 sum这些模式。
DEMO:在 dates 字段中用最早的日期来进行排序

  1. "sort": {
  2. "dates": {
  3. "order": "asc",
  4. "mode": "min"
  5. }
  6. }

–验证查询语句是否合法(注:只验证语法,不验证字段名称。)

  1. GET /testindex/testtable/_validate/query
  2. {
  3. "query": {
  4. "demo" : {
  5. "match" : "kimchy"
  6. }
  7. }
  8. }

–验证查询语句是否合法,并给出说明(可以提示字段名称错误)

  1. GET /testindex/testtable/_validate/query?explain
  2. {
  3. "query": {
  4. "match" : {
  5. "demos" : "kimchy"
  6. }
  7. }
  8. }

发表评论

表情:
评论列表 (有 0 条评论,320人围观)

还没有评论,来说两句吧...

相关阅读

    相关 DSL查询过滤

    1、 什么是DSL查询    由ES提供丰富且灵活的查询语言叫做DSL查询(Query DSL),它允许你构建更加复杂、强大的查询。   DSL(Domain Specif

    相关 ES Query DSL

    Query DSL 基本语法格式 Elasticsearch提供了一个可以执行查询的Json风格的DSL。这个被称为Query DSL,该查询语言非常全面。