ElasticSearch权威指南学习(索引管理)

创建索引

  1. 当我们需要确保索引被创建在适当数量的分片上,在索引数据之前设置好分析器和类型映射。
  2. 手动创建索引,在请求中加入所有设置和类型映射,如下所示:
PUT /my_index
{
    "settings": { ... any settings ... },
    "mappings": {
        "type_one": { ... any mappings ... },
        "type_two": { ... any mappings ... },
        ...
    }
  1. 你可以通过在 config/elasticsearch.yml 中添加下面的配置来防止自动创建索引。
action.auto_create_index: false

删除索引

  1. 使用以下的请求来删除索引:
DELETE /my_index
  1. 用下面的方式删除多个索引
DELETE /index_one,index_two
DELETE /index_*
  1. 甚至可以删除所有索引
DELETE /_all

索引设置

  1. 下面是两个最重要的设置:
    • number_of_shards
      • 定义一个索引的主分片个数,默认值是 5。这个配置在索引创建后不能修改。
    • number_of_replicas
      • 每个主分片的复制分片个数,默认是 1。这个配置可以随时在活跃的索引上修改。
  2. 例如,我们可以创建只有一个主分片,没有复制分片的小索引。
PUT /my_temp_index
{
    "settings": {
        "number_of_shards" :   1,
        "number_of_replicas" : 0
    }
}
  1. 然后,我们可以用 update-index-settings API 动态修改复制分片个数
PUT /my_temp_index/_settings
{
    "number_of_replicas": 1
}

配置分析器

  1. 第三个重要的索引设置是 analysis 部分,用来配置已存在的分析器或创建自定义分析器来定制化你的索引。
  2. 在下面的例子中,我们创建了一个新的分析器,叫做 es_std,并使用预定义的西班牙语停用词:
PUT /spanish_docs
{
    "settings": {
        "analysis": {
            "analyzer": {
                "es_std": {
                    "type":      "standard",
                    "stopwords": "_spanish_"
                }
            }
        }
    }
}
  • es_std 分析器不是全局的,它仅仅存在于我们定义的 spanish_docs 索引中

自定义分析器

  1. 虽然 Elasticsearch 内置了一系列的分析器,但是真正的强大之处在于定制你自己的分析器。你可以通过在配置文件中组合字符过滤器,分词器和标记过滤器,来满足特定数据的需求。

创建自定义分析器

  1. 与索引设置一样,我们预先配置好 es_std 分析器,我们可以再 analysis 字段下配置字符过滤器,分词器和标记过滤器:
PUT /my_index
{
    "settings": {
        "analysis": {
            "char_filter": { ... custom character filters ... },
            "tokenizer":   { ...    custom tokenizers     ... },
            "filter":      { ...   custom token filters   ... },
            "analyzer":    { ...    custom analyzers      ... }
        }
    }
}
  1. 作为例子,我们来配置一个这样的分析器:
    • 用 html_strip 字符过滤器去除所有的 HTML 标签
    • 将 & 替换成 and,使用一个自定义的 mapping 字符过滤器
    "char_filter": {
        "&_to_and": {
            "type":       "mapping",
            "mappings": [ "&=> and "]
        }
    }
    • 使用 standard 分词器分割单词
    • 使用 lowercase 标记过滤器将词转为小写
    • 用 stop 标记过滤器去除一些自定义停用词。
    "filter": {
        "my_stopwords": {
            "type":        "stop",
            "stopwords": [ "the", "a" ]
        }
    }
    • 根据以上描述来将预定义好的分词器和过滤器组合成我们的分析器:
    "analyzer": {
        "my_analyzer": {
            "type":           "custom",
            "char_filter":  [ "html_strip", "&_to_and" ],
            "tokenizer":      "standard",
            "filter":       [ "lowercase", "my_stopwords" ]
        }
    }
    • 用下面的方式可以将以上请求合并成一条:
    PUT /my_index
    {
        "settings": {
            "analysis": {
                "char_filter": {
                    "&_to_and": {
                        "type":       "mapping",
                        "mappings": [ "&=> and "]
                }},
                "filter": {
                    "my_stopwords": {
                        "type":       "stop",
                        "stopwords": [ "the", "a" ]
                }},
                "analyzer": {
                    "my_analyzer": {
                        "type":         "custom",
                        "char_filter":  [ "html_strip", "&_to_and" ],
                        "tokenizer":    "standard",
                        "filter":       [ "lowercase", "my_stopwords" ]
                }}
    }}}
    • 然后查看下(es5.0版本后的查询格式)
    GET /my_index/_analyze
    {
      "analyzer": "my_analyzer",
      "text":"The quick & brown fox"
    }
    • 5.0前老版本
    GET /my_index/_analyze?analyzer=my_analyzer
    The quick & brown fox
    • 结果
    {
      "tokens": [
        {
          "token": "quick",
          "start_offset": 4,
          "end_offset": 9,
          "type": "<ALPHANUM>",
          "position": 1
        },
        {
          "token": "and",
          "start_offset": 10,
          "end_offset": 11,
          "type": "<ALPHANUM>",
          "position": 2
        },
        {
          "token": "brown",
          "start_offset": 12,
          "end_offset": 17,
          "type": "<ALPHANUM>",
          "position": 3
        },
        {
          "token": "fox",
          "start_offset": 18,
          "end_offset": 21,
          "type": "<ALPHANUM>",
          "position": 4
        }
      ]
    }
    ## 元数据:_source 字段
  2. 在搜索请求中你可以通过限定 _source 字段来请求指定字段:
GET /_search
{
    "query":   { "match_all": {}},
    "_source": [ "title", "created" ]
}
  1. 元数据:_all 字段
    • 如果你决定不再使用 _all 字段,你可以通过下面的映射禁用它:
    PUT /my_index/_mapping/my_type
    {
        "my_type": {
            "_all": { "enabled": false }
        }
    }

    默认映射

  2. 我们可以使用 default 映射对所有类型禁用 _all 字段,而只在 blog 字段上开启它:
PUT /my_index
{
    "mappings": {
        "_default_": {
            "_all": { "enabled":  false }
        },
        "blog": {
            "_all": { "enabled":  true  }
        }
    }
}
  1. default 映射也是定义索引级别的动态模板的好地方。

总结

  1. 一口气学到这里,这章开始已经有点力不从心了,很多东西已经理解不了了,需要实际工作中,不断查找资料深入学习理解才能掌控了,索引管理的内容这里并不全面,我理解不了的地方这里我也不写了~

参考 https://es.xiaoleilu.com/070_Index_Mgmt/25_Mappings.html

猜你喜欢

转载自www.cnblogs.com/sky-chen/p/9965236.html