Swiftype - Read-Only Public API を使用して検索する
Swiftype (https://swiftype.com) サーチエンジンは、サイトへスクリプト設置して利用するだけではなく、Developer API を使用してプログラムからアクセスすることも可能です。ここでは、Read-only Public API について説明します。
Read-only Public API は、クライアントサイドの JavaScript やスマートフォンなどのモバイルアプリケーションからサーチインターフェースを利用するためのAPI インターフェースです。Public Search API と、Public autocomplete API の2種類が用意されています。
API を使用するには、まずEngine Key を入手します。DASHBOAD の管理している検索エンジンの一覧から入手できるので、管理サイトにアクセスして入手してください。
Public Search API は、全文検索用の search に使用するエンドポイントです。
/api/v1/public/engines/search?q={query}&engine_key={your engine key}
/api/v1/public/engines/search?q={query}&engine_key={your engine key}
Public autocomplete API は、prefix search の suggest に使用するエンドポイントです。※ キーワードサジェストではないので注意。Search API と同じ検索オプションが提供されているが、単語でマッチするというよりは、単語の前方一致でマッチする点で、Search API とは動作が異なる。
/api/v1/public/engines/suggest?q={query}&engine_key={your engine key}
/api/v1/public/engines/suggest?q={query}&engine_key={your engine key}
curl -X GET "http://api.swiftype.com/api/v1/public/engines/search.json\ ?q=python&engine_key=xaGLFMfpLP6YUni4aZuc"
{ "errors": {}, "info": { "page": { "facets": {}, "total_result_count": 28, "per_page": 1, "num_pages": 27, "current_page": 1, "query": "python" } }, "records": { "page": [ { "highlight": {}, "info": "", "popularity": 0, "published_at": "2013-08-17T08:30:50Z", "image": "", "type": "text", "id": "5211845f8e9c0f1d86000005", "external_id": "c348e3b4aeecb408a7cfb10bea3a437aab32d65d", "engine_id": "52118454eaa0b242b800007a", "updated_at": "2013-08-19T02:35:11Z", "title": "Python でパブリックIP取得", "url": "http://kunihikokido.tumblr.com/post/58495182185/python-ip", "sections": ["Python","Snippets"], "body": "https://gist.github.com/KunihikoKido/6129368" }, .... ] }
curl -X GET "http://api.swiftype.com/api/v1/public/engines/search.json\ ?q=python&engine_key=xaGLFMfpLP6YUni4aZuc"
{ "errors": {}, "info": { "page": { "facets": { "type": { "text": 11, "link": 16 }, "sections": { "pip": 2, "search": 2, "python": 25, "django": 18, "tips": 8, "snippets": 6, "packages": 3, "frameworks": 3, "tools": 2, "security": 2 } }, "total_result_count": 28, "per_page": 1, "num_pages": 27, "current_page": 1, "query": "Python" } }, "records": { "page": [ { "highlight": {}, "info": "", "popularity": 0, "published_at": "2013-08-17T08:30:50Z", "image": "", "type": "text", "id": "5211845f8e9c0f1d86000005", "external_id": "c348e3b4aeecb408a7cfb10bea3a437aab32d65d", "engine_id": "52118454eaa0b242b800007a", "updated_at": "2013-08-19T02:35:11Z", "title": "Python でパブリックIP取得", "url": "http://kunihikokido.tumblr.com/post/58495182185/python-ip", "sections": ["Python", "Snippets"], "body": "https://gist.github.com/KunihikoKido/6129368" }, ... ] } }
curl -X GET "http://api.swiftype.com/api/v1/public/engines/search.json\ ?q=python&callback=jsonCallbackFunction&engine_key=xaGLFMfpLP6YUni4aZuc"
jsonCallbackFunction({ "errors": {}, "info": { "page": { "facets": {}, "total_result_count": 28, "per_page": 1, "num_pages": 27, "current_page": 1, "query": "python" } }, "records": { "page": [ { "highlight": {}, "info": "", "popularity": 0, "published_at": "2013-08-17T08:30:50Z", "image": "", "type": "text", "id": "5211845f8e9c0f1d86000005", "external_id": "c348e3b4aeecb408a7cfb10bea3a437aab32d65d", "engine_id": "52118454eaa0b242b800007a", "updated_at": "2013-08-19T02:35:11Z", "title": "Python でパブリックIP取得", "url": "http://kunihikokido.tumblr.com/post/58495182185/python-ip", "sections": ["Python","Snippets"], "body": "https://gist.github.com/KunihikoKido/6129368" }, .... ] } })
基本的には、Public API を使用して問題ないですが、Engine Key さえわかっていれば、誰でも使用でき、クエリを変更して利用することが可能です。確実に一部のデータを制限して公開したい場合には、Private API を使用して、自身のサーバーを経由して検索インターフェースを提供するようにしましょう。