Elasticsearch常用查询

本文详细介绍了Elasticsearch中各种查询方法,包括评分查询、过滤查询、常量评分查询、精确值匹配、多值精确匹配、经过分析器后的匹配、短语匹配、多字段匹配、存在性检查、范围匹配、组合查询、分页查询、多字段搜索控制权重及控制精度等,为用户提供全面的搜索解决方案。

使用评分查询来进行全文搜索或者其它任何需要影响相关性得分的搜索。

除此以外的情况都使用过滤查询。过滤查询不需要计算得分,且会缓存结果。

1. constant_score 查询

常用于只有filter,查询结果使用常量评分

GET /_search
{
  "query": {
    "constant_score": {
      "filter" : {
        "term" : {
          "playCount" : 113411 
        }
      }
    }
  }    
}

2. term 查询

用于精确值匹配

GET /_search
{
  "query": {
    "term" : {
        "playCount" : 1134
    }
  }    
}

3. terms查询

多值精确匹配

GET /_search
{
  "query": {
    "terms" : {
        "playCount" : [1134,113411]
    }
  }    
}

4. match 查询

经过分析器后进行匹配

GET /_search
{
  "query": {
    "match" : {
        "title" : "wake"
    }
  }    
}

5. match_phrase 查询

短语匹配,短语经过分析器后形成多个词,这些词必须按序出现在该字段中,这些词组成一个不可分割的短语。

GET /_search
{
  "query": {
    "match_phrase" : {
        "title" : "wake me"
    }
  }    
}

6. multi_match 查询

可匹配多个字段

GET /_search
{
  "query": {
     "multi_match": {
        "query":    "Tang",
        "fields":   [ "artist", "album" ]
    }
  }    

7. exists查询

GET /_search
{
  "query":{
      "exists":   {
        "field":    "album"
    }
  }
}

8. range 查询

范围匹配,用于日期/数值

可选比较:gt/gte/lt/lte

GET /_search
{
  "query":{
    "range":{
      "playCount":{
        "gt":10,
        "lte":1134
      }
    }
  }
}

9. 组合查询

should并不影响结果,但影响评分

如果不存在must,至少匹配一条should

GET /_search
{
  "query": {
     "bool": {
        "must":[{
          "term":{
            "playCount":1134
          }
        }],
        "must_not":[{
          "exists":{
            "field":"album"
          }
        }],
        "should":[{
          "term":{
            "playCount":1134
          }
        }],
        "filter":{
            "term":{
            "playCount":1134
          }
        }
    }
  }    
}

10. 分页查询

GET /_search
{
  "from": 1,
  "size": 3
}

11. 多字段搜索 控制权重

GET /books/_search
{
    "query": {
        "multi_match": {
            "query":       "peter smith",
            "type":        "cross_fields",
            "fields":      [ "title^2", "description" ] 
        }
    }
}

12. 控制精度

GET /_search
{
  "query": {
     "bool": {
        "must":[{
          "term":{
            "playCount":1134
          }
        }],
        "must_not":[{
          "exists":{
            "field":"album"
          }
        }],
        "should":[{
          "term":{
            "playCount":1134
          }
        }],
        "filter":{
            "term":{
            "playCount":1134
          }
        },
        "minimum_should_match": "75%"
    }
  }    
}
GET /my_index/my_type/_search
{
  "query": {
    "match": {
      "title": {
        "query":                "quick brown dog",
        "minimum_should_match": "75%"
      }
    }
  }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值