本快速入門導覽課程將介紹 Google Knowledge Graph Search API。在本快速入門導覽課程中,您將使用 API 在 Google 知識圖譜中搜尋或查詢實體。
如果您正在規劃新專案,請使用 Cloud Knowledge Graph Advanced 版建構應用程式,才能利用新功能與改善的服務項目。 基本版仍可使用,但不支援新功能、高 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
如要搜尋 Basic 版的知識圖譜,請呼叫 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 知識圖譜 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 查閱 Basic 版的實體,請呼叫 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 關鍵字
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 Knowledge Graph 中常見的實體類型:
BookBookSeriesEducationalOrganizationEventGovernmentOrganizationLocalBusinessMovieMovieSeriesMusicAlbumMusicGroupMusicRecordingOrganizationPeriodicalPersonPlaceSportsTeamTVEpisodeTVSeriesVideoGameVideoGameSeriesWebSite