代码之家  ›  专栏  ›  技术社区  ›  I lebzdel I

如何为单个字段添加char_filter

  •  0
  • I lebzdel I  · 技术社区  · 1 年前

    我有这个索引,它包含字段“firstName”、“lastName”、”patronymic“、”birthDate“,我需要为字段“birthDate”添加char_filter。 例如,1939-02-21必须是1939.02.21。 所有字段都是文本。

    是否可以创建影响“出生日期”而不影响其他字段的char_filter?

    1 回复  |  直到 1 年前
        1
  •  0
  •   HappyCoding    1 年前

    在Elasticsearch中,您可以通过使用字符过滤器来实现这一点。但是,请注意,Elasticsearch中的字符过滤器在索引时工作,而不是在查询时工作。这意味着字符过滤器所做的更改将应用于存储的数据,而不是应用于查询项。

    以下是如何使用自定义字符过滤器为“birthDate”字段创建索引的示例:

    PUT /your_index
    {
      "settings": {
        "analysis": {
          "char_filter": {
            "birthDate_filter": {
              "type": "pattern_replace",
              "pattern": "(\\d{4})-(\\d{2})-(\\d{2})",
              "replacement": "$1.$2.$3"
            }
          },
          "analyzer": {
            "custom_analyzer": {
              "type": "custom",
              "tokenizer": "standard",
              "char_filter": ["birthDate_filter"],
              "filter": ["lowercase"]
            }
          }
        }
      },
      "mappings": {
        "properties": {
          "firstName": {
            "type": "text",
            "analyzer": "custom_analyzer"
          },
          "lastName": {
            "type": "text",
            "analyzer": "custom_analyzer"
          },
          "patronymic": {
            "type": "text",
            "analyzer": "custom_analyzer"
          },
          "birthDate": {
            "type": "text",
            "analyzer": "custom_analyzer"
          }
        }
      }
    }
    

    说明:

    • 这个 char_filter 名为“birthDate_filter”的使用 pattern_replace 键入以将“birthDate”字段中的连字符替换为句点。
    • 这个 analyzer 名为“custom_analyzer”的字段被配置为对“birthDate”字段使用“birthDate_filter”字符筛选器,它还使用标准的标记符和小写筛选器。

    这样,当您为文档编制索引时,“birthDate”字段将根据Char Filter模式进行转换,分析器将应用于所有文本字段。

    请记住根据您的实际要求调整索引名称、字段名称和任何其他设置。