本快速入门为您介绍 Google Knowledge Graph Search API。 在本快速入门中,您将使用该 API 在 Google 知识图谱中搜索或查找实体。
如果您正在规划新项目,请使用 Cloud Knowledge Graph 高级版构建应用,以利用新功能和改进的服务。 基本版仍然可用,但不支持新功能、高 QPS 或其他安全合规性标准。
搜索 Cloud 知识图谱实体
以下示例展示了如何针对知识图谱搜索实体。
高级
REST
如需搜索知识图谱(高级版),请调用 projects.locations.cloudKnowledgeGraphEntities.search 方法。
在使用任何请求数据之前,请先进行以下替换:
- PROJECT_ID:您的 Google Cloud 项目 ID。
- LOCATION:知识图谱位置。
- 选项:
global- 全球端点
- 选项:
- SEARCH_QUERY:用于搜索的字面查询字符串。
- LANGUAGES:(可选)用于运行查询的语言代码(在 ISO 693 中定义)列表。如果未指定,则默认值为
en。 - TYPES:(可选)限制返回具有这些类型的实体,如 `https://schema.org` 中所定义。如果指定了多个类型,则返回的实体将包含这些类型中的一个或多个。默认值为空,表示返回的实体不受类型限制。如需了解支持的类型,请参阅 Schema.org 完整层次结构。
- LIMIT:(可选)限制要返回的实体的数量。默认值为
20。
HTTP 方法和网址:
GET https://enterpriseknowledgegraph.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/cloudKnowledgeGraphEntities:Search?query=SEARCH_QUERY&limit=LIMIT
如需发送请求,请选择以下方式之一:
curl
执行以下命令:
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://enterpriseknowledgegraph.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/cloudKnowledgeGraphEntities:Search?query=SEARCH_QUERY&limit=LIMIT"
PowerShell
执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://enterpriseknowledgegraph.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/cloudKnowledgeGraphEntities:Search?query=SEARCH_QUERY&limit=LIMIT" | Select-Object -Expand Content
响应包含实体列表,这些实体以 JSON-LD 格式呈现,并且与具有受限外部扩展的 schema.org 架构兼容。
如需详细了解响应结构,请参阅实体响应结构。
以下 JSON-LD 示例展示了响应正文的结构:
{
"@context": {
"@vocab": "http://schema.org/"
},
"@type": "ItemList",
"itemListElement": [
{
"result": {
"@id": "c-07xuup16g",
"name": "Stanford University",
"description": "Private university in Stanford, California",
"detailedDescription": {
"articleBody": "Stanford University, officially Leland Stanford Junior University, is a private research university in Stanford, California. The campus occupies 8,180 acres, among the largest in the United States, and enrolls over 17,000 students. ",
"url": "https://en.wikipedia.org/wiki/Stanford_University",
"license": "https://en.wikipedia.org/wiki/Wikipedia:Text_of_Creative_Commons_Attribution-ShareAlike_3.0_Unported_License"
},
"url": "http://www.stanford.edu/",
"image": {
"contentUrl": "https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcTfPPf-ker0y_892m1wu8-U89furQgQ67foDFncY3r9sREpeWxV",
"url": "https://es.wikipedia.org/wiki/Archivo:Logo_of_Stanford_University.png"
},
"identifier": [
{
"@type": "PropertyValue",
"propertyID": "googleKgMID",
"value": "/m/06pwq"
},
{
"@type": "PropertyValue",
"propertyID": "googlePlaceID",
"value": "ChIJneqLZyq7j4ARf2j8RBrwzSk"
},
{
"@type": "PropertyValue",
"propertyID": "wikidataQID",
"value": "Q41506"
}
],
"@type": [
"Place",
"Organization",
"MovieTheater",
"Corporation",
"EducationalOrganization",
"Thing",
"CollegeOrUniversity"
]
}
}
]
}
Python
如需了解详情,请参阅 Enterprise Knowledge Graph Python API 参考文档。
如需向 Enterprise Knowledge Graph 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
基本
REST
如需搜索知识图谱(基本版),请调用 projects.locations.publicKnowledgeGraphEntities.search 方法。
在使用任何请求数据之前,请先进行以下替换:
- PROJECT_ID:您的 Google Cloud 项目 ID。
- LOCATION:知识图谱位置。
- 选项:
global- 全球端点
- 选项:
- SEARCH_QUERY:用于搜索的字面查询字符串。
- LANGUAGES:(可选)用于运行查询的语言代码(在 ISO 693 中定义)列表。如果未指定,则默认值为
en。 - TYPES:(可选)限制返回具有这些类型的实体,如 `https://schema.org` 中所定义。如果指定了多个类型,则返回的实体将包含这些类型中的一个或多个。默认值为空,表示返回的实体不受类型限制。如需了解支持的类型,请参阅 Schema.org 完整层次结构。
- LIMIT:(可选)限制要返回的实体的数量。默认值为
20。
HTTP 方法和网址:
GET https://enterpriseknowledgegraph.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publicKnowledgeGraphEntities:Search?query=SEARCH_QUERY&limit=LIMIT
如需发送请求,请选择以下方式之一:
curl
执行以下命令:
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://enterpriseknowledgegraph.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publicKnowledgeGraphEntities:Search?query=SEARCH_QUERY&limit=LIMIT"
PowerShell
执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://enterpriseknowledgegraph.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publicKnowledgeGraphEntities:Search?query=SEARCH_QUERY&limit=LIMIT" | Select-Object -Expand Content
响应包含实体列表,这些实体以 JSON-LD 格式呈现,并且与具有受限外部扩展的 schema.org 架构兼容。
如需详细了解响应结构,请参阅实体响应结构。
以下 JSON-LD 示例展示了响应正文的结构:
{
"@context": {
"@vocab": "http://schema.org/"
},
"@type": "ItemList",
"itemListElement": [
{
"result": {
"@id": "c-07xuup16g",
"name": "Stanford University",
"description": "Private university in Stanford, California",
"detailedDescription": {
"license": "https://en.wikipedia.org/wiki/Wikipedia:Text_of_Creative_Commons_Attribution-ShareAlike_3.0_Unported_License",
"url": "https://en.wikipedia.org/wiki/Stanford_University",
"articleBody": "Stanford University, officially Leland Stanford Junior University, is a private research university in Stanford, California. The campus occupies 8,180 acres, among the largest in the United States, and enrolls over 17,000 students. "
},
"url": "http://www.stanford.edu/",
"identifier": [
{
"@type": "PropertyValue",
"propertyID": "googleKgMID",
"value": "/m/06pwq"
}
],
"image": {
"contentUrl": "https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcTfPPf-ker0y_892m1wu8-U89furQgQ67foDFncY3r9sREpeWxV",
"url": "https://es.wikipedia.org/wiki/Archivo:Logo_of_Stanford_University.png"
},
"@type": [
"EducationalOrganization",
"CollegeOrUniversity",
"Thing",
"Place",
"Corporation",
"MovieTheater",
"Organization"
]
}
}
]
}
Python
如需了解详情,请参阅 Enterprise Knowledge Graph Python API 参考文档。
如需向 Enterprise Knowledge Graph 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
通过 Cloud Knowledge Graph MID 查找实体
Google Knowledge Graph Search API 引入了新的机器 ID (MID) 格式,以便更轻松地集成产品和应用。lookup API 适用于新的 Cloud Knowledge Graph MID(以 c- 开头)以及 Google Knowledge Graph MID(以 /m 开头)。
高级
REST
如需在高级版中按 MID 查找实体,请调用 projects.locations.cloudKnowledgeGraphEntities.lookup 方法。
在使用任何请求数据之前,请先进行以下替换:
- PROJECT_ID:您的 Google Cloud 项目 ID。
- LOCATION:知识图谱位置。
- 选项:
global- 全球端点
- 选项:
- LOOKUP_IDS:用于查找的实体 ID 列表。
- 示例:
/m/0dl567
- 示例:
- LANGUAGES:(可选)用于运行查询的语言代码(在 ISO 693 中定义)列表。如果未指定,则默认值为
en。
HTTP 方法和网址:
GET https://enterpriseknowledgegraph.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/cloudKnowledgeGraphEntities:Lookup?ids=LOOKUP_IDS
如需发送请求,请选择以下方式之一:
curl
执行以下命令:
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://enterpriseknowledgegraph.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/cloudKnowledgeGraphEntities:Lookup?ids=LOOKUP_IDS"
PowerShell
执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://enterpriseknowledgegraph.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/cloudKnowledgeGraphEntities:Lookup?ids=LOOKUP_IDS" | Select-Object -Expand Content
响应包含实体列表,这些实体以 JSON-LD 格式呈现,并且与具有受限外部扩展的 schema.org 架构兼容。
如需详细了解响应结构,请参阅实体响应结构。
以下 JSON-LD 示例展示了响应正文的结构:
{
"@context": {
"@vocab": "http://schema.org/"
},
"@type": "ItemList",
"itemListElement": [
{
"result": {
"description": "American singer-songwriter",
"@type": [
"Person",
"Thing"
],
"name": "Taylor Swift",
"@id": "c-0260160kc",
"url": "http://www.taylorswift.com/",
"identifier": [
{
"@type": "PropertyValue",
"propertyID": "googleKgMID",
"value": "/m/0dl567"
},
{
"@type": "PropertyValue",
"propertyID": "wikidataQID",
"value": "Q26876"
}
],
"detailedDescription": {
"articleBody": "Taylor Alison Swift is an American singer-songwriter. Her discography spans multiple genres and her narrative songwriting—often inspired by her personal life—has received critical praise and widespread media coverage. ",
"license": "https://en.wikipedia.org/wiki/Wikipedia:Text_of_Creative_Commons_Attribution-ShareAlike_3.0_Unported_License",
"url": "https://en.wikipedia.org/wiki/Taylor_Swift"
},
"image": {
"contentUrl": "https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcSnsjOArwgD-bhyRslj_Qa7Z2tIPLRFU3VnEuLr1ybcyned49Pt",
"url": "https://pt.wikipedia.org/wiki/Ficheiro:191125_Taylor_Swift_at_the_2019_American_Music_Awards_(cropped).png"
}
}
}
]
}
https://enterpriseknowledgegraph.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/cloudKnowledgeGraphEntities:Lookup?ids=LOOKUP_IDS&ids=LOOKUP_IDS
Python
如需了解详情,请参阅 Enterprise Knowledge Graph Python API 参考文档。
如需向 Enterprise Knowledge Graph 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
基本
REST
如需按 MID 查找实体(针对基本版),请调用 projects.locations.publicKnowledgeGraphEntities.lookup 方法。
在使用任何请求数据之前,请先进行以下替换:
- PROJECT_ID:您的 Google Cloud 项目 ID。
- LOCATION:知识图谱位置。
- 选项:
global- 全球端点
- 选项:
- LOOKUP_IDS:用于查找的实体 ID 列表。
- 示例:
/m/0dl567
- 示例:
- LANGUAGES:(可选)用于运行查询的语言代码(在 ISO 693 中定义)列表。如果未指定,则默认值为
en。
HTTP 方法和网址:
GET https://enterpriseknowledgegraph.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publicKnowledgeGraphEntities:Lookup?ids=LOOKUP_IDS
如需发送请求,请选择以下方式之一:
curl
执行以下命令:
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://enterpriseknowledgegraph.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publicKnowledgeGraphEntities:Lookup?ids=LOOKUP_IDS"
PowerShell
执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://enterpriseknowledgegraph.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publicKnowledgeGraphEntities:Lookup?ids=LOOKUP_IDS" | Select-Object -Expand Content
响应包含实体列表,这些实体以 JSON-LD 格式呈现,并且与具有受限外部扩展的 schema.org 架构兼容。
如需详细了解响应结构,请参阅实体响应结构。
以下 JSON-LD 示例展示了响应正文的结构:
{
"@context": {
"@vocab": "http://schema.org/"
},
"@type": "ItemList",
"itemListElement": [
{
"result": {
"description": "American singer-songwriter",
"@type": [
"Person",
"Thing"
],
"name": "Taylor Swift",
"@id": "c-0260160kc",
"url": "http://www.taylorswift.com/",
"identifier": [
{
"@type": "PropertyValue",
"propertyID": "googleKgMID",
"value": "/m/0dl567"
}
],
"detailedDescription": {
"articleBody": "Taylor Alison Swift is an American singer-songwriter. Her discography spans multiple genres and her narrative songwriting—often inspired by her personal life—has received critical praise and widespread media coverage. ",
"license": "https://en.wikipedia.org/wiki/Wikipedia:Text_of_Creative_Commons_Attribution-ShareAlike_3.0_Unported_License",
"url": "https://en.wikipedia.org/wiki/Taylor_Swift"
},
"image": {
"contentUrl": "https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcSnsjOArwgD-bhyRslj_Qa7Z2tIPLRFU3VnEuLr1ybcyned49Pt",
"url": "https://pt.wikipedia.org/wiki/Ficheiro:191125_Taylor_Swift_at_the_2019_American_Music_Awards_(cropped).png"
}
}
}
]
}
https://enterpriseknowledgegraph.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publicKnowledgeGraphEntities:Lookup?ids=LOOKUP_IDS&ids=LOOKUP_IDS
Python
如需了解详情,请参阅 Enterprise Knowledge Graph Python API 参考文档。
如需向 Enterprise Knowledge Graph 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
实体结构
| 字段名称 | 类型 | 说明 |
|---|---|---|
@id |
string |
实体的规范 URI。 |
name |
string |
实体的名称。 |
@type |
array |
与实体匹配的支持的 schema.org 类型列表。 |
description |
string |
实体的简短说明。 |
image |
URL |
有助于识别实体的图片。 |
detailedDescription |
string |
实体的详细说明。 |
url |
URL |
实体的官方网站网址(如有)。 |
identifier |
array |
其他关联 ID 的列表,例如 WikidataQID。 |
JSON-LD 关键字
如需了解 JSON-LD 关键字(例如:),请参阅 JSON-LD 规范。
Schema.org 兼容性
对于每种 Schema.org 类型(例如 Person)和属性(例如 name),都有一个对应的完整 URI,如以下示例所示:
| 类 | 名称 | URI |
|---|---|---|
| 类型 | Person |
https://schema.org/Person |
| 类型 | Thing |
https://schema.org/Thing |
| 属性 | name |
https://schema.org/name |
| 属性 | description |
https://schema.org/description |
架构扩展
此 API 中使用的类型和属性以外部扩展程序的形式托管在 https://schema.googleapis.com。
每种类型和属性的文档都可在相应的 URI 中找到。
| 类 | 名称 | URI |
|---|---|---|
| 类型 | EntitySearchResult |
https://schema.googleapis.com/EntitySearchResult |
| 属性 | detailedDescription |
https://schema.googleapis.com/detailedDescription |
知识图谱实体
知识图谱包含数百万个条目,用于描述现实世界中的实体,例如人物、地点和事物。这些实体构成了图的节点。
以下是 Cloud 知识图谱中常见的一些实体类型:
BookBookSeriesEducationalOrganizationEventGovernmentOrganizationLocalBusinessMovieMovieSeriesMusicAlbumMusicGroupMusicRecordingOrganizationPeriodicalPersonPlaceSportsTeamTVEpisodeTVSeriesVideoGameVideoGameSeriesWebSite