在elasticsearch-dsl-py中定义parent



我试图使用Elasticsearch-dsl-py索引一些数据从json文件与许多字段。忽略不太通用的部分,代码看起来像这样:

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")

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

非常欢迎任何帮助!

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

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

相关内容

最新更新