【问题标题】:python elasticsearch-dsl parent child relationshippython elasticsearch-dsl父子关系
【发布时间】:2016-05-06 06:20:40
【问题描述】:

我开始使用python库elasticsearch-dsl

我正在尝试实现父子关系,但它不起作用:

    class Location(DocType):
        name = String(analyzer='snowball', fields={'raw': String(index='not_analyzed')})
        latitude = String(analyzer='snowball')
        longitude = String(analyzer='snowball')
        created_at = Date()

   class Building(DocType):
       parent = Location()

【问题讨论】:

  • 代码示例?实际问题?
  • @ScottM 更新了代码示例
  • 在关系数据库意义上实现对象关系有很多不同的选项。您可以决定在文档中嵌入文档(基本上,如果您愿意,可以在表中嵌入子表。)或者重新实现关系数据库“连接”原语。如果您解释了您正在考虑的方法、遇到的技术挑战等,将会有所帮助。您的问题太宽泛,任何人都无法给您具体的答案。

标签: python elasticsearch elasticsearch-dsl


【解决方案1】:

elasticsearch-dsl 使用MetaField 内置了父子关系:

class Location(DocType):
    name = String(analyzer='snowball', fields={'raw': String(index='not_analyzed')})
    latitude = String(analyzer='snowball')
    longitude = String(analyzer='snowball')
    created = Date()

    class Meta:
        doc_type = 'location' 

class Building(DocType):

    class Meta:
        doc_type = 'building'
        parent = MetaField(type='location')

如何插入和查询(HT to @Maresh):
- DSL 获取:ChildDoc.get(id=child_id, routing=parent_id)
- DSL 插入:我相信是child.save(id=child_id, routing=parent_id)
- 字典插入:在字典中指定'_parent': parent_id

【讨论】:

  • 你知道救孩子的时候怎么测试parent_id吗,我在纠结这个。
  • @Maresh 在字典中插入您指定的 _parent 键。搜索你做MyDoc.get(id=doc_id, routing=parent_id)
  • 所以当我保存孩子的时候是这样做的:child.save(parent=parent.meta.id) ?谢啦。用它编辑你的答案,它非常有用,文档没有说明任何内容。
  • @Maresh 我相信是child.save(id=child_id, routing=parent_id)。可以查吗?
  • 已确认。非常感谢。
【解决方案2】:

好的,谢谢大家。 对我有用的简单而混乱的解决方案是使用:

from elasticsearch_dsl import Mapping

mcc = Mapping(typeChild)
mcc.meta('_parent', type=typeParent)
mcc.field(fieldName, 'string', fielddata=True, store=True)
mcc.save(index)

在创建父文档类型之前

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多