bingmous

欢迎交流,不吝赐教~

导航

Elasticsearch常用命令总结

查看集群状态

# 查看集群健康状态
curl http://*:9200/_cluster/health?pretty

# 查询节点当前状态
curl http://*:9200/_nodes/stats?pretty

# 查看所有索引状态
curl http://*:9200/_cat/indices?pretty

# 查看异常索引状态
curl http://*:9200/_cat/indices?v&health=red

# 查看异常索引分片分配状态
curl http://*:9200/_cat/shards/my_index?v

# 查看分片分配不成功的原因
curl http://*:9200/_cat/shards/my_index?v&h=n,index,shard,prirep,state,sto,sc,unassigned.reason,unassigned.details

# 重新分配失败的分片
curl -XPOST http://*:9200/_cluster/reroute?retry_failed=true

查看配置

curl http://*:9200/_cluster/settings # 查看明确定义的配置
    # &flat_settings 表示使用平面化格式,不加使用默认json格式
    # &include_defaults 表示包含默认参数

curl http://*:9200/your_index/_settings # 查看单个索引的配置

修改配置

# 修改索引副本数 *表示所有索引
curl -XPUT http://*:9200/*/_settings -H 'Content-Type:application/json' -d '{"index":{"numer_of_replicas":0}}'

# 设置scroll上限
curl -XPUT http://*:9200/_cluster/settings -H 'Content-Type:application/json' -d '{"persistent":{"search.max_open_scroll_context":5000},"transient":{"search.max_open_scroll_context":5000}}'

# 取消所有索引只读
curl -XPUT http://*:9200/_all/_settings -H "Content-Type: application/json" -d '{"index.blocks.read_only_allow_delete": false}'

查询数据

kibana操作

GET _search
{
  "query": {
    "match_all": {}
  }
}
 
GET _cat/nodes
GET _cat/health
GET _cat/master
GET _cat/indices
GET _cat/indices?v
 
# 索引一个文档
PUT /customer/_doc/6
{
  "name":"Haha"
}
 
# 查看一个文档
GET /customer/_doc/6
GET /customer
 
# 更新一个文档
POST customer/_update/6/
{
  "doc":{
    "name":"Jane",
    "age":19
  }
}
 
# 删除一个文档
DELETE customer/_doc/5
# 删除整个索引
DELETE customer
DELETE bank
 
GET bank/_doc/1
PUT /bank/_doc/_bulk
 
 
 
 
GET customer/_doc/1
PUT /customer/_bulk
{"index":{"_id":"1"}}   
{"name":"John Doe"}
{"index":{"_id":"2"}}
{"name":"Jane"}
 
# 对整个es操作
PUT /_bulk
{"delete":{"_index":"webset","_id":"123"}}
{"create":{"_index":"webset","_id":"123"}}
{"title":"My first blog post"}
{"index":{"_index":"webset"}}
{"title":"My second blog post"}
{"update":{"_index":"webset","_id":"123"}}
{"doc":{"title":"My updateed blok post"}}
 
GET _cat/indices?v
DELETE bank
 
# 通过request uri检索
GET bank/_search?q=*&sort=account_number:desc
 
GET bank/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "account_number": "desc"
    },
    {
      "balance": "desc"
    }
  ]
}
 
GET /
 
GET bank/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "account_number": {
        "order": "desc"
      }
    }
  ]
}
 
# match
GET bank/_search
{
  "query": {
    "match": {
      "address": "mill lane"
    }
  }
}
 
# match_phrase
GET bank/_search
{
  "query": {
    "match_phrase": {
      "address": "mill lane"
    }
  }
}
# multi_match 在某个字段里面匹配 分词
GET bank/_search
{
  "query": {
    "multi_match": {
      "query": "mill Movico",
      "fields": ["address", "city"]
    }
  }
}
 
# bool复合查询
GET bank/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "gender": "M"
        }},
        {
          "match": {
            "address": "mill"
          }
        }
      ],
      "must_not": [
        {"match": {
          "age": 28
        }}
      ],
      "should": [
        {"match": {
          "lastname": "Holland"
        }}
      ]
    }
  }
}
 
# filter
# 年龄在18-30
GET bank/_search
{
  "query": {
    "bool": {
      "must": [
        {"range": {
          "age": {
            "gte": 18,
            "lte": 30
          }
        }}
      ]
    }
  }
}
# filter不贡献相关性得分,可以对结果直接过滤
GET bank/_search
{
  "query": {
    "bool": {
      "filter": [
        {"range": {
          "age": {
            "gte": 18,
            "lte": 30
          }
        }}
      ]
    }
  }
}
 
# 精确匹配字段值
GET bank/_search
{
  "query": {
    "term": {
      "age":28
    }
  }
}
# match_phrase不分词查询
GET bank/_search
{
  "query": {
    "match_phrase": {
      "address": "mill lane"
    }
  }
}
# 使用keyword精确完整匹配
GET bank/_search
{
  "query": {
    "match": {
      "address.keyword": "198 Mill Lane"
    }
  }
}
 
# 搜索地址中含有mill的所有人的年龄分布terms、平均年龄avg、平均薪资
GET bank/_search
{
  "query": {
    "match": {
      "address": "mill"
    }
  },
  "aggs": {
    "ageAgg": {
      "terms": {
        "field": "age",
        "size": 10
      }
    },
    "ageAvg":{
      "avg": {
        "field": "age"
      }
    },
    "balanceAvg":{
      "avg": {
        "field": "balance"
      }
    }
  },
  "size": 0
}
 
# 求所有人的年龄分布、各年龄分布的平均值、工资平均值(子聚合)
GET bank/_search
{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "ageAgg": {
      "terms": {
        "field": "age",
        "size": 100
      },
      "aggs": {
        "ageAvg": {
          "avg": {
            "field": "age"
          }
        },
        "balanceAvg":{
          "avg": {
            "field": "balance"
          }
        }
      }
    }
  },
  "size": 0
}
 
# 求所有人的年龄分布、各年龄分布中M和F的平均薪资及这个年龄段的总体平均薪资
GET bank/_search
{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "ageAgg": {
      "terms": {
        "field": "age",
        "size": 100
      },
      "aggs": {
        "balanceAvg": {
          "avg": {
            "field": "balance"
          }
        },
        "genderAgg": {
          "terms": {
            "field": "gender.keyword",
            "size": 10
          },
          "aggs": {
            "balanceAvg": {
              "avg": {
                "field": "balance"
              }
            }
          }
        }
      }
    }
  },
  "size": 0
}
 
# mapping 字段的类型,决定了如何处理数据
# 查看映射
GET bank/_mapping
DELETE my_index
# 创建映射 keyword不进行分词,text分词,index默认为true(是否可被检索)
PUT my_index
{
  "mappings": {
    "properties": {
      "age":{"type": "integer"},
      "email":{"type": "keyword"},
      "name":{"type": "text","index": true}
    }
  }
}
GET my_index/
# 添加映射
PUT my_index/_mapping
{
  "properties":{
    "employee_id": {
      "type":"keyword",
      "index":"false"
    }
  }
}
DELETE my_index
 
GET bank/_mapping
 
PUT newbank
{
  "mappings": {
    "properties": {
      "account_number": {
        "type": "long"
      },
      "address": {
        "type": "text"
      },
      "age": {
        "type": "integer"
      },
      "balance": {
        "type": "long"
      },
      "city": {
        "type": "keyword"
      },
      "email": {
        "type": "keyword"
      },
      "employer": {
        "type": "keyword"
      },
      "firstname": {
        "type": "keyword"
      },
      "gender": {
        "type": "keyword"
      },
      "lastname": {
        "type": "keyword"
      },
      "state": {
        "type": "text"
      }
    }
  }
}
GET bank/_search
# 迁移数据
POST _reindex
{
  "source": {
    "index": "bank"
  },
  "dest": {
    "index": "newbank"
  }
}
 
GET newbank/_search
 
# 分词 无法分割中文
POST _analyze
{
  "analyzer": "standard",
  "text": ["hello你好"]
}
# 使用ik分词器
POST _analyze
{
  "analyzer": "ik_smart",
  "text": ["我是中国人"]
}
POST _analyze
{
  "analyzer": "ik_max_word",
  "text": ["我是中国人"]
}
 
POST _analyze
{
  "analyzer": "ik_smart",
  "text": ["乔碧萝殿下天大我地大"]
}
 

posted on 2023-06-01 19:56  Bingmous  阅读(112)  评论(0编辑  收藏  举报