栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

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

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, should 和must_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" : [ ]
  }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/734760.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号