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

rap*_*2-h 3 mapping elasticsearch kibana

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

PUT thing/_mappings/thing
{
  "properties": {
    "title": {
      "type": "text",
      "analyzer": "french",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

但结果是:

{
  "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
}
Run Code Online (Sandbox Code Playgroud)

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

 // ...
    "title": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    }
  }
Run Code Online (Sandbox Code Playgroud)

我如何将我的title财产视为法国财产?(来源:https : //www.elastic.co/guide/en/elasticsearch/reference/current/analysis-lang-analyzer.html

Val*_*Val 5

不允许更改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"
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

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

POST thing/_update_by_query
Run Code Online (Sandbox Code Playgroud)

以获取新的子字段。

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