查询

URI Search

Request body search

  • query
    GET test_index/_search
    {
      "query": {
        "term": {    
          "age": {
            "value": "22"
          }}}}
  • from/size
    GET test_index/_search
    {
      "from": 0,    #第一个结果的偏移量,第0页  from默认为0
      "size": 1,     # 最大命中个数 , 1条数据   size 默认为10
      "query": {
        "term": {
          "name": {
            "value": "ketty"
          }}}}
  • sort    
  • GET test_index/_search
    {
      "query": {         #先过滤出含有ketty的name字段
        "term": {
          "name": {
            "value": "ketty"            
          }
        }
      }, 
      "sort": [        #后对这些字段排序
        {
          "age": {
            "order": "desc"         #desc 倒序 asc 顺序
          }}}}
  • source filtering

       默认会返回_source 所有字段的内容。  

  • # "_source": false   关闭
    # "_source": "fieldname"  
    # "_source": "obj.*"  可接受通配符
    
    GET test_index/_search
    {
      "_source": {                
        "excludes": "content",     
        "includes": "name"
      },
      "query": {
        "term": {
          "content": {
            "value": "hello"
          }}}}

 query DSL

  • query and filter context

              查询上下文:文档匹不匹配这个查询,相关度高吗。是在query进行查询是的执行环境

              过滤器上下文:文档匹不匹配。 不考虑相关性算分,和返回的排序问题。使用filter参数时

  • GET /_search
    {
      "query": {           #查询上下文
        "bool": {          
          "must": [      #两个match在查询上下文中
            { "match": { "title":   "Search"        }},        
            { "match": { "content": "Elasticsearch" }}  
          ],
          "filter": [       # 过滤器上下文
            { "term":  { "status": "published" }},   
            { "range": { "publish_date": { "gte": "2015-01-01" }}} 
          ]   # term,range在过滤器上下文中,会过滤掉不匹配的,但不会影响算分
        }
      }
    }
    View Code
  • match_all query
    GET test_index/_search
    {
      "query": {
        "match_all": {}
      }
    }
  • full-context queries

     - match   query

    1. GET test_index/_search
      {
        "query": {
            "match": {
               "content": "content"
            }}}

      #模糊查询 fuzziness
      GET test_index/_search
      {
        "query": {
          "match": {
            "content": {
              "query": "test tontent",
              "fuzziness": 1      # "fuzziness":"auto" 自动根据字段值长度编辑距离
            }}}}

     - match_phrase query 匹配短语

    1. GET test_index/_search
      {
        "query": {
          "match_phrase": {
            "content": {
              "query": "test content"   # 如果使用match,会匹配带有test,content,test content的文档 
            }}}}
        # "slop":"2"  "content test" 这个顺序 也可以匹配
      # "analyzer": "my_analyzer" 可以指定分词器

     - match_phrase_prefix  query  匹配短语前缀

    1. PUT test_index/doc/6
       {  "name":"fox",
           "message":"quick brown fox." }
      GET test_index/doc/_search
      {
        "query": {
          "match_phrase_prefix": {
            "message": {
              "query": "quick brown f",
              "max_expansions": "2"  # 控制将要扩展的后缀数量
            }
          }}}

     - multi_match query 多字段匹配    

    1. GET test_index/_search
      {
        "query": {
          "multi_match": {
            "query": "hello",
            "fields": ["message","conte*"]    #可以使用通配符
          }
        }
      }

     - query_string  query

      1. GET test_index/_search
        {
          "query": {
            "query_string": {
              "fields": ["message","content"],    #可查询多个字段
              "query": "fox AND quick OR white"
            }}}
        #返回结果
        ..........
                "_source": {"message": "quick brown fox."  }}]

     - simple query string query

    1. GET test_index/_search
      {
        "query": {
          "simple_query_string": {
            "query": "(test content) | fox + white",  # 匹配到一条 "message": "slow white fox."
            "fields": ["content","mess*"],
            "default_operator": "AND"
          }}}
      # +AND |OR -否定 *在末尾表示前缀查询

  

猜你喜欢

转载自www.cnblogs.com/xiaobaozi-95/p/9182330.html