במדריך הזה נסביר איך להשתמש ב-Google Knowledge Graph Search API. במדריך לתחילת העבודה הזה, משתמשים ב-API כדי לחפש ישויות בתרשים הידע של Google או כדי לאחזר מידע עליהן.
אם אתם מתכננים פרויקט חדש, כדאי לבנות את האפליקציה באמצעות Cloud Knowledge Graph Advanced edition כדי ליהנות מהתכונות החדשות ומהשיפורים בשירות. מהדורת Basic עדיין זמינה, אבל היא לא תומכת בתכונות חדשות, ב-QPS גבוה או בתקני אבטחה ותאימות נוספים.
חיפוש ישויות ב-Cloud Knowledge Graph
בדוגמאות הבאות מוסבר איך לחפש ישות בתרשים הידע.
Advanced
REST
כדי לחפש ב-Knowledge Graph במהדורת Advanced, מפעילים את השיטה projects.locations.cloudKnowledgeGraphEntities.search.
לפני שמשתמשים בנתוני הבקשה, צריך להחליף את הנתונים הבאים:
- PROJECT_ID: מזהה הפרויקט ב- Google Cloud .
- LOCATION: מיקום ב-Knowledge Graph.
- אפשרויות:
global– נקודת קצה גלובלית
- אפשרויות:
- SEARCH_QUERY: מחרוזת השאילתה המילולית לחיפוש.
- LANGUAGES: (אופציונלי) רשימת קודי שפה (מוגדרים בתקן ISO 693) להרצת השאילתה. אם לא מציינים ערך, ערך ברירת המחדל הוא
en. - TYPES: (אופציונלי) מגביל את הישויות שמוחזרות לסוגים האלה, כפי שמוגדר בכתובת `https://schema.org`. אם מציינים כמה סוגים, הישויות שמוחזרות יכללו סוג אחד או יותר מהסוגים האלה. ערך ברירת המחדל הוא ריק, ולכן יוחזרו ישויות ללא הגבלות על הסוג. בהיררכיה המלאה של Schema.org מפורטים הסוגים הנתמכים.
- LIMIT: (אופציונלי) מגביל את מספר הישויות שיוחזרו. ערך ברירת המחדל הוא
20.
ה-method של ה-HTTP וכתובת ה-URL:
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, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.
בסיסי
REST
כדי לחפש ב-Knowledge Graph במהדורת Basic, מפעילים את השיטה projects.locations.publicKnowledgeGraphEntities.search.
לפני שמשתמשים בנתוני הבקשה, צריך להחליף את הנתונים הבאים:
- PROJECT_ID: מזהה הפרויקט ב- Google Cloud .
- LOCATION: מיקום ב-Knowledge Graph.
- אפשרויות:
global– נקודת קצה גלובלית
- אפשרויות:
- SEARCH_QUERY: מחרוזת השאילתה המילולית לחיפוש.
- LANGUAGES: (אופציונלי) רשימת קודי שפה (מוגדרים בתקן ISO 693) להרצת השאילתה. אם לא מציינים ערך, ערך ברירת המחדל הוא
en. - TYPES: (אופציונלי) מגביל את הישויות שמוחזרות לסוגים האלה, כפי שמוגדר בכתובת `https://schema.org`. אם מציינים כמה סוגים, הישויות שמוחזרות יכללו סוג אחד או יותר מהסוגים האלה. ערך ברירת המחדל הוא ריק, ולכן יוחזרו ישויות ללא הגבלות על הסוג. בהיררכיה המלאה של Schema.org מפורטים הסוגים הנתמכים.
- LIMIT: (אופציונלי) מגביל את מספר הישויות שיוחזרו. ערך ברירת המחדל הוא
20.
ה-method של ה-HTTP וכתובת ה-URL:
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, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.
חיפוש ישויות מ-MID של Cloud Knowledge Graph
Google Knowledge Graph Search API מציג את הפורמט החדש של מזהה מכונה (MID) כדי להקל על שילוב מוצרים ואפליקציות. ממשק lookup API פועל גם עם ה-MID החדש של תרשים הידע ב-Cloud (מתחיל ב-c-) וגם עם ה-MID של תרשים הידע של Google (מתחיל ב-/m).
Advanced
REST
כדי לחפש ישות לפי MID במהדורת Advanced, צריך להפעיל את השיטה projects.locations.cloudKnowledgeGraphEntities.lookup.
לפני שמשתמשים בנתוני הבקשה, צריך להחליף את הנתונים הבאים:
- PROJECT_ID: מזהה הפרויקט ב- Google Cloud .
- LOCATION: מיקום ב-Knowledge Graph.
- אפשרויות:
global– נקודת קצה גלובלית
- אפשרויות:
- LOOKUP_IDS: רשימת מזהי הישויות שמשמשים לחיפוש.
- לדוגמה:
/m/0dl567
- לדוגמה:
- LANGUAGES: (אופציונלי) רשימת קודי שפה (מוגדרים בתקן ISO 693) להרצת השאילתה. אם לא מציינים ערך, ערך ברירת המחדל הוא
en.
ה-method של ה-HTTP וכתובת ה-URL:
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, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.
בסיסי
REST
כדי לחפש ישות לפי MID במהדורת Basic, צריך לבצע קריאה ל-method projects.locations.publicKnowledgeGraphEntities.lookup.
לפני שמשתמשים בנתוני הבקשה, צריך להחליף את הנתונים הבאים:
- PROJECT_ID: מזהה הפרויקט ב- Google Cloud .
- LOCATION: מיקום ב-Knowledge Graph.
- אפשרויות:
global– נקודת קצה גלובלית
- אפשרויות:
- LOOKUP_IDS: רשימת מזהי הישויות שמשמשים לחיפוש.
- לדוגמה:
/m/0dl567
- לדוגמה:
- LANGUAGES: (אופציונלי) רשימת קודי שפה (מוגדרים בתקן ISO 693) להרצת השאילתה. אם לא מציינים ערך, ערך ברירת המחדל הוא
en.
ה-method של ה-HTTP וכתובת ה-URL:
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, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.
מבנה הישות
| שם השדה | סוג | תיאור |
|---|---|---|
@id |
string |
ה-URI הקנוני של הישות. |
name |
string |
שם הישות. |
@type |
array |
רשימת הסוגים הנתמכים של schema.org שתואמים לישות. |
description |
string |
תיאור קצר של הישות. |
image |
URL |
תמונה שתעזור לזהות את הישות. |
detailedDescription |
string |
תיאור מפורט של הישות. |
url |
URL |
כתובת האתר הרשמי של הישות, אם יש כזו. |
identifier |
array |
רשימה של מזהים מקושרים אחרים, למשל WikidataQID. |
מילות מפתח של JSON-LD
למידע על מילות מפתח של JSON-LD כמו:
תאימות ל-Schema.org
לכל אחד מהסוגים (כמו Person) והמאפיינים (כמו name) של Schema.org יש URI מלא תואם, כמו בדוגמאות הבאות:
| מחלקה | שם | URI |
|---|---|---|
| סוג | Person |
https://schema.org/Person |
| סוג | Thing |
https://schema.org/Thing |
| מאפיין (property) | name |
https://schema.org/name |
| מאפיין (property) | description |
https://schema.org/description |
תוספי סכימה
הסוגים והמאפיינים שמשמשים ב-API הזה מתארחים בכתובת https://schema.googleapis.com כתוספים חיצוניים.
התיעוד של כל סוג ונכס זמין במזהה ה-URI המתאים.
| מחלקה | שם | URI |
|---|---|---|
| סוג | EntitySearchResult |
https://schema.googleapis.com/EntitySearchResult |
| מאפיין (property) | detailedDescription |
https://schema.googleapis.com/detailedDescription |
ישויות של Knowledge Graph
ב-Knowledge Graph יש מיליוני ערכים שמתארים ישויות מהעולם האמיתי, כמו אנשים, מקומות ודברים. הישויות האלה יוצרות את הצמתים של הגרף.
אלה כמה מהסוגים הנפוצים של ישויות שנמצאות ב-Cloud Knowledge Graph:
BookBookSeriesEducationalOrganizationEventGovernmentOrganizationLocalBusinessMovieMovieSeriesMusicAlbumMusicGroupMusicRecordingOrganizationPeriodicalPersonPlaceSportsTeamTVEpisodeTVSeriesVideoGameVideoGameSeriesWebSite