Elasticsearch:如何在字段上添加语言分析器?

Elasticsearch: how to add language analyser on a field?

我有一个法语单词索引。我想对索引属性应用分析器。假设我有一个 title 属性,我想将其视为 "french property"。我试过这个(在 kibana 中):

PUT thing/_mappings/thing
{
  "properties": {
    "title": {
      "type": "text",
      "analyzer": "french",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    }
  }
}

但结果是:

{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "Mapper for [title] conflicts with existing mapping in other types:\n[mapper [title] has different [analyzer]]"
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "Mapper for [title] conflicts with existing mapping in other types:\n[mapper [title] has different [analyzer]]"
  },
  "status": 400
}

我不明白为什么会出现此错误。如果我显示映射(GET thing/_mappings),它不包含现有的分析器(除非我误解了什么):

 // ...
    "title": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    }
  }

我怎么能把我的 title 属性 视为法国人 属性?(来源:https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-lang-analyzer.html

title字段的分析器不能更改,如果在创建字段时未指定,则默认为standard

您需要删除索引,更改映射以满足您的需要,然后重新索引数据。

另一种解决方案是使用适当的分析器将另一个子字段添加到 title 字段:

PUT thing/_mappings/thing
{
  "properties": {
    "title": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        },
        "french": {                 <--- add this
          "type": "text",
          "analyzer": "french"
        }
      }
    }
  }
}

完成运行后,您无需重新上传所有 1GB 数据,只需调用

POST thing/_update_by_query

以便选择新的子字段。

第二种方法的唯一缺点是,如果您不需要 standard 分析器的 title 字段,您最终会得到比需要更多的分析数据。由你决定。