ES 查询

查询

1 基本查询

  • 根据文档ID

      curl -X GET 127.0.0.1:9200/articles/article/1
      curl -X GET 127.0.0.1:9200/articles/article/1?_source=title,user_id
      curl -X GET 127.0.0.1:9200/articles/article/1?_source=false
    
  • 查询所有

      curl -X GET 127.0.0.1:9200/articles/article/_search?_source=title,user_id
    
  • 分页

    • from 起始
    • size 每页数量

      curl -X GET 127.0.0.1:9200/articles/article/_search?_source=title,user_id\&size=3
      
      curl -X GET 127.0.0.1:9200/articles/article/_search?_source=title,user_id\&size=3\&from=10
      
  • 全文检索

      curl -X GET 127.0.0.1:9200/articles/article/_search?q=content:python%20web\&_source=title,article_id\&pretty
    
      curl -X GET 127.0.0.1:9200/articles/article/_search?q=title:python%20web,content:python%20web\&_source=title,article_id\&pretty
    
      curl -X GET 127.0.0.1:9200/articles/article/_search?q=_all:python%20web\&_source=title,article_id\&pretty
    
    • %20 表示空格

2 高级查询

  • 全文检索 match

      curl -X GET 127.0.0.1:9200/articles/article/_search -d'
      {
          "query" : {
              "match" : {
                  "title" : "python web"
              }
          }
      }'
    
      curl -X GET 127.0.0.1:9200/articles/article/_search?pretty -d'
      {
          "from": 0,
          "size": 5,
          "_source": ["article_id","title"],
          "query" : {
              "match" : {
                  "title" : "python web"
              }
          }
      }'
    
      curl -X GET 127.0.0.1:9200/articles/article/_search?pretty -d'
      {
          "from": 0,
          "size": 5,
          "_source": ["article_id","title"],
          "query" : {
              "match" : {
                  "_all" : "python web 编程"
              }
          }
      }'
    
  • 短语搜索 match_phrase

      curl -X GET 127.0.0.1:9200/articles/article/_search?pretty -d'
      {
          "size": 5,
          "_source": ["article_id","title"],
          "query" : {
              "match_phrase" : {
                  "_all" : "python web"
              }
          }
      }'
    
  • 精确查找 term

      curl -X GET 127.0.0.1:9200/articles/article/_search?pretty -d'
      {
          "size": 5,
          "_source": ["article_id","title", "user_id"],
          "query" : {
              "term" : {
                  "user_id" : 1
              }
          }
      }'
    
  • 范围查找 range

      curl -X GET 127.0.0.1:9200/articles/article/_search?pretty -d'
      {
          "size": 5,
          "_source": ["article_id","title", "user_id"],
          "query" : {
              "range" : {
                  "article_id": { 
                      "gte": 3,
                      "lte": 5
                  }
              }
          }
      }'
    
  • 高亮搜索 highlight

      curl -X GET 127.0.0.1:9200/articles/article/_search?pretty -d '
      {
          "size":2,
          "_source": ["article_id", "title", "user_id"],
          "query": {
              "match": {
                   "title": "python web 编程"
               }
           },
           "highlight":{
                "fields": {
                    "title": {}
                }
           }
      }
      '
    
  • 组合查询

    • must

      文档 必须 匹配这些条件才能被包含进来。

    • must_not

      文档 必须不 匹配这些条件才能被包含进来。

    • should

      如果满足这些语句中的任意语句,将增加 _score ,否则,无任何影响。它们主要用于修正每个文档的相关性得分。

    • filter

      必须 匹配,但它以不评分、过滤模式来进行。这些语句对评分没有贡献,只是根据过滤标准来排除或包含文档。

      curl -X GET 127.0.0.1:9200/articles/article/_search?pretty -d '
      {
        "_source": ["title", "user_id"],
        "query": {
            "bool": {
                "must": {
                    "match": {
                        "title": "python web"
                    }
                },
                "filter": {
                    "term": {
                        "user_id": 2
                    }
                }
            }
        }
      }
      '
      
  • 排序

      curl -X GET 127.0.0.1:9200/articles/article/_search?pretty -d'
      {
          "size": 5,
          "_source": ["article_id","title"],
          "query" : {
              "match" : {
                  "_all" : "python web"
              }
          },
          "sort": [
              { "create_time":  { "order": "desc" }},
              { "_score": { "order": "desc" }}
          ]
      }'
    
  • boost 提升权重,优化排序

      curl -X GET 127.0.0.1:9200/articles/article/_search?pretty -d'
      {
          "size": 5,
          "_source": ["article_id","title"],
          "query" : {
              "match" : {
                  "title" : {
                      "query": "python web",
                      "boost": 4
                  }
              }
          }
      }'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值