ElasticSearch-Query DSL-领域特定语言查询

Elasticsearch提供了一个可以执行查询的Json风格的DSL(domain-specific language 领域特定语言),这个被称为Query DSL。

样例

GET bank/_search
{
  "query": {  #  查询的字段
    "match_all": {} # 查询类型【代表查询所有的索引】,es中可以在query中组合非常多的查询类型完成复杂查询;
  },
  "from": 0,  # 从第0条文档开始查
  "size": 5,  # 返回5条文档
  "_source":["balance"], # _source为要返回balance这个字段
  "sort": [
    {
      "account_number": {  # 返回结果按account_number的值进行排序
        "order": "desc"  # 降序
      }
    }
  ]
}

结果

{
  "took" : 18,  # 查询花费18ms
  "timed_out" : false,  # 没有超时
  "_shards" : { # 分片信息 
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : { # 命中
    "total" : {
      "value" : 1000,  # 查到1000"relation" : "eq"   
    },
    "max_score" : null, # 匹配分数
    "hits" : [
      {
        "_index" : "bank", # 索引
        "_type" : "account", # 类型
        "_id" : "999",  # 第一条数据id是999
        "_score" : null,  # 得分信息
        "_source" : { # 返回的字段
          "firstname" : "Dorothy",
          "balance" : 6087
        },
        "sort" : [  #  排序字段的值
          999
        ]
      },
      省略。。。

match

分词检索

GET bank/_search
{
  "query": {
    "match": {
      "address": "mill road"   # 只要包含mill或者road就可以查出来
    }
  }
}

match_phrase

不分词检索

GET bank/_search
{
  "query": {
    "match_phrase": {
      "address": "mill road"   # 只要包含(mill road)就可以查出来 mill和road作为一个查询整体不分开
    }
  }
}

keyword

精确检索

GET bank/_search
{
  "query": {
    "match": {
      "address.keyword": "990 Mill"  # 内容必须是990 Mill 严格相等 equal
    }
  }
}

multi_math

多字段检索

GET bank/_search
{
  "query": {
    "multi_match": { 
      "query": "mill",
      "fields": [ # state或address字段的值包含mill 不要求两个字段都包含
        "state",
        "address"
      ]
    }
  }
}

bool/must

GET bank/_search
{
   "query":{
        "bool":{
             "must":[ # address和gender的值必须包含mill和M
              {"match":{"address":"mill"}},
              {"match":{"gender":"M"}}
             ]
         }
    }
}

bool/must_not

GET bank/_search
{
  "query": {
    "bool": {
      "must_not": [  # age不包含38 过滤作用
        { "match": { "age": "38" }}
      ]
   }
}

bool/should

GET bank/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "gender": "M"
          }
        },
        {
          "match": {
            "address": "mill"
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "age": "18"
          }
        }
      ],
      "should": [ # 如果有lastname字段值等于Wallace的文档 它的匹配分数会更高
        {
          "match": {
            "lastname": "Wallace"
          }
        }
      ]
    }
  }
}

结果

{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 12.585751,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "970",
        "_score" : 12.585751, # 得分第一
        "_source" : {
          "account_number" : 970,
          "balance" : 19648,
          "firstname" : "Forbes",
          "lastname" : "Wallace",  # should匹配的字段
          "age" : 28, # 不是18
          "gender" : "M",  # 包含M
          "address" : "990 Mill Road",  # 包含mill
          "employer" : "Pheast",
          "email" : "forbeswallace@pheast.com",
          "city" : "Lopezo",
          "state" : "AK"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "136",
        "_score" : 6.0824604,
        "_source" : {
          "account_number" : 136,
          "balance" : 45801,
          "firstname" : "Winnie",
          "lastname" : "Holland",
          "age" : 38,
          "gender" : "M",
          "address" : "198 Mill Lane",
          "employer" : "Neteria",
          "email" : "winnieholland@neteria.com",
          "city" : "Urie",
          "state" : "IL"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "345",
        "_score" : 6.0824604,
        "_source" : {
          "account_number" : 345,
          "balance" : 9812,
          "firstname" : "Parker",
          "lastname" : "Hines",
          "age" : 38,
          "gender" : "M",
          "address" : "715 Mill Avenue",
          "employer" : "Baluba",
          "email" : "parkerhines@baluba.com",
          "city" : "Blackgum",
          "state" : "KY"
        }
      }
    ]
  }
}

bool/filter

在boolean查询中,must, shouldmust_not 元素都被称为查询子句 。文档是否符合每个“must”或“should”子句中的标准,决定了文档的“相关性得分”。 得分越高,文档越符合您的搜索条件。 默认情况下,Elasticsearch返回根据这些相关性得分排序的文档。

如果bool查询中只有filter条件的话,我们会发现得分都是0。

  • must 贡献得分
  • should 贡献得分
  • must_not 不贡献得分
  • filter 不贡献得分
GET bank/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "address": "mill"
          }
        }
      ],
      "filter": { 
        "range": { # 范围
          "balance": { 
            "gte": "10000", # 大于10000
            "lte": "20000"  # 小于20000
          }
        }
      }
    }
  }
}

term

非text字段匹配 包含匹配 适用于查数字 如果使用term来查文本字段很能查不到

GET bank/_search
{
  "query": {
    "term": {
      "address": "717" # 或 "address": 717 数字可以去掉双引号
    }
  }
}

结果

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 5.9908285,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "872",
        "_score" : 5.9908285,
        "_source" : {
          "account_number" : 872,
          "balance" : 26314,
          "firstname" : "Jane",
          "lastname" : "Greer",
          "age" : 36,
          "gender" : "F",
          "address" : "717 Hewes Street",
          "employer" : "Newcube",
          "email" : "janegreer@newcube.com",
          "city" : "Delshire",
          "state" : "DE"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "248",
        "_score" : 5.9908285,
        "_source" : {
          "account_number" : 248,
          "balance" : 49989,
          "firstname" : "West",
          "lastname" : "England",
          "age" : 36,
          "gender" : "M",
          "address" : "717 Hendrickson Place",
          "employer" : "Obliq",
          "email" : "westengland@obliq.com",
          "city" : "Maury",
          "state" : "WA"
        }
      }
    ]
  }
}

如果查文本

GET bank/_search
{
  "query": {
    "term": {
      "address": "717 Hewes Street"
    }
  }
}

结果为null

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

aggs/aggName

搜索address中包含mill的所有人的年龄分布以及平均年龄和平均薪资,但不显示这些人的详情。

GET bank/_search
{
  "query": { # 查询出包含mill的
    "match": {
      "address": "Mill"
    }
  },
  "aggs": { #基于查询聚合
    "ageAgg": {  # 聚合的名字,随便起
      "terms": { # 看值的可能性分布
        "field": "age",
        "size": 10
      }
    },
    "ageAvg": { 
      "avg": { # 看age值的平均
        "field": "age"
      }
    },
    "balanceAvg": {
      "avg": { # 看balance的平均
        "field": "balance"
      }
    }
  },
  "size": 0  # 不看详情 把查询到的记录省略 只看聚合结果
}

结果

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4, // 命中4条
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "ageAgg" : { // 第一个聚合的结果
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : 38,  # 38岁有两人
          "doc_count" : 2
        },
        {
          "key" : 28,
          "doc_count" : 1
        },
        {
          "key" : 32,
          "doc_count" : 1
        }
      ]
    },
    "ageAvg" : { // 平均年龄
      "value" : 34.0 
    },
    "balanceAvg" : { // 平均薪资
      "value" : 25208.0
    }
  }
}

aggs/aggName/aggs/aggName

复杂聚合

查出每个年龄段下男女的平均薪资和每个年龄段的平均薪资。

GET bank/_search
{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "ageAgg": {
      "terms": {  #  看age分布
        "field": "age",
        "size": 100
      },
      "aggs": { # 子聚合
        "genderAgg": {
          "terms": { # 看gender分布
            "field": "gender.keyword" # 注意这里,文本字段应该用.keyword
          },
          "aggs": { # 子聚合
            "balanceAvg": {
              "avg": { # 男性的平均
                "field": "balance"
              }
            }
          }
        },
        "ageBalanceAvg": {
          "avg": { #age分布的平均(男女)
            "field": "balance"
          }
        }
      }
    }
  },
  "size": 0
}

结果

{
  "took" : 119,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1000,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "ageAgg" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : 31,
          "doc_count" : 61,
          "genderAgg" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "M",
                "doc_count" : 35,
                "balanceAvg" : {
                  "value" : 29565.628571428573
                }
              },
              {
                "key" : "F",
                "doc_count" : 26,
                "balanceAvg" : {
                  "value" : 26626.576923076922
                }
              }
            ]
          },
          "ageBalanceAvg" : {
            "value" : 28312.918032786885
          }
        }
      ]
        .......//省略其他
    }
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王景清

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值