ElasticSearch常用检索命令(二)

测试数据:见个人资源《es批量插入bank数据

##查询所有数据并按account_number和balance排序
## sort --》order by
GET /bank/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "account_number": "asc"
    },
    {
      "balance":{
        "order": "desc"
      } 
    }
  ]
}


##查询所有数据并分页
## from,size --》limit
GET /bank/_search
{
  "query": {
    "match_all": {}
  },
  "from": 0,
  "size": 5
}


## [返回部分字段] _source
## 查询指定字段数据
## _source --》 select account_number,firstname,age from bank
GET /bank/_search
{
  "query": {
    "match_all": {}
  },
  "_source": ["account_number","firstname","age"]
}


## [匹配查询] match
## 字符串时模糊匹配 结果按max_score权重排序
## match --》where address like %Lane%
GET bank/_search
{
  "query": {
    "match": {
      "address": "Lane"
    }
  }
}

## [匹配查询] match 
## 非字符串时精确匹配
## match --》where balance = 39225
GET bank/_search
{
  "query": {
    "match": {
      "balance": 39225
    }
  }
}


## [短句匹配] match_phrase
## 将"Holmes Lane"定义为一个整体字符串查询
## match_phrase --》 where address like "%Holmes Lane%"
GET bank/_search
{
  "query": {
    "match_phrase": {
      "address": "Holmes Lane"
    }
  }
}

## [完全匹配] .keyword
## 匹配与该值完全对应的数据
## .keyword --》 where address = "Holmes Lane"
GET bank/_search
{
  "query": {
    "match": {
      "address.keyword": "990 Mill Road"
    }
  }
}

## [多字段匹配] multi_math
## 查询多字段值包含"mill"的数据
## where state like "%mill%" or address like "%mill%"
GET bank/_search
{
  "query": {
    "multi_match": {
      "query": "mill",
      "fields": [
        "state",
        "address"
      ]
    }
  }
}


## [满足多条件] must
## 必须满足must内部所有条件
## where address like "%mill%" and gender like "M"
GET bank/_search
{
   "query":{
      "must":[
        {"match":{"address":"mill"}},
        {"match":{"gender":"M"}}
       ]
   }
}

## [不满足条件] must_not
## 必须不满足must_not内部所有条件 不记录权重分值
## where address like "%mill%" and gender like "M"
GET bank/_search
{
   "query":{
       "must_not":[
        {"match":{"age":"38"}},
        {"match":{"gender":"M"}}
       ]
  }
}



## [复合查询] bool
## 可拼接多个条件进行查询
## where (address like "%mill%" and gender like "%M%") and (age !=38 and state not like "%PA%")
GET bank/_search
{
   "query":{
     "bool": {
       "must":[
        {"match":{"address":"mill"}},
        {"match":{"gender":"M"}}
       ],
       "must_not":[
        {"match":{"age":"38"}},
        {"match":{"state":"PA"}}
       ]
     }
   }
}


## [应该满足条件] should
## 查询应该满足或不满足该条件的数据,满足的增加权重分
## 若query中只有should一个条件,那么should=must
## where (address like "%mill%" and age !=38) or address like "%Road%"
GET bank/_search
{
  "query": {
    "bool": {
      "must":[
        {"match":{"address":"mill"}}
       ],
       "must_not":[
        {"match":{"age":"38"}}
       ],
      "should": [
        {
          "match": {
            "address": "Road"
          }
        }
      ]
    }
  }
}

## [结果过滤] Filter
## 过滤指定条件的数据,不增加权重分
## where address like "%mill%" and (balance >=15000 and balance <=20000)
GET bank/_search
{
   "query":{
     "bool": {
        "must":[
          {"match":{"address":"mill"}}
        ],
        "filter": {
          "range": {
            "balance": {
              "gte": 15000,
              "lte": 20000
            }
          }
        }
     } 
   }
}

## [匹配某个字段] term 
## 全文检索用macth,非字符串用term
GET bank/_search
{
  "query": {
    "term": {
      "age": "38"
    }
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值