首页 > 解决方案 > Elasticsearch:无法将参数 [analyzer] 从 [default] 更新为 [autocomplete]

问题描述

因此,伙计们,我从 elasticsearch 开始学习,并开始尝试在我的机器上使用 edge - ngram 标记器构建自动完成功能,并在尝试遵循此类配置的文档时发现了一个问题。

我按照以下步骤操作:

  1. 我按照文档要求使用标准配置创建了索引:

     {
        "settings": {
           "analysis": {
              "analyzer": {
                 "autocomplete": {
                    "tokenizer": "autocomplete",
                      "filter": [
                        "lowercase"
                      ]
                  },
                  "autocomplete_search": {
                     "tokenizer": "lowercase"
                  }
               },
               "tokenizer": {
                  "autocomplete": {
                     "type": "edge_ngram",
                     "min_gram": 2,
                     "max_gram": 10,
                     "token_chars": [
                        "letter"
                     ]
                  }
               }
            }
         }
     }
    
  2. 我通过批量向索引添加了一个项目:

     {"index":{"_index":"products","_type":"products"}}{"sku":"73792589","name":"Bruno's test","img":"73792589_5911_20201229172349.jpg,73792589_5911_20201229172349.jpg,73792589_5911_20201229172349.jpg,73792589_5911_20201229172349.jpg,73792589_5911_20201229172349.jpg,73792589_5911_20201229172349.jpg,73792589_5911_20201229172349.jpg,73792589_5911_20201229172349.jpg","avaliacao":0,"preco_de":159,"preco_por":143.1}
    
  3. 我尝试更新字段映射以引用我的自定义分析器

问题:尝试继续第 3 步时,向我返回以下错误:

[REQUEST] _mapping
{
  "properties": {
    "name": {
      "type": "text",
      "analyzer": "autocomplete",
      "search_analyzer": "autocomplete_search",
      "fields": {
        "keyword": {
            "type": "keyword",
            "ignore_above": 256
        }  
      }
    },
    "sku": {
      "type": "text",
      "analyzer": "autocomplete",
      "search_analyzer": "autocomplete_search",
      "fields": {
        "keyword": {
            "type": "keyword",
            "ignore_above": 256
        }  
      }
    }
  }
}

[RESPONSE]
{
  "error": {
    "root_cause": [
        {
            "type": "illegal_argument_exception",
            "reason": "Mapper for [name] conflicts with existing mapper:\n\tCannot update parameter [analyzer] from [default] to [autocomplete]"
        }
    ],
    "type": "illegal_argument_exception",
    "reason": "Mapper for [name] conflicts with existing mapper:\n\tCannot update parameter [analyzer] from [default] to [autocomplete]"
  },
  "status": 400
}

标签: elasticsearch

解决方案


settingsproducts索引添加 之后,您已经对数据进行了索引,而没有定义任何显式映射。因此,生成了动态映射

您的索引的映射是

{
  "products": {
    "mappings": {
      "properties": {
        "avaliacao": {
          "type": "long"
        },
        "img": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "name": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "preco_de": {
          "type": "long"
        },
        "preco_por": {
          "type": "float"
        },
        "sku": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        }
      }
    }
  }
}

现在您正在尝试更新索引的映射(在第 3 步中)。您想将analyzer参数与字段一起添加,但这无法完成。这是因为在更新索引映射时,您可以 -

  1. 向现有对象字段添加新属性
  2. 将多字段添加到现有字段
  3. 更改现有字段的映射
  4. 重命名字段

要了解有关更新映射 API的更多信息,请参阅此文档


(您需要先删除products索引)按照以下步骤对批量数据进行索引(使用适当的索引映射)

1. 第一步:索引映射

{
  "settings": {
    "analysis": {
      "analyzer": {
        "autocomplete": {
          "tokenizer": "autocomplete",
          "filter": [
            "lowercase"
          ]
        },
        "autocomplete_search": {
          "tokenizer": "lowercase"
        }
      },
      "tokenizer": {
        "autocomplete": {
          "type": "edge_ngram",
          "min_gram": 2,
          "max_gram": 10,
          "token_chars": [
            "letter"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "name": {
        "type": "text",
        "analyzer": "autocomplete",
        "search_analyzer": "autocomplete_search",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "sku": {
        "type": "text",
        "analyzer": "autocomplete",
        "search_analyzer": "autocomplete_search",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      }
    }
  }
}

第二步:索引批量数据

POST/ _bulk

{"index":{"_index":"products","_type":"products"}}
{"sku":"73792589","name":"Bruno's test","img":"73792589_5911_20201229172349.jpg,73792589_5911_20201229172349.jpg,73792589_5911_20201229172349.jpg,73792589_5911_20201229172349.jpg,73792589_5911_20201229172349.jpg,73792589_5911_20201229172349.jpg,73792589_5911_20201229172349.jpg,73792589_5911_20201229172349.jpg","avaliacao":0,"preco_de":159,"preco_por":143.1}

推荐阅读