首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ElasticSearch权威指南学习(索引管理)

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

作者头像
老梁
发布2019-09-10 17:32:03
4200
发布2019-09-10 17:32:03
举报

创建索引

  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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-11-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 创建索引
  • 删除索引
  • 索引设置
  • 配置分析器
  • 自定义分析器
  • 创建自定义分析器
  • 总结
相关产品与服务
Elasticsearch Service
腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档