You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Weaviate查询中如何正确包含交叉引用属性wrotePublications?

问题

在Weaviate数据库中有两个集合Researcher和Publication,其中Researcher集合的wrotePublications属性是指向Publication的交叉引用。尝试查询Researcher并包含wrotePublications属性时,收到错误:

{
  "errors": [
    {
      "locations": [
        {
          "column": 217,
          "line": 1
        }
      ],
      "message": "Field \"wrotePublications\" of type \"[ResearcherWrotePublicationsObj]\" must have a sub selection.",
      "path": null
    }
  ]
}

相关定义与数据如下:

Schema定义

publication_class_schema = {
    "class": publication_class_name,
    "description": "Publication information",
    "properties": [
        {
            "name": "content",
            "dataType": ["text"],
            "description": "Content of the publication",
        },
        {
            "name": "title",
            "dataType": ["text"],
            "description": "Title of the publication",
        },
        . . .
        {
            "name": "citedby",
            "dataType": ["int"],
            "description": "Total number of citations of the publication",
        },
    ],
}

researcher_class_schema = {
    "class": researcher_class_name,
    "description": "Researcher profile information and statistics",
    "properties": [
        {
            "name": "name",
            "dataType": ["text"],
            "description": "The name of the researcher",
        },
        . . .
        {
            "name": "wrotePublications",
            "dataType": [publication_class_name],
            "description": "Cross-reference objects of publications writed by the researcher",
        },
    ],
}

数据导入代码

with client.batch as batch:
    researcher_obj_uuid = batch.add_data_object(researcher_obj, class_name=researcher_class_name)
    pub_obj_uuid = batch.add_data_object(publication_obj, class_name=publication_class_name)
    batch.add_reference(
        from_object_uuid=researcher_obj_uuid,
        from_object_class_name=researcher_class_name,
        from_property_name="wrotePublications",
        to_object_uuid=pub_obj_uuid,
        to_object_class_name=publication_class_name,
    )

示例数据

Researcher记录:

{'class': 'Researcher',
 'creationTimeUnix': 1683744762093,
 'id': '9f1f09f7-dc73-4a08-a914-0f6df5c8fc3f',
 'lastUpdateTimeUnix': 1683744762298,
 'properties': {
   'affiliation': 'res univ',
   'citedby': 485,
   'name': 'res name',
   'wrotePublications': [
    {'beacon': 'weaviate://localhost/Publication/8884d468-2a3a-4344-ada9-07e1876c5364',
     'href': '/v1/objects/Publication/8884d468-2a3a-4344-ada9-07e1876c5364'}
  ]
},
 'vectorWeights': None}

对应的Publication记录:

{'class': 'Publication',
 'creationTimeUnix': 1683744762092,
 'id': '8884d468-2a3a-4344-ada9-07e1876c5364',
 'lastUpdateTimeUnix': 1683744762092,
 'properties': {'citedby': 2,
  'content': '',
  'pub_year': 1910,
  'title': 'pub title'},
 'vectorWeights': None}

原错误查询代码:

client.query
   .get(researcher_class_name, ["name", "wrotePublications"]) \
   .with_near_text(nearText) \
   .with_limit(3) \
   .with_additional(['certainty']) \
   .do()

解决方案

错误原因是交叉引用属性属于对象类型,不能直接作为顶层字段返回,必须明确指定要从关联对象中获取的子属性。

基础正确查询代码

在查询时,将wrotePublications定义为包含子属性的字典,指定要返回的Publication对象属性:

result = client.query
    .get(researcher_class_name, [
        "name",
        # 指定要从关联的Publication中获取的属性
        {"wrotePublications": ["title", "citedby", "pub_year", "content"]}
    ]) \
    .with_near_text(nearText) \
    .with_limit(3) \
    .with_additional(['certainty']) \
    .do()

进阶:返回关联对象的元数据

如果需要返回关联Publication对象的元数据(如ID、创建时间),可通过_additional字段指定:

result = client.query
    .get(researcher_class_name, [
        "name",
        {
            "wrotePublications": [
                "title",
                "citedby",
                "_additional['id', 'creationTimeUnix']"
            ]
        }
    ]) \
    .with_near_text(nearText) \
    .with_limit(3) \
    .with_additional(['certainty']) \
    .do()

结果说明

执行上述查询后,返回的Researcher对象中,wrotePublications字段将包含完整的关联Publication对象属性,而非仅返回beacon链接。

内容的提问来源于stack exchange,提问作者jedrix

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 14:37:08