• 学习用Node.js和Elasticsearch构建搜索引擎(6):实际项目中常用命令使用记录


    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、自定义分词器

    #如果索引demo_v6没有创建,可以直接创建并设置逗号分词器
    curl -XPUT 'localhost:9200/demo_v6' -d'{
      "settings": {
        "index": {
          "analysis": {
            "analyzer": {
              "douhao_analyzer": {
                "pattern": ",",
                "type": "pattern"
              }
            }
          },
          "number_of_shards": "5",
          "number_of_replicas": "1"
        }
      }
    }'
    
    #如果索引demo_v1已经存在,先关索引,再设置逗号分词器,然后打开索引
    curl -XPOST 'localhost:9200/omo_v1/_close'
    curl -XPUT 'localhost:9200/demo_v1/_settings' -d'{"analysis":
        {
          "analyzer":
          {
            "douhao_analyzer":
            {
              "type":"pattern",
              "pattern":","
            }
          }
        }}'
    curl -XPOST 'localhost:9200/omo_v1/_open'

    11、给索引设置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):零停机时间更新索引配置或迁移索引》 

  • 相关阅读:
    leetcode 对称二叉树
    leetcode 验证二叉搜索树
    蓝桥杯 完美的代价 贪心
    蓝桥杯 字符串对比 模拟
    蓝桥杯 芯片测试 极限找规律
    蓝桥杯 2n皇后问题 深搜
    74. 搜索二维矩阵
    二分 34
    二分 35
    二分 69
  • 原文地址:https://www.cnblogs.com/fhen/p/7055798.html
Copyright © 2020-2023  润新知