【问题标题】:define parent in elasticsearch-dsl-py在 elasticsearch-dsl-py 中定义父级
【发布时间】:2016-11-07 11:50:33
【问题描述】:

我正在尝试使用 Elasticsearch-dsl-py 从具有许多字段的 jsonl 文件中索引一些数据。忽略不太通用的部分,代码如下所示:

es = Elasticsearch()

for id,line in enumerate(open(jsonlfile)):
  jline = json.loads(line)
  children = jline.pop('allChildrenOfTypeX')
  res = es.index(index="mydocs", doc_type='fatherdoc', id=id, body=jline)
  for ch in children:
    res = es.index(index="mydocs", doc_type='childx', parent=id, body=ch)

尝试运行它以错误结束:

RequestError: TransportError(400, u'illegal_argument_exception', u"Can't specify parent if no parent field has been configured")

我想我需要提前告诉 es 有一个父母。但是,我不想要的是映射两者的所有字段只是为了做到这一点。

非常欢迎任何帮助!

【问题讨论】:

    标签: python elasticsearch parent-child jsonlines


    【解决方案1】:

    在创建mydocs 索引时,在定义childx 映射类型时,需要指定_parent 字段,其值为fatherdoc

    PUT mydocs
    {
      "mappings": {
        "fatherdoc": {
           "properties": {
              ... parent type fields ...
           }
        },
        "childx": {
          "_parent": {                      <---- add this
            "type": "fatherdoc" 
          },
          "properties": {
              ... parent type fields ...
          }
        }
      }
    }
    

    【讨论】:

    • 我可以在不列出所有字段的情况下这样做吗?只列出“_parent”?
    • 您需要在创建索引时执行此操作,这意味着您定义映射时。您绝对可以让 ES 在旅途中动态创建您的字段,但必须在索引第一个文档之前一开始就指定 _parent 字段。
    • 这个运气好吗?
    猜你喜欢
    • 2016-03-27
    • 1970-01-01
    • 2017-05-20
    • 1970-01-01
    • 2017-12-26
    • 2018-10-05
    • 2016-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多