json - 提取twitter json以使用python进行 Elasticsearch 时映射字段类型

标签 json python-3.x curl elasticsearch twitter

我正在使用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/

相关文章:

javascript - Spotify 专辑的长度属性未定义?

python - python3中的日期时间到十进制小时和分钟

PHP CURL 和 ssl 认证基础

javascript - 使用带有嵌套对象的 javascript 和 json 发布表单

python - 使用 Python 指定 JSON 对象中的键

jquery - 使用 jQuery 通过 .ajax() 传递 JSON

python - 处理大小为 10**6 的列表的时间复杂度

python-3.x - 用于Elasticsearch的Python客户端的正则表达式支持有问题

php - 使用php下载文件并将其保存到数据库

php - 如何防止PHP发送的HTTP请求出现403错误?