Elasticsearch 基础教程

Elasticsearch 高级教程

Elasticsearch 插件

Elasticsearch 笔记

es(elasticsearch)创建索引详解

es(elasticsearch) 索引管理详解 es(elasticsearch) 索引管理详解


Elasticsearch 创建索引如同 MySQL 创建数据库,es 提供了 RESTFul API 来创建索引,可以通过 PUT 命令快速创建一个默认配置的索引。

快速创建索引

快速创建一个 article 的索引,可以如下命令:

PUT http://127.0.0.1:9200/article

如上索引会采用默认的配置,Elasticsearch 默认给一个索引设置 5 个分片和 1 个副本,一个索引的分片数一旦指定后就不能再修改,而副本数可以通过命令随时修改

值得注意的是,索引不能出现大写字母。

如果索引名称包含大写字母,会报如下错误信息:

{
  "error": {
    "root_cause": [{
      "type": "invalid_index_name_exception",
      "reason": "Invalid index name [Article], must be lowercase",
      "index_uuid": "_na_",
      "index": "Article"
    }],
    "type": "invalid_index_name_exception",
    "reason": "Invalid index name [Article], must be lowercase",
    "index_uuid": "_na_",
    "index": "Article"
  },
  "status": 400
}

我们通过 CURL 命令演示一个 article 索引,看一下效果:

curl -X PUT "127.0.0.1:9200/article?pretty"

创建索引成功,返回如下信息:

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "article"
}

设置索引分片及副本

ES 的索引分片和副本可以通过 settings 参数在索引初始化时设置。假设给索引 article 分配 3 个分片和 0 个副本,命令如下:

PUT article
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 0
  }
}

改变索引副本数

如上所述,索引创建后,分片数是不能再改变的,但是副本数允许可变。

比如,把 article 索引的副本数修改为 2 个,命令如下:

PUT article/_settings
{
  "number_of_replicas": 2
}

创建带有静态映射类型的索引

其实,在生产环境中创建索引时,除了设置分片副本数外,还会同时将索引中字段类型映射通过 mappings 参数进行设置,如下示例:

PUT article
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 1
  },
  "mappings": {
    "crawl_article": {
      "_all": {
        "enabled": false
      },
      "properties": {
        "summary": {
          "search_analyzer": "ik_smart",
          "similarity": "BM25",
          "analyzer": "ik_max_word",
          "type": "text"
        },
        "title": {
          "search_analyzer": "ik_smart",
          "similarity": "BM25",
          "analyzer": "ik_max_word",
          "type": "text"
        },
        "content": {
          "search_analyzer": "ik_smart",
          "similarity": "BM25",
          "analyzer": "ik_max_word",
          "type": "text"
        }
      }
    }
  }
}

创建带有分析器的索引

Elasticsearch 创建索引时,也可以自定义的分析器(analyzer),下面给出带有自定义分析器、指定分片副本数及 mapping 映射的完整索引示例:

PUT article
{
  "settings": {
    "number_of_shards": 1, 
    "number_of_replicas": 0, 
    "analysis": {
      "char_filter": {
        "point_2_whitespace": {
          "type": "mapping", 
          "mappings": [
            ".=>' '"
          ]
        }
      }, 
      "analyzer": {
        "my_ik_max_word": {
          "char_filter": [
            "html_strip", 
            "point_2_whitespace"
          ], 
          "tokenizer": "ik_max_word", 
          "filter": [
            "lowercase"
          ]
        }, 
        "my_ik_smart": {
          "char_filter": [
            "html_strip", 
            "point_2_whitespace"
          ], 
          "tokenizer": "ik_smart", 
          "filter": [
            "lowercase"
          ]
        }
      }
    }
  }, 
  "mappings": {
    "article": {
      "_all": {
        "enabled": false
      }, 
      "properties": {
        "title": {
          "search_analyzer": "my_ik_smart", 
          "similarity": "BM25", 
          "analyzer": "my_ik_max_word", 
          "type": "text"
        }, 
        "summary": {
          "search_analyzer": "my_ik_smart", 
          "similarity": "BM25", 
          "analyzer": "my_ik_max_word", 
          "type": "text"
        }, 
        "content": {
          "search_analyzer": "my_ik_smart", 
          "similarity": "BM25", 
          "analyzer": "my_ik_max_word", 
          "type": "text"
        }
      }
    }
  }
}