Elasticsearch 自定义模板配置与 IK 分词器使用(文章末尾附带一个根据类型配置的通用模版)

267 阅读6分钟

Elasticsearch 自定义模板配置与 IK 分词器使用

在使用 Elasticsearch 进行全文检索时,合理的索引配置和分词器选择对于提升查询效率和准确性至关重要。本文将介绍如何配置自定义模板,为特定字段使用 IK 分词器,以及如何调整模板细节使数据查询更加方便。最后,我们还将提供一个基于数据类型的通用模板示例。

目录

  1. Elasticsearch 模板概述
  2. 创建自定义模板
  3. 为特定字段配置 IK 分词器
  4. 调整模板细节优化查询
  5. 基于数据类型的通用模板示例
  6. 总结

一、Elasticsearch 模板概述

模板(Template)是 Elasticsearch 提供的一种功能,允许我们在索引创建时自动应用预定义的设置和映射。通过模板,我们可以统一管理索引的配置,避免手动为每个索引设置相同的配置。

模板的主要作用:

  • 索引设置(settings):配置分片数、副本数、分词器等。
  • 映射(mappings):定义字段的数据类型、分词方式、是否索引等。

二、创建自定义模板

2.1 模板基本结构

一个基本的模板包含以下结构:

PUT _template/your_template_name
{
  "index_patterns": ["your_index_pattern*"],
  "order": 0,
  "settings": {
    // 索引设置
  },
  "mappings": {
    // 字段映射
  }
}
  • index_patterns:指定模板适用的索引名称模式,可以使用通配符。
  • order:模板的优先级,数值越大优先级越高。
  • settings:索引的设置,如分片数、副本数等。
  • mappings:字段的映射配置。

2.2 创建示例模板

假设我们要创建一个用于存储文章的索引模板,索引名称以 articles_ 开头。

PUT _template/articles_template
{
  "index_patterns": ["articles_*"],
  "order": 1,
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 1
  },
  "mappings": {
    "_source": {
      "enabled": true
    },
    "properties": {
      "title": {
        "type": "text"
      },
      "content": {
        "type": "text"
      },
      "publish_date": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
      },
      "author": {
        "type": "keyword"
      }
    }
  }
}

说明:

  • number_of_shards:设置分片数为 3。
  • number_of_replicas:设置副本数为 1。
  • _source.enabled:启用原始文档存储。
  • 字段映射:定义了 titlecontentpublish_dateauthor 等字段。

三、为特定字段配置 IK 分词器

IK 分词器是 Elasticsearch 中常用的中文分词器,支持细粒度和智能分词。要在模板中为特定字段配置 IK 分词器,需要在字段映射中指定分词器。

3.1 安装 IK 分词器插件

在使用 IK 分词器之前,需要先安装插件。

# 下载 IK 分词器插件
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.10.2/elasticsearch-analysis-ik-7.10.2.zip

# 重启 Elasticsearch

请根据您的 Elasticsearch 版本选择对应的 IK 分词器版本。

3.2 配置字段使用 IK 分词器

在模板的字段映射中,指定 analyzersearch_analyzer

"mappings": {
  "_source": {
    "enabled": true
  },
  "properties": {
    "title": {
      "type": "text",
      "analyzer": "ik_max_word",
      "search_analyzer": "ik_smart"
    },
    "content": {
      "type": "text",
      "analyzer": "ik_max_word",
      "search_analyzer": "ik_smart"
    },
    "publish_date": {
      "type": "date",
      "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
    },
    "author": {
      "type": "keyword"
    }
  }
}

说明:

  • analyzer:索引时使用的分词器,设置为 ik_max_word,会将文本进行最细粒度的切分。
  • search_analyzer:搜索时使用的分词器,设置为 ik_smart,会进行最粗粒度的切分,提升搜索性能。

3.3 更新模板

将模板更新为包含 IK 分词器配置的版本。

PUT _template/articles_template
{
  "index_patterns": ["articles_*"],
  "order": 1,
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 1
  },
  "mappings": {
    "_source": {
      "enabled": true
    },
    "properties": {
      "title": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart"
      },
      "content": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart"
      },
      "publish_date": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
      },
      "author": {
        "type": "keyword"
      }
    }
  }
}

四、调整模板细节优化查询

为了使数据查询更加方便和高效,我们可以根据业务需求调整模板的细节配置。

4.1 添加多字段(Multi-fields)

有时候,我们需要对同一字段使用不同的分词器或数据类型进行索引。例如,对 title 字段,既需要全文搜索,又需要精确匹配。

"title": {
  "type": "text",
  "analyzer": "ik_max_word",
  "search_analyzer": "ik_smart",
  "fields": {
    "keyword": {
      "type": "keyword",
      "ignore_above": 256
    }
  }
}

说明:

  • fields.keyword:为 title 字段添加一个子字段 keyword,类型为 keyword,用于精确匹配或排序。

4.2 配置拼音分词器(可选)

如果需要支持中文拼音搜索,可以配置拼音分词器。

"content": {
  "type": "text",
  "analyzer": "ik_max_word",
  "search_analyzer": "ik_smart",
  "fields": {
    "pinyin": {
      "type": "text",
      "analyzer": "pinyin_analyzer"
    }
  }
}

需要在 settings 中定义自定义的 pinyin_analyzer

"settings": {
  "analysis": {
    "analyzer": {
      "pinyin_analyzer": {
        "tokenizer": "my_pinyin"
      }
    },
    "tokenizer": {
      "my_pinyin": {
        "type": "pinyin",
        "keep_first_letter": false,
        "keep_separate_first_letter": false,
        "keep_full_pinyin": true,
        "keep_original": false,
        "limit_first_letter_length": 16,
        "lowercase": true,
        "remove_duplicated_term": true
      }
    }
  }
}

注意:使用拼音分词器需要安装对应的插件,并根据需求调整配置。

4.3 配置字段的拷贝(Copy to)

使用 copy_to 可以将多个字段的内容合并到一个字段,方便统一搜索。

"properties": {
  "title": {
    "type": "text",
    "analyzer": "ik_max_word",
    "search_analyzer": "ik_smart",
    "copy_to": "combined_fields"
  },
  "content": {
    "type": "text",
    "analyzer": "ik_max_word",
    "search_analyzer": "ik_smart",
    "copy_to": "combined_fields"
  },
  "combined_fields": {
    "type": "text",
    "analyzer": "ik_max_word",
    "search_analyzer": "ik_smart"
  }
}

说明

  • copy_to:将 titlecontent 字段的内容复制到 combined_fields 字段。
  • combined_fields:新建的字段,用于统一搜索。

五、基于数据类型的通用模板示例

为了方便管理不同类型的数据,可以根据数据类型创建通用模板。

5.1 通用模板结构

PUT _template/general_template
{
  "index_patterns": ["*"],
  "order": 0,
  "settings": {
    "number_of_shards": 5,
    "number_of_replicas": 1,
    "analysis": {
      "analyzer": {
        "ik_max_word": {
          "type": "ik_max_word"
        },
        "ik_smart": {
          "type": "ik_smart"
        }
      }
    }
  },
  "mappings": {
    "_source": {
      "enabled": true
    },
    "dynamic_templates": [
      {
        "strings": {
          "match_mapping_type": "string",
          "mapping": {
            "type": "text",
            "analyzer": "ik_max_word",
            "search_analyzer": "ik_smart",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      },
      {
        "longs": {
          "match_mapping_type": "long",
          "mapping": {
            "type": "long"
          }
        }
      },
      {
        "doubles": {
          "match_mapping_type": "double",
          "mapping": {
            "type": "double"
          }
        }
      },
      {
        "dates": {
          "match_mapping_type": "date",
          "mapping": {
            "type": "date",
            "format": "strict_date_optional_time||epoch_millis"
          }
        }
      },
      {
        "booleans": {
          "match_mapping_type": "boolean",
          "mapping": {
            "type": "boolean"
          }
        }
      }
    ]
  }
}

说明:

  • dynamic_templates:动态模板,根据字段的数据类型自动应用映射。
    • strings:将所有字符串类型字段映射为 keyword 类型,适用于精确匹配。
    • longsdoublesdatesbooleans:分别处理对应的数据类型。

5.2 使用通用模板

当没有特定的模板匹配时,Elasticsearch 会应用通用模板 general_template,根据字段的数据类型自动映射。

示例:

PUT my_index
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text",
        "analyzer": "ik_max_word"
      }
    }
  }
}
  • name 字段将使用指定的映射。
  • 其他未指定的字段将根据通用模板中的动态模板进行映射。

六、总结

通过自定义模板,我们可以统一管理 Elasticsearch 索引的配置,满足不同的业务需求。为特定字段配置 IK 分词器,可以提升中文全文检索的效果。调整模板细节,如添加多字段、配置拼音分词器、使用 copy_to 等,可以优化查询性能和用户体验。最后,使用基于数据类型的通用模板,可以简化索引管理,提高开发效率。

建议

  • 根据具体业务需求,灵活调整模板配置。
  • 在生产环境中,谨慎更新模板和映射,避免影响现有数据。
  • 定期优化和维护索引,确保 Elasticsearch 的高效运行。

参考资料

希望本文对您在 Elasticsearch 的使用和配置上有所帮助!