elasticsearch,Python,elasticsearch" /> elasticsearch,Python,elasticsearch" />

使用Python弹性客户端插入新文档引发非法\u参数\u异常

使用Python弹性客户端插入新文档引发非法\u参数\u异常,python,elasticsearch,Python,elasticsearch,我在AWS上安装了一个Elasticsearch服务,其中包含一个现有索引,我正在尝试向其中添加更多文档。我想使用Python Elasticsearch客户端与此服务交互。我能够成功地连接服务并按预期查询它。但是,当我向Elasticsearch添加新文档时,我收到以下错误: RequestError: RequestError(400, 'illegal_argument_exception', 'mapper [city] cannot be changed from type [keyw

我在AWS上安装了一个Elasticsearch服务,其中包含一个现有索引,我正在尝试向其中添加更多文档。我想使用Python Elasticsearch客户端与此服务交互。我能够成功地连接服务并按预期查询它。但是,当我向Elasticsearch添加新文档时,我收到以下错误:

RequestError: RequestError(400, 'illegal_argument_exception', 'mapper [city] cannot be changed from type [keyword] to [text]')
我是否需要为添加Elasticsearch的每个文档指定映射?我已经搜索了文档,但没有看到任何这样的例子。我希望将城市字段映射为关键字,但我不知道在上载新文档时如何指定它

以下是我当前的流程:

注意:这是输出fomo
es.info()


当您以某种方式修改接收文档时,Elasticsearch会自动为索引生成映射,然后您尝试接收不一定符合先前定义的结构(映射)的文档时,会引发此错误

要运行,请执行以下操作:

现在,缓解原始问题的下一步是删除索引并显式提供映射,以便您可以控制它:

# create a document to upload
data = [{
    'ad_id': 1053674,
    'city': 'Houston',
    'category': 'Cars',
    'date_posted': datetime.datetime(2021, 1, 29, 19, 33),
    'title': '2020 Chevrolet Silverado',
    'body': "This brand new vehicle is the perfect truck for you.",
    'phone': None
}]

mapping = '''
{
  "mappings" : {
    "properties" : {
      "ad_id" : {
        "type" : "long"
      },
      "body" : {
        "type" : "text",
        "fields" : {
          "keyword" : {
            "type" : "keyword",
            "ignore_above" : 256
          }
        }
      },
      "category" : {
        "type" : "text",
        "fields" : {
          "keyword" : {
            "type" : "keyword",
            "ignore_above" : 256
          }
        }
      },
      "city" : {
        "type" : "text",
        "fields" : {
          "keyword" : {
            "type" : "keyword",
            "ignore_above" : 256
          }
        }
      },
      "date_posted" : {
        "type" : "date"
      },
      "title" : {
        "type" : "text",
        "fields" : {
          "keyword" : {
            "type" : "keyword",
            "ignore_above" : 256
          }
        }
      }
    }
  }
}
'''

# drop the index
# es.indices.delete(index='ads', ignore=[400, 404])

# create the index w/ the mapping
es.indices.create(index='ads', ignore=400, body=mapping)

# add document to index
res = es.index(index='ads', doc_type="_doc", id=data[0]['ad_id'], body=data[0])

print(res['result'])
仅供参考-如果您打算将
城市
映射为,则在查询时只能进行精确匹配

RequestError: RequestError(400, 'illegal_argument_exception', 'mapper [city] cannot be changed from type [keyword] to [text]')
{'name': '123456789', 'cluster_name': '123456789:ads', 'cluster_uuid': '123456789', 'version': {'number': '7.9.1', 'build_flavor': 'oss', 'build_type': 'tar', 'build_hash': 'unknown', 'build_date': '2020-11-03T09:54:32.349659Z', 'build_snapshot': False, 'lucene_version': '8.6.2', 'minimum_wire_compatibility_version': '6.8.0', 'minimum_index_compatibility_version': '6.0.0-beta1'}, 'tagline': 'You Know, for Search'}
current_mapping = es.indices.get_mapping('ads')
# create a document to upload
data = [{
    'ad_id': 1053674,
    'city': 'Houston',
    'category': 'Cars',
    'date_posted': datetime.datetime(2021, 1, 29, 19, 33),
    'title': '2020 Chevrolet Silverado',
    'body': "This brand new vehicle is the perfect truck for you.",
    'phone': None
}]

mapping = '''
{
  "mappings" : {
    "properties" : {
      "ad_id" : {
        "type" : "long"
      },
      "body" : {
        "type" : "text",
        "fields" : {
          "keyword" : {
            "type" : "keyword",
            "ignore_above" : 256
          }
        }
      },
      "category" : {
        "type" : "text",
        "fields" : {
          "keyword" : {
            "type" : "keyword",
            "ignore_above" : 256
          }
        }
      },
      "city" : {
        "type" : "text",
        "fields" : {
          "keyword" : {
            "type" : "keyword",
            "ignore_above" : 256
          }
        }
      },
      "date_posted" : {
        "type" : "date"
      },
      "title" : {
        "type" : "text",
        "fields" : {
          "keyword" : {
            "type" : "keyword",
            "ignore_above" : 256
          }
        }
      }
    }
  }
}
'''

# drop the index
# es.indices.delete(index='ads', ignore=[400, 404])

# create the index w/ the mapping
es.indices.create(index='ads', ignore=400, body=mapping)

# add document to index
res = es.index(index='ads', doc_type="_doc", id=data[0]['ad_id'], body=data[0])

print(res['result'])