我正在使用Python脚本将Twitter对象中的JSON对象加载到 flex 搜索实例中。
列表包含作为JSON对象的推文,这些推文被解析为 flex 搜索。
import elasticsearch
import json
import requests
tweet_list = request(get_tweets_via_request)
for tweet in tweet_list:
es.index(index="twitter",doc_type="tweet",body=tweet)
在加载 flex 搜索之前,我想将
created_at
字段映射为string
而不是date
。如果我在不配置任何映射的情况下运行python脚本,则ES会将created_at
字段解释为string
。{"created_at":{"type":"string"}
我正在尝试使用带有curl的curl命令在运行python脚本之前应用一些映射(映射中已删除回车符/空格):
curl -XPUT localhost:9200/twitter -d {"settings":{"index":{"number_of_shards":1}},"mappings":{"tweet":{"properties":{"created_at":{"format":"EEEMMMddHH:mm:ssZYYYY","type":"date"}}}}}
和产生的错误:
{"error":{"root_cause":[{"type":"parse_exception","reason":"failed to parse source for create index"}],"type":"parse_exception","reason":"failed to parse source for create index","caused_by":{"type":"json_parse_exception","reason":"Unrecognized token 'EEEMMMddHH': was expecting ('true', 'false' or 'null')\n at [Source: [B@14b6e4; line: 1, column: 99]"}},"status":400}
最佳答案
您可以在索引推文之前使用the following mapping来创建索引。
如您所见,created_at
字段被声明为日期,其特定日期格式与Twitter feed中的内容匹配。
PUT /twitter
{
"settings" : {
"index": {
"number_of_shards" : 1
}
},
"mappings": {
"tweet": {
"properties": {
"created_at": {
"format": "EEE MMM dd HH:mm:ss Z YYYY",
"type": "date"
},
...
}
}
}
}
关于json - 提取twitter json以使用python进行 Elasticsearch 时映射字段类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38663436/