API

Google Knowledge Graph Search APIを触ってみる

ナレッジグラフって外部からも参照できるんですね。Google検索の右側に出てくるだけだと思ってました。ということで早速触ってみます。 準備 プロジェクトを作成してKnowledge Graph Search APIを有効 […]

広告ここから
広告ここまで

ナレッジグラフって外部からも参照できるんですね。Google検索の右側に出てくるだけだと思ってました。
ということで早速触ってみます。

準備

プロジェクトを作成してKnowledge Graph Search APIを有効にしましょう。

APIキーを発行します。

APIキーは本来利用の制限をちゃんとかけておくべきですが、試した後消すので今回は横着します。

サンプル

リクエストを出します。API_KEYを自分のキーに書き換えましょう。

$ curl https://kgsearch.googleapis.com/v1/entities:search?query=taylor+swift&key=API_KEY&limit=1&indent=True | jq .

{
  "@context": {
    "@vocab": "https://schema.org/",
    "goog": "https://schema.googleapis.com/",
    "EntitySearchResult": "goog:EntitySearchResult",
    "detailedDescription": "goog:detailedDescription",
    "resultScore": "goog:resultScore",
    "kg": "https://g.co/kg"
  },
  "@type": "ItemList",
  "itemListElement": [
    {
      "@type": "EntitySearchResult",
      "result": {
        "@id": "kg:/m/0dl567",
        "name": "Taylor Swift",
        "@type": [
          "Person",
          "Thing"
        ],
        "description": "American singer-songwriter",
        "image": {
          "contentUrl": "https://t0.gstatic.com/images?q=tbn:ANd9GcST848UJ0u31E6aoQfb2nnKZFyad7rwNF0ZLOCACGpu4jnboEzV",
          "url": "https://en.wikipedia.org/wiki/Begin_Again_(Taylor_Swift_song)"
        },
        "detailedDescription": {
          "articleBody": "Taylor Alison Swift is an American singer-songwriter. One of the world's leading contemporary recording artists, she is known for narrative songs about her personal life, which have received widespread media coverage.\n",
          "url": "https://en.wikipedia.org/wiki/Taylor_Swift",
          "license": "https://en.wikipedia.org/wiki/Wikipedia:Text_of_Creative_Commons_Attribution-ShareAlike_3.0_Unported_License"
        },
        "url": "https://taylorswift.com/"
      },
      "resultScore": 1110.446045
    }
  ]
}

queryの文字列を変えることで、他のワードを検索することもできます。

$ curl https://kgsearch.googleapis.com/v1/entities:search?query=wordcamp+us&key=API_KEY&limit=1&indent=True | jq .

{
  "@context": {
    "@vocab": "https://schema.org/",
    "goog": "https://schema.googleapis.com/",
    "EntitySearchResult": "goog:EntitySearchResult",
    "detailedDescription": "goog:detailedDescription",
    "resultScore": "goog:resultScore",
    "kg": "https://g.co/kg"
  },
  "@type": "ItemList",
  "itemListElement": [
    {
      "@type": "EntitySearchResult",
      "result": {
        "@id": "kg:/g/11f6czrlzv",
        "name": "WordCamp US",
        "@type": [
          "Event",
          "Thing"
        ],
        "description": "Event"
      },
      "resultScore": 146.830338
    }
  ]
}

戻り値はJSON-LDで帰ってきますので、解析するのは簡単そうかなと思います。

いろいろなクエリ

typeでエンテティの種類を決めれる

schema.org準拠の値を入れることで、本やイベントだけ検索ということができます。

Amazon Alexaについての本を1冊

$ curl https://kgsearch.googleapis.com/v1/entities:search?key=API_KEY&limit=1&indent=True&types=Book&query=amazon+alexa | jq .

{
  "@context": {
    "@vocab": "https://schema.org/",
    "goog": "https://schema.googleapis.com/",
    "EntitySearchResult": "goog:EntitySearchResult",
    "detailedDescription": "goog:detailedDescription",
    "resultScore": "goog:resultScore",
    "kg": "https://g.co/kg"
  },
  "@type": "ItemList",
  "itemListElement": [
    {
      "@type": "EntitySearchResult",
      "result": {
        "@id": "kg:/g/11c601vs4r",
        "name": "Amazon Echo Dot: Echo Dot User Manual: From Newbie to Expert in One Hour: Echo Dot 2nd Generation User Guide: (Amazon Echo, Amazon Dot, Echo Dot, ... Manual, Alexa, User Manual, Echo Dot ebook)",
        "@type": [
          "Book",
          "Thing"
        ],
        "description": "Book by Ray Higgins"
      },
      "resultScore": 8.12117
    }
  ]
}

WordCampイベントを1件

$ curl https://kgsearch.googleapis.com/v1/entities:search?key=API_KEY&limit=1&indent=True&types=Event&query=wordcamp | jq .

{
  "@context": {
    "@vocab": "https://schema.org/",
    "goog": "https://schema.googleapis.com/",
    "EntitySearchResult": "goog:EntitySearchResult",
    "detailedDescription": "goog:detailedDescription",
    "resultScore": "goog:resultScore",
    "kg": "https://g.co/kg"
  },
  "@type": "ItemList",
  "itemListElement": [
    {
      "@type": "EntitySearchResult",
      "result": {
        "@id": "kg:/g/11f618wjsz",
        "name": "WordCamp Karachi",
        "@type": [
          "Event",
          "Thing"
        ],
        "description": "Event"
      },
      "resultScore": 8.659028
    }
  ]
}

つかいどころ

そもそもナレッジグラフに対応したコンテンツがどれくらいあるのかというところがボトルネックになりそうな気はします。ただ、うまくいけばGoogle経由でいろんなデータが引けるようになるので便利かなと思います。

ブックマークや限定記事(予定)など

WP Kyotoサポーター募集中

WordPressやフロントエンドアプリのホスティング、Algolia・AWSなどのサービス利用料を支援する「WP Kyotoサポーター」を募集しています。
月額または年額の有料プランを契約すると、ブックマーク機能などのサポーター限定機能がご利用いただけます。

14日間のトライアルも用意しておりますので、「このサイトよく見るな」という方はぜひご検討ください。

広告ここから
広告ここまで

Related Category posts