zoukankan      html  css  js  c++  java
  • 查询示例一

    数据准备

    DELETE /clayindex
    PUT /clayindex
    {
       "mappings": {
         "properties": {
         "name":{
           "type": "keyword"
         },
         "address":{
            "type": "text"
         },
         "age":{
            "type": "integer"
         }
         }
       }
    }
    #插入数据 可以自己生成id //  
    POST  /clayindex/_doc
    {
       "name":"曹操",
       "address":"魏国",
       "age":18
      
    }
    POST  /clayindex/_doc
    {
       "name":"贾诩",
       "address":"魏国",
       "age":19
      
    }
    POST  /clayindex/_doc
    {
       "name":"诸葛亮",
       "address":"蜀国",
       "age":37
      
    }
    POST  /clayindex/_doc
    {
       "name":"关羽",
       "address":"蜀国",
       "age":35
      
    }
    POST  /clayindex/_doc
    {
       "name":"周瑜",
       "address":["吴国","蜀国"],
       "age":25
    }
    View Code

    1、获取所有文档

    GET /clayindex/_doc/_search
    {
      "query": {
        "match_all":{
        }
      }
    }
    View Code

    2、分页查询,从第二条开始,查两条

    GET /clayindex/_doc/_search
    {
      "query": {
        "match_all": {}
      },
      "from": 1,
      "size": 2
    }
    View Code

    这种分页方式如果进行深度分页,比如到100页,每页十条数据,它会从每个分片都查询出100*10条数据,假设有五个分片,就是5000条数据,然后在内存中进行排序,然后返回拍过序之后的集合中的第1000-1010条数据

    3、指定查询出来的数据返回的字段

    GET /clayindex/_doc/_search
    {
      "query": {
        "match_all": {}
      },
      "_source": ["name","age"]
    }
    View Code

    返回的数据中返回nametages字段。

    4、address字段中包含魏

    GET /clayindex/_doc/_search
    {
      "query": {
        "match": {
          "address": ""
        }
      }
    }
    View Code

    返回的结果中元字段_score有评分,说明使用query会计算评分

    5、address字段中包含魏,并按照年龄升序排列

    GET /clayindex/_doc/_search
    {
      "query": {
        "match": {
          "address": ""
        }
      },
      "sort": [
        {
          "age": {
            "order": "asc"
          }
        }
      ]
    }
    View Code

    6、年龄字段大于30

    GET /clayindex/_doc/_search
    {
      "query": {
        "bool": {
          "filter": {
            "range": {
              "age": {
                "gte": 30
              }
            }
          }
        }
      }
    }
    View Code

    返回的结果中元字段_score字段等于0,没评分,说明使用filter不会计算评分

    7、 address字段中包含魏,并且年龄字段大于10

    GET /clayindex/_doc/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "address": ""
              }
            }
          ], 
          "filter": {
            "range": {
              "age": {
                "gte": 10
              }
            }
          }
        }
      }
    }
    View Code

    8、查询address字段包含魏的文档的数量

    GET /clayindex/_doc/_count
    {
      "query": {
        "match": {
          "address": ""
        }
      }
    }
    View Code

    9、排序

    GET clayindex/_doc/_search
    {
      "query":{
         
         "match": {
           "address": "魏国"
         }
      },
      "_source":["name"],
      "sort":[
        {"age":{"order":"desc"}},
        {"name":{"order":"asc"}}
        ]
    }
    View Code

    10、分页查询

    GET clayindex/_doc/_search
    {
      "query":{
         
         "match": {
           "address": "魏国"
         }
      },
      "_source":["name"],
      "sort":[
        {"age":{"order":"desc"}},
        {"name":{"order":"asc"}}
        ],
       "from":0,
       "size":10
    }
    View Code

    11、布尔值查询

    GET clayindex/_doc/_search
    {
      "query":{
          "bool": {
            "must": [
              {
         "match": {
           "address": "魏国"
         }
                
              },{
                "match": {
           "name": "曹操"
         }
                
              }
            ]
            
          }
      },
      "_source":["name"],
      "sort":[
        {"age":{"order":"desc"}},
        {"name":{"order":"asc"}}
        ],
       "from":0,
       "size":2
    }
    View Code

    12、条件1 or 条件2

    GET clayindex/_doc/_search
    {
      "query":{
          "bool": {
            "should": [
              {
         "match": {
           "address": "魏国"
         }
                
              },{
                "match": {
           "name": "曹操"
         }
                
              }
            ]
            
          }
      },
      "_source":["name"],
      "sort":[
        {"age":{"order":"desc"}},
        {"name":{"order":"asc"}}
        ],
       "from":0,
       "size":10
    }
    View Code

    13、排除 MUST_NOT

    #查询名字不是曹操的信息
    GET clayindex/_doc/_search
    {
      "query":{
         
        "bool": {
          "must_not": [
            {
               "match": {
           "name": "曹操"
            
         }
            }
          ]
        }
        
      }
    }
    View Code

    14、数据过滤FILTER

    gt   大于

    gte  大于等于

    lt  小于

    lte  小于等于!

    # 查询名字是曹操,然后年龄大于等于10 小于等于30

    GET clayindex/_doc/_search
    {
      "query":{
         
        "bool": {
          "must": [
            {
               "match": {
           "name": "曹操"
            
         }
            }
          ],
          "filter": {
            "range": {
              "age": {
                "gte": 10,
                "lte": 20
              }
            }
          }
        }
        
      }
    }
    View Code

    15、配备多个条件查询

    GET clayindex/_doc/_search
    {
      
      "query":{
        "match":{"address":"吴国 魏国 "}
        
      }
    }
    View Code

    16、高亮查询

    GET clayindex/_doc/_search
    {
      "query":{
      "match": {
        "name": "曹操"
      }
      },
      "highlight":{
        
        "fields": {
          "name": {}
        }
      }
    }
    
    GET clayindex/_doc/_search
    {
      "query":{
      "match": {
        "address": "魏国"
      }
      },
      "highlight":{
        "fields": {
          "address": {}
        }
      }
    }
    View Code

    17、自定义高亮的风格

    GET clayindex/_doc/_search
    {
      "query":{
      "match": {
        "name": "曹操"
        
      }
      },
      "highlight":{
        
        "pre_tags": "<p class='gaoliang'>", 
        "post_tags": "</p>", 
        "fields": {
          "name": {}
        }
      }
    }
    View Code

    18、多个条件多个字段高亮查询

    GET clayindex/_doc/_search
    {
      "query":{
       "bool": {
            "must": [
              {
         "match": {
           "address": "魏国"
         }
              },{
                "match": {
           "name": "曹操"
         }  
              }
            ]
            
          }
      },
      "highlight":{
        
        "pre_tags": "<p class='gaoliang'>", 
        "post_tags": "</p>", 
        "fields": [
          {"name": {}},
           {"address": {}}
       ]
      }
    }
    View Code
    gt   大于
    2
    gte  大于等于
    3
    lt  小于
    4
    lte  小于等于!
    5
    # 查询名字是曹操,然后年龄大于等于10 小于等于30
  • 相关阅读:
    leetcode443
    leetcode429
    leetcode55
    2019-8-31-PowerShell-拿到最近的10个系统日志
    2019-6-11-WPF-如何在应用程序调试启动
    2019-8-31-C#-将-Begin-和-End-异步方法转-task-异步
    2019-9-18-WPF-笔刷绑定不上可能的原因
    2019-3-25-win10-uwp-如何将像素数组转-png-文件
    2018-9-30-C#-从零开始写-SharpDx-应用-画三角
    2018-8-10-Roslyn-节点的-Span-和--FullSpan-有什么区别
  • 原文地址:https://www.cnblogs.com/Duko/p/14852815.html
Copyright © 2011-2022 走看看