• 作者:老汪软件技巧
  • 发表时间:2024-10-30 07:02
  • 浏览量:

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

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

目录一、Elasticsearch 模板概述

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

模板的主要作用:

二、创建自定义模板2.1 模板基本结构

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

PUT _template/your_template_name
{
  "index_patterns": ["your_index_pattern*"],
  "order": 0,
  "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"
      }
    }
  }
}

说明:

三、为特定字段配置 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 分词器

在模板的字段映射中,指定 analyzer 和 search_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"
    }
  }
}

说明:

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
    }
  }
}

说明:

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"
  }
}

说明:

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

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

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"
          }
        }
      }
    ]
  }
}

说明:

5.2 使用通用模板

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

示例:

PUT my_index
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text",
        "analyzer": "ik_max_word"
      }
    }
  }
}

六、总结

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

建议:

参考资料:

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