Elasticsearh常用命令

1 集群管理

启动集群

./bin/elasticsearch -d

查看集群健康状态

curl '192.168.43.30:9200/_cat/health?v

关闭集群

jps

kill -9 4670

2 索引

创建索引

curl -XPUT '192.168.43.30:9200/weimr?pretty' -H 'Content-Type: application/json' -d'

{"settings" : {"number_of_shards" : 3,"number_of_replicas" : 1},

 "mappings" : {

   "doc" : {

     "properties" : {

       "about" : {"type" : "text","fields" : {"keyword" : {"type" : "keyword","ignore_above" : 256}}},

       "age" : {"type" : "long"},

       "first_name" : {"type" : "text","fields" : {"keyword" : {"type" : "keyword","ignore_above" : 256}}},

       "interests" : {"type" : "text","fields" : {"keyword" : {"type" : "keyword","ignore_above" : 256}}},

       "last_name" : {"type" : "text","fields" : {"keyword" : {"type" : "keyword","ignore_above" : 256}}}

     }

   }

 }

}'

查看所有索引

curl 192.168.43.30:9200/_cat/indices?v

所有索引库(index)和索引表(type)

curl 192.168.43.30:9200/_search?pretty=true

查看索引映射信息

curl http://192.168.43.30:9200/weimr/doc/_mapping?pretty=true

插入记录

curl -XPUT 'http://192.168.43.30:9200/weimr/doc/1' -H 'Content-Type: application/json'  -d '

{

    "first_name" : "John",

    "last_name" :  "Smith",

    "age" :        25,

    "about" :      "I love to go rock climbing",

    "interests": [ "sports", "music" ]

}'

curl -XPUT http://192.168.43.30:9200/weimr/doc/2 -H 'Content-Type: application/json' -d '

{

    "first_name" :  "Jane",

    "last_name" :   "Smith",

    "age" :         32,

    "about" :       "I like to collect rock albums",

    "interests":  [ "music" ]

}'

curl -XPUT http://192.168.43.30:9200/weimr/doc/3 -H 'Content-Type: application/json' -d '

{

    "first_name" :  "Douglas",

    "last_name" :   "Fir",

    "age" :         35,

    "about":        "I like to build cabinets",

    "interests":  [ "forestry" ]

}'

查看某个索引和记录

curl 192.168.43.30:9200/weimr/_search?pretty

查询某个索引类型记录数

curl 192.168.43.30:9200/weimr/doc/_count?pretty

查询所有记录

curl 192.168.43.30:9200/weimr/doc/_search?pretty=true

多条件分页精确查询

curl '192.168.43.30:9200/weimr/doc/_search?pretty' -H 'Content-Type: application/json' -d'

{

  "query": {

    "bool": {

      "filter":[

        {"term": {"first_name.keyword": "Jane"}},

        {"range": {"age":{ "gte": 32 }}}

      ]

    }

  },

  "sort": { "_id": "asc"},

  "from": 0,

  "size": 2

}'

备注:因为语法解析的缘故,first_name.keyword表示不解析情况下匹配

多条件分页匹配查询

curl '192.168.43.30:9200/weimr/doc/_search?pretty' -H 'Content-Type: application/json' -d'

{

  "query": {

    "bool": {

      "must":[

        {"match":{"about": "like"}},

        {"range": {"age":{ "gte": 32 }}}

      ]

    }

  },

  "sort": { "_id": "asc"},

  "from": 0,

  "size": 2

}'

全文检索

curl -XGET 192.168.43.30:9200/weimr/doc/_search?pretty -H 'Content-Type: application/json' -d '

{

  "query": {

    "multi_match": {

      "query":"rock"

    }

  },

  "sort": { "_id": "asc"},

  "from": 0,

  "size": 2

}'

分组统计

curl '192.168.43.30:9200/weimr/doc/_search?pretty' -H 'Content-Type: application/json' -d'

{

  "size": 0,

  "aggs": {

    "app_code_count": {

      "terms": {

        "field": "last_name.keyword",

        "order" : {

          "_count" : "asc"

        }

      },

      "aggs": {

        "num_id_count": {

          "terms": {

            "field": "last_name.keyword"

          }

        }

      }

    }

  }

}'

删除文档(记录)

curl -XDELETE 192.168.43.30:9200/weimr/doc/1

清空数据

curl -XPOST 192.168.43.30:9200/weimr/doc/_delete_by_query?pretty -H 'Content-Type: application/json' -d '

{

  "query": {

    "match_all": {}

  }

}'

删除索引库

curl -XDELETE 'http://192.168.43.30:9200/weimr?pretty=true'

批量导入数据

data.json

{"index": { "_id": 1 }}

{"first_name":"John","last_name":"Smith","age":25,"about":"I love to go rock climbing","interests": [ "sports", "music" ]}

{"index": { "_id": 2 }}

{"first_name":"Jane","last_name":"Smith","age":32,"about":"I like to collect rock albums","interests": [ "music" ]}

{"index": { "_id": 3 }}

{"first_name":"Douglas","last_name":"Fir","age":35,"about":"I like to build cabinets","interests": [ "forestry" ]}

 

curl -XPOST '192.168.43.30:9200/weimr/doc/_bulk' -H 'Content-Type: application/json' --data-binary @data.json

更新文档(记录)

curl -XPUT 192.168.43.30:9200/weimr/doc/1 -H 'Content-Type: application/json' -d '

{

    "first_name" : "John",

    "last_name" :  "Smith",

    "age" :        26,

    "about" :      "I love to go rock climbing",

    "interests": [ "sports", "music" ]

}'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

手心脈动

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值