【问题标题】:Bulk index django model to elastic search批量索引 django 模型到弹性搜索
【发布时间】:2017-12-21 01:50:31
【问题描述】:

我正在尝试将我的 django 模型批量索引到弹性搜索 6,我的计划是每天将其作为 cron 运行一次以更新索引。 导入请求

data = serialize('json', CapitalSheet.objects.all())

data += "\n"

r = requests.post("http://127.0.0.1:9200/capitalsheet/_bulk", json = data)

print(r.content)

我收到此错误: b'{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"The bulk request must be terminated by a newline [\\n]"}],"type":"illegal_argument_exception","reason":"The bulk request must be terminated by a newline [\\n]"},"status":400}'

如果你能提出更好的建议,我会很高兴。

【问题讨论】:

  • 批量语法有点奇怪(至少对我来说)。您需要将每个对象作为单独的行发送。
  • 你最好还是使用 python 库。

标签: django elasticsearch


【解决方案1】:

我建议查看 elasticsearch 提供的 python 库。这将使批量插入变得更容易。这是文档的链接:

https://elasticsearch-py.readthedocs.io/en/master/helpers.html#bulk-helpers

如果您想手动执行此操作,ES 批量 API 实际上需要为您要插入的每条记录使用两行代码。第一行详细说明了什么索引和操作类型,第二行是要插入的记录。例如,您的请求正文如下所示:

{ "index" : { "_index" : "test", "_type" : "type1" } }
{ "field1" : "value1" }
{ "index" : { "_index" : "test", "_type" : "type1" } }
{ "field1" : "value2" }

ES 文档在这里很好地解释了这一点: https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html

【讨论】:

    猜你喜欢
    • 2016-06-20
    • 1970-01-01
    • 2018-04-07
    • 2013-10-16
    • 2021-08-16
    • 2019-06-03
    • 1970-01-01
    • 2021-11-23
    • 2017-10-22
    相关资源
    最近更新 更多