1. 程式人生 > >學習用Node.js和Elasticsearch構建搜索引擎(6):實際項目中常用命令使用記錄

學習用Node.js和Elasticsearch構建搜索引擎(6):實際項目中常用命令使用記錄

nds 黃色 ati cat htm action last shard open

1、檢測集群是否健康。

curl -XGET localhost:9200/_cat/health?v

#後面加一個v表示讓輸出內容表格顯示表頭

綠色表示一切正常,黃色表示所有的數據可用但是部分副本還沒有分配,紅色表示部分數據因為某些原因不可用。

2、查看集群的的節點列表。

curl -XGET localhost:9200/_cat/nodes?v

3、創建索引(方法一)

#創建一個名為demo_v1的索引
> curl -XPUT localhost:9200/demo_v1
{"acknowledged":true,"shards_acknowledged
":true}%

默認的number_of_shards=5, number_of_replicas=1,

說明:number_of_shards表示設置一個索引的碎片數量,number_of_replicas表示設置一個索引可被復制的數量。這兩個屬性的設置直接影響集群中索引和搜索操作的執行。

假設你有足夠的機器來持有碎片和復制品,那麽可以按如下規則設置這兩個值:

1) 擁有更多的碎片可以提升索引執行能力,並允許通過機器分發一個大型的索引;

2) 擁有更多的復制器能夠提升搜索執行能力以及集群能力。

對於一個索引來說,number_of_shards只能設置一次,而number_of_replicas可以使用索引更新設置API在任何時候被增加或者減少。

4、創建索引(方法二)

#創建一個名為demo_v2的索引,並設置number_of_shards=3,number_of_replicas=2
curl -XPUT http://localhost:9200/demo_v2/ -d{ "settings":{ "index":{ "number_of_shards":3, "number_of_replicas":2 } } }

5、查看索引

#查看所有索引列表
>curl -XGET ‘localhost:9200/_cat/indices?v

health status index uuid pri rep docs.count docs.deleted store.size pri.store.size

yellow open test_v3 MBBLajV1RXynlf9AsRk-nQ 5 1 0 0 260b 260b
yellow open test_v1 nD02rWTqQO-vUaiM8fvolg 5 1 0 0 650b 650b
yellow open demo_v1 TItuViE3Qyu8qqqMMiLkAQ 5 1 0 0 795b 795b
yellow open english Y6Zwxj2cQPijI6NBOmGy9g 5 1 10010130 0 29gb 29gb
yellow open demo_v2 RmYvA8NdSOupXCS5VlXraA 3 2 0 0 390b 390b
yellow open test_v2 HBlEt6zCRomBnQYQrfcpuA 5 1 0 0 260b 260b


#查看索引demo_v1列表
curl -XGET ‘localhost:9200/_cat/indices/demo_v1?v
#查看索引demo_v1,demo_v2列表
curl -XGET ‘localhost:9200/_cat/indices/demo_v1,demo_v2?v

6、給索引設置別名

#給索引demo_v1設置別名demo
curl -XPUT localhost:9200/demo_v1/_alias/demo

#給索引demo_v1設置別名demo
curl -XPOST localhost:9200/_aliases?pretty -d{
  "actions" : [
  { "add" : { "index" : "demo_v1", "alias" : "demo"} }
]}
#移除索引demo_v1的別名demo,並給索引demo_v2設置別名demo
curl -XPOST ‘localhost:9200/_aliases?pretty -d{
  "actions" : [
  { "remove" : { "index" : "demo_v1", "alias" : "demo" } },
  { "add" : { "index" : "demo_v2", "alias" : "demo" } }
]}

命令後面添加pretty表示漂亮的輸出,也就是格式化輸出

7、查看別名

#查看所有的別名
curl -XGET localhost:9200/_alias/*
{"demo_v1":{"aliases":{"demo":{}}}}%
#查看索引demo_v1的所有別名
curl -XGET localhost:9200/demo_v1/_alias/*
{"demo_v1":{"aliases":{"demo":{}}}}%

8、刪除索引

#刪除索引test_v1
curl -XDELETE ‘localhost:9200/test_v1
#刪除索引test_v2,test_v3
curl -XDELETE ‘localhost:9200/test_v2,test_v3

9、查看索引的設置信息

#查看索引demo_v1的所有設置信息
> curl -XGET localhost:9200/demo_v1?pretty
{
  "demo_v1" : {
    "aliases" : {
      "demo" : { }
    },
    "mappings" : { },
    "settings" : {
      "index" : {
        "creation_date" : "1497955726944",
        "number_of_shards" : "5",
        "number_of_replicas" : "1",
        "uuid" : "TItuViE3Qyu8qqqMMiLkAQ",
        "version" : {
          "created" : "5030099"
        },
        "provided_name" : "demo_v1"
      }
    }
  }
}

#查看索引demo_v1設置的基礎信息
curl -XGET localhost:9200/demo_v1/_settings?pretty
{
  "demo_v1" : {
    "settings" : {
      "index" : {
        "creation_date" : "1497955726944",
        "number_of_shards" : "5",
        "number_of_replicas" : "1",
        "uuid" : "TItuViE3Qyu8qqqMMiLkAQ",
        "version" : {
          "created" : "5030099"
        },
        "provided_name" : "demo_v1"
      }
    }
  }
}

#查看索引demo_v1設置的mapping信息
curl -XGET localhost:9200/demo_v1/_mapping?pretty
{
  "demo_v1" : {
    "mappings" : { }
  }
}

9、CRUD數據

#向索引demo_v1中添加幾條數據(type=fruit添加3條數據,type=book添加兩條數據)
curl -XPOST localhost:9200/_bulk?pretty -d{ "index" : { "_index" : "demo_v1", "_type" : "fruit", "_id" : "1" }}
{ "name" : "蘋果"}
{ "create" : { "_index" : "demo_v1", "_type" : "fruit", "_id" : "2" }}
{ "name" : "香蕉"}
{ "index" : { "_index" : "demo_v1", "_type" : "fruit", "_id" : "3" }}
{ "name" : "西瓜"}
{ "index" : { "_index" : "demo_v1", "_type" : "book", "_id" : "1" }}
{ "name" : "深入淺出node.js" }
{ "create" : { "_index" : "demo_v1", "_type" : "book", "_id" : "2" }}
{ "name" : "你不知道的javascript" }
#查看索引demo_v1下的數據
curl -XGET localhost:9200/demo_v1/_search?pretty
#查看索引demo_v1類型fruit下的數據
curl -XGET localhost:9200/demo_v1/fruit/_search?pretty
#查看索引demo_v1類型fruit,類型book下的數據
curl -XGET localhost:9200/demo_v1/fruit,book/_search?pretty
#用別名查看demo_v1下的數據
curl -XGET ‘localhost:9200/demo/_search?pretty #刪除索引=demo_v1類型=fruit下面id=
2的數據 curl -XPOST localhost:9200/_bulk?pretty -d{ "delete" : { "_index" : "demo_v1", "_type" : "fruit", "_id" : "2" }}
或者操作
curl -XDELETE ‘localhost:9200/demo_v1/fruit/2
#修改索引demo_v1類型fruit下面id=3的數據name字段 curl -XPOST localhost:9200/_bulk?pretty -d{ "update" : {"_id" : "3", "_type":"fruit", "_index": "demo_v1"}} { "doc" : {"name":"草莓"}}

10、給索引設置mapping

#查看索引demo_v1的mapping設置( 沒有手動設置的話采用系統默認設置,分詞器默認standard分詞(標準分詞器))
curl -XGET localhost:9200/demo_v1/_mapping?pretty
{
  "demo_v1" : {
    "mappings" : {
      "book" : {
        "properties" : {
          "name" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          }
        }
      },
      "fruit" : {
        "properties" : {
          "name" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          }
        }
      }
    }
  }
}

#給索引demo_v1的mapping設置中fruit新增一個字段tag
curl -XPOST localhost:9200/demo_v1/fruit/_mapping?pretty -d {
     "fruit": {
                "properties": {
                     "tag":{
                        "type":"text"
                   }
                }
            }
 }

註意:已經設置好的mapping可以新增字段,但是對已經設置的字段是不能修改的。因為Elasticsearch底層使用的是lucene庫,修改以後索引和搜索要涉及分詞方式等操作,所以不允許修改。

如果一個mapping設置過了,想要修改type或analyzer,通常的做法是新建一個索引,重新設置mapping,再把數據同步過來。

具體做法參見下一篇文章 《學習用Node.js和Elasticsearch構建搜索引擎(7):零停機時間更新索引配置或遷移索引》

學習用Node.js和Elasticsearch構建搜索引擎(6):實際項目中常用命令使用記錄