代码之家  ›  专栏  ›  技术社区  ›  rap-2-h

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

  •  1
  • rap-2-h  · 技术社区  · 6 年前

    我有一个法语单词索引。我想对索引属性应用一个分析器。假设我有一个 title 我想把它看作是“法国财产”。我试过这个(在基巴纳):

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

    我怎样才能考虑我的 法国财产? (来源: https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-lang-analyzer.html )

    1 回复  |  直到 6 年前
        1
  •  5
  •   Val    6 年前

    不允许您更改 title 字段,即 standard 默认情况下,如果在创建字段时未指定。

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

    另一个解决方案是向 标题 使用合适的分析仪:

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

    POST thing/_update_by_query
    

    以获取新的子字段。

    第二种方法的唯一缺点是,如果您不需要 分析仪。由你决定。