Elasticsearch常用命令

本文深入探讨了Elasticsearch中的数据结构、查询方法,包括字段查询、时间区间查询、组合查询及排序等高级功能,并讲解了如何进行数据删除与故障排除。通过具体实例,读者可以了解如何构建复杂查询,掌握数据管理和系统状态监控技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、数据结构如下:

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 11,
    "successful": 11,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 6.1136823,
    "hits": [
      {
        "_index": "news",
        "_type": "article",
        "_id": "1048088",
        "_score": 6.1136823,
        "_source": {
          "is_recommend": 0,
          "topic": [],
          "channel": "游戏",
          "item_id": "1048088",
          "item_type": "ARTICLE",
          "image_num": 3,
          "create_type": 1,
          "title": "美国两男子在网络中相识7年,第一次现实中见面激动不已",
          "region": "",
          "keyword": [],
          "janesi_time": "2018-09-07 13:16:07",
          "author_id": 23501,
          "tag": [
            "游戏",
            "照片",
            "游戏",
            "见面",
            "网络",
            "女网友"
          ]
        }
      }
    ]
  }
}

二、查询:

    1.查询某个字段 
    match  对查询的字段进行分词 , term 对查询的字段不分词



//匹配标题中含有字符串“男”“女”的条目
GET _search
{
  "query": {
    "match": {
   "title":"男 女"
    }
    
  }
}

//查询channel包含“育儿”的条目
GET _search
{
  "query": {
    "term": {
   "channel.keyword":"育儿"
    }
    
  }
}


//查询index/type下的所有记录
GET index/type/_search
{
  "query": {
    "match_all": {}
  }
}

     2.查询某个时间区间
     gt 大于 , gte 大于等于 , lt 小于 , lte 小于等于

//查询janesi_time为 9月5日5点之后,10月1日0点之前的数据
GET _search
{
  "query": {
    "range": {
      "janesi_time":{
        "gt":"2018-09-05 17:00:00",
        "lt":"2018-10-01 00:00:00"
      }
    }
    
  }
}

    3.组合查询

       must:满足条件 ,must_not:不满足的条件;条件以数组形式拼接    

//查询创建类型 为1或2或3 且is_recommend为0 且item_type为ARTICLE 且时间大于2018-09-05 17点 且item_id不为1046031和1045902的数据

GET _search
{
  "query": {
       "bool" : {
          "must" : [
            {
              "terms" : {
                "create_type" : [
                  1,
                  2,
                  3
                ]
              }
            },
            {
              "match" : {
                "is_recommend" : 0
                }
            },
            {
              "term" : {
                "item_type.keyword" : {
                  "value" : "ARTICLE"
                }
              }
            },
            {
              "range": {
                "janesi_time":{
                "gt":"2018-09-05 17:00:00"
                } 
              }
            }
          ],
          "must_not" : [
            {
              "terms" : {
                "item_id.keyword" : [
                  1046031,
                  1045902
                ]
              }
            }
          ]
        }
  }
}

     4.排序

//查询图片数大于2的数据 按照分值相关度从大大小 时间从大到小排序
GET _search
{
  "query": {
    "range": {
      "image_num":{
        "gt":"2"
      }
    }
  },
    "sort" : [
    {
      "_score" : {
        "order" : "desc"
      }
    },
    {
      "janesi_time" : {
        "order" : "desc"
      }
    }
  ]
}

 

三、删除:

    删除满足查询条件的条目

// 删除janesi_time 小于 2018-09-05 17:00:00的条目
POST news/article/_delete_by_query
{
  "query": {
    "range": {
      "janesi_time":{
        "lt":"2018-09-05 17:00:00"
      }
    }
    
  }
}


// 删除按id查到的记录
POST index/type/id
{
  "query": {
    "match_all": {}
  }

}

四、故障排除

  解除写锁:磁盘使用率达85%以上后 ES将自动触发磁盘写锁;

//解除ES所有index的写锁 [ _all:所有 ]

PUT _all/_settings 
{"index.blocks.read_only_allow_delete": null}

_cat命令: 查看ES系统状态信息 

// cat支持的所有命令

/_cat/allocation            
/_cat/shards
/_cat/shards/{index}
/_cat/master
/_cat/nodes
/_cat/indices
/_cat/indices/{index}
/_cat/segments
/_cat/segments/{index}
/_cat/count
/_cat/count/{index}
/_cat/recovery
/_cat/recovery/{index}
/_cat/health
/_cat/pending_tasks
/_cat/aliases
/_cat/aliases/{alias}
/_cat/thread_pool
/_cat/plugins
/_cat/fielddata
/_cat/fielddata/{fields}
/_cat/nodeattrs
/_cat/repositories
/_cat/snapshots/{repository}

每个命令都支持使用?v参数,来显示详细的信息:例如

//查询健康状态

GET /_cat/health?v

//结果

epoch      timestamp cluster        status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1536306810 15:53:30  janesi-cluster yellow          1         1     11  11    0    0       11             0                  -                 50.0%

每个命令都支持使用help参数,来输出可以显示的列:例如

//  查看分片状态信息

GET /_cat/allocation?help

//  结果

shards       | s              | number of shards on node      
disk.indices | di,diskIndices | disk used by ES indices       
disk.used    | du,diskUsed    | disk used (total, not just ES)
disk.avail   | da,diskAvail   | disk available                
disk.total   | dt,diskTotal   | total capacity of all volumes 
disk.percent | dp,diskPercent | percent disk used             
host         | h              | host of node                  
ip           |                | ip of node                    
node         | n              | name of node                  

通过h参数,可以指定输出的字段:

//   查询分片状态 只显示磁盘使用率和ip地址 同时显示列名

GET /_cat/allocation?h=disk.used,ip&v

//结果

disk.used ip
   24.6gb 172.17.0.12
          

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值