Elasticsearch(十)索引管理

1、创建索引

你可以通过在 config/elasticsearch.yml 中添加下面的配置来防止自动创建索引。

action.auto_create_index: false

手动创建

手动创建:确保索引被创建在适当数量的分片上,在索引数据之前设置好分析器和类型映射。

PUT /my_index
{
    "settings": { ... any settings ... },
    "mappings": {
        "type_one": { ... any mappings ... },
        "type_two": { ... any mappings ... },
        ...
    }

删除索引

使用以下的请求来删除索引:

DELETE /my_index

你也可以用下面的方式删除多个索引

DELETE /index_one,index_two
DELETE /index_*

你甚至可以删除所有索引

DELETE /_all

2、设置索引

两个最重要的设置:

number_of_shards

定义一个索引的主分片个数,默认值是 `5`。这个配置在索引创建后不能修改。

number_of_replicas

每个主分片的复制分片个数,默认是 `1`。这个配置可以随时在活跃的索引上修改。

扫描二维码关注公众号,回复: 6184117 查看本文章

我们可以创建只有一个主分片,没有复制分片的小索引。

PUT /my_temp_index
{
    "settings": {
        "number_of_shards" :   1,
        "number_of_replicas" : 0
    }
}

然后,我们可以用 update-index-settings API 动态修改复制分片个数:

PUT /my_temp_index/_settings
{
    "number_of_replicas": 1
}

3、配置分析器

分析器的作用:用于将全文字符串转换为适合搜索的倒排索引。

standard 分析器是用于全文字段的默认分析器,对于大部分西方语系来说是一个不错的选择。

4、自定义分析器

分析器文章:https://blog.csdn.net/ruanhao1203/article/details/90024347

创建自定义分析器

与索引设置一样,我们预先配置好 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 标签

  2. 将 & 替换成 and,使用一个自定义的 mapping 字符过滤器

"char_filter": {
    "&_to_and": {
        "type":       "mapping",
        "mappings": [ "&=> and "]
    }
}
  1. 使用 standard 分词器分割单词

  2. 使用 lowercase 标记过滤器将词转为小写

  3. 用 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" ]
            }}
}}}

转换为命令:

curl -H "Content-Type: application/json" -XPUT 'http://10.24.54.241:9200/my_index' -d '
{
    "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" ]
            }}
}}}'

创建索引后,验证自定义分析器是否生效,运行以下命令:

curl -H "Content-Type: application/json" -XPOST 'http://10.24.54.241:9200/my_index/_analyze?pretty' -d '{"analyzer": "my_analyzer","text": "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
    }
  ]
}

除非我们告诉 Elasticsearch 在哪里使用,否则分析器不会起作用。我们可以通过下面的映射将它应用在一个 string 类型的字段上:

PUT /my_index/_mapping/my_type
{
    "properties": {
        "title": {
            "type":      "string",
            "analyzer":  "my_analyzer"
        }
    }
}

猜你喜欢

转载自blog.csdn.net/ruanhao1203/article/details/90023295
今日推荐