zoukankan      html  css  js  c++  java
  • es基本使用之查询数据

    es基本使用之查询数据


    查询全部

    {
      "query": {
        "match_all": {}
      }
    }
    

    • 查询结果说明
      • took:耗费了几毫秒
      • timed_out:是否超时,这里是没有
      • _shards:数据拆成了5个分片,所以对于搜索请求,会打到所有的primary shard
      • hits.total:查询结果的数量,多少个document
      • hits.max_score: score的含义,就是document对于一个search的相关度的匹配分数,越相关,就越匹配,分数也高
      • hits.hits:包含了匹配搜索的document的详细数据

    单个条件查询

    ## 方法1
    GET goods/fruit/_search/?q=name:xiangjiao         ## 查询名字为xiangjiao的
    ## 方法2
    GET goods/fruit/_search
    {
      "query": {
        "match": {
          "name": "xiangjiao"
        }
      }
    }
    

    多个条件查询

    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "taskid": "123"
              }
            },
            {
              "match": {
                "time": "2021-05-20"
              }
            }
          ]
        }
      }
    }
    

    按条件查询和排序

    • 查询 name=pingguo 使用价格进行正向排序
    GET goods/fruit/_search
    {
    	"query": {
    		"match": {
    			"name": "pingguo"
    		}
    	},
    	"sort": [
    	  {
    	    "price": {
    	      "order": "desc"
    	    }
    	  }
    	]
    }
    

    分页查询

    • 注意: 这里的检索结果是倒排索引,不是按照id排序的,是按照倒排的方式来进行检索的,再强调下,不是根据id排序
    • 每页显示两条数据
    GET /goods/fruit/_search
    {
    	"query": {
    		"match_all": {}
    	},
    	"from": 0,
    	"size": 2
    }
    

    只显示指定字段

    • 检索出来的内容也就只包含了name和price字段的内容
    GET /goods/fruit/_search
    {
    	"query": {
    		"match_all": {}
    	},
    	"_source": ["name","price"], 
    	"from": 0,
    	"size": 2
    }
    

    多条件匹配查询

    • 查询 name=xiangjiao 并且 price=25
    GET /goods/fruit/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "name": "xiangjiao"
              }
            },
            {
              "match": {
                "price": "25"
              }
            }
          ]
        }
      }
    }
    

    • 查询 name=xiangjiao 或者 price=45
    GET /goods/fruit/_search
    {
      "query": {
        "bool": {
          "should": [
            {
              "match": {
                "name": "xiangjiao"
              }
            },
            {
              "match": {
                "price": "45"
              }
            }
          ]
        }
      }
    }
    

    过滤查询

    • 注意:filter于must/must_not/should是并列关系,同属于bool的子属性
    • lt:小于, lte:小于等于, gt:大于, gte:大于等于
    • 查询 name=xiangjiao 或者 价格在 10~40之间的水果
    GET /goods/fruit/_search
    {
      "query": {
        "bool": {
          "should": [
            {
              "match": {
                "name": "xiangjiao"
              }
            }
          ],
          "filter": {
            "range": {
              "price": {
                "gte": 10,
                "lte": 40
              }
            }  
          }
        }
      }
    }
    

    全文检索

    • 查询 name=pingguo 或者 name=xiangjiao的
    GET /goods/fruit/_search
    {
      "query": {
        "match": {
          "name": "pingguo xiangjiao"
        }
      }
    }
    

    参考博客:http://v5blog.cn/pages/df12b3/#_3-6-多条件匹配查询

    从小白到大神的蜕变~~
  • 相关阅读:
    MVAPICH
    sql server触发器的例子
    Sql Server 判断表或数据库是否存在
    JS验证用户真实姓名
    js实现瀑布流的一种简单方法实例分享
    C#实现登录窗口(不用隐藏)
    判断滚动条到底部的JS代码
    php 中文字符串首字母的获取函数
    C#获取当前页面的URL
    C#动态生成图书信息XML文件
  • 原文地址:https://www.cnblogs.com/tjw-bk/p/14790688.html
Copyright © 2011-2022 走看看