ภาพรวม

Freebase Search API ให้สิทธิ์เข้าถึงข้อมูล Freebase เมื่อมีการค้นหาข้อความอิสระ ผลลัพธ์ของคำค้นหาจะได้รับการจัดเรียงและมีคะแนนความเกี่ยวข้องที่เป็นตัวเลข
นักพัฒนาแอปสามารถใช้ตัวกรองเพื่อจำกัดผลการค้นหาให้เป็นข้อมูลบางประเภทได้ ดูข้อมูลเพิ่มเติมเกี่ยวกับวิธีสร้างคำค้นหาแบบละเอียดได้ที่ตำราการค้นหา
ตัวอย่างวิธีที่นักพัฒนาแอปอาจต้องการใช้ Search API มีดังนี้
- การแนะนำเอนทิตีโดยอัตโนมัติ (เช่น วิดเจ็ตคำแนะนำของ Freebase)
- รับรายการเอนทิตีที่โดดเด่นที่สุดที่จัดอันดับแล้วซึ่งมีชื่อที่ระบุ
- การค้นหาเอนทิตีโดยใช้ Search Metaschema
ตัวอย่างโค้ดต่อไปนี้ในภาษาที่รองรับหลายภาษาแสดงวิธีค้นหาศิลปินเพลงที่ตรงกับข้อความ "Cee Lo Green" ข้อจำกัดเพิ่มเติมคือศิลปินต้องสร้างผลงานที่ชื่อว่า "The Lady Killer"
Python
import json import urllib api_key = open(".api_key").read() query = 'blue bottle' service_url = 'https://www.googleapis.com/freebase/v1/search' params = { 'query': query, 'key': api_key } url = service_url + '?' + urllib.urlencode(params) response = json.loads(urllib.urlopen(url).read()) for result in response['result']: print(result['name'] + ' (' + str(result['score']) + ')')
Ruby
require 'rubygems' require 'cgi' require 'httparty' require 'json' require 'addressable/uri' API_KEY = open(".freebase_api_key").read() url = Addressable::URI.parse('https://www.googleapis.com/freebase/v1/search') url.query_values = { 'query' => 'Blue Bottle', 'key'=> API_KEY } response = HTTParty.get(url, :format => :json) response['result'].each { |topic| puts topic['name'] }
Java
package com.freebase.samples; import com.google.api.client.http.GenericUrl; import com.google.api.client.http.HttpRequest; import com.google.api.client.http.HttpRequestFactory; import com.google.api.client.http.HttpResponse; import com.google.api.client.http.HttpTransport; import com.google.api.client.http.javanet.NetHttpTransport; import com.jayway.jsonpath.JsonPath; import java.io.FileInputStream; import java.util.Properties; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; public class SearchExample { public static Properties properties = new Properties(); public static void main(String[] args) { try { properties.load(new FileInputStream("freebase.properties")); HttpTransport httpTransport = new NetHttpTransport(); HttpRequestFactory requestFactory = httpTransport.createRequestFactory(); JSONParser parser = new JSONParser(); GenericUrl url = new GenericUrl("https://www.googleapis.com/freebase/v1/search"); url.put("query", "Cee Lo Green"); url.put("filter", "(all type:/music/artist created:\"The Lady Killer\")"); url.put("limit", "10"); url.put("indent", "true"); url.put("key", properties.get("API_KEY")); HttpRequest request = requestFactory.buildGetRequest(url); HttpResponse httpResponse = request.execute(); JSONObject response = (JSONObject)parser.parse(httpResponse.parseAsString()); JSONArray results = (JSONArray)response.get("result"); for (Object result : results) { System.out.println(JsonPath.read(result,"$.name").toString()); } } catch (Exception ex) { ex.printStackTrace(); } } }
JavaScript
<!DOCTYPE html> <html> <head> <script src="https://www.gstatic.com/external_hosted/jquery2.mi><n.js&qu>o<t;/sc>r<ipt<>/span> </head bodyaside class><=">;warning&<quot;st>rongWarning: /strongThe Freebase API will be retired< on Ju>n<e 30,<>/span> 2015./aside script var service_url = 'https://www.googleapis.com/freebase/v1/search'; var params = { 'query': 'Cee Lo Green', 'filter': '(all type:/music/artist created:"The Lady Killer")', 'limit': 10, 'indent': true }; $.getJSON(service_url + '?callback=?',< pa>rams).done(function(response) { $.each(response.result, funct<ion(i, >r<esult>)< { > $('div', {text:result['name']}).appendTo(document.body); }); }); /script /body /html
PHP
<!DOCTYPE html> <html> <body> <?php include('.freebase-api-key'); $service_url = 'https://www.googleapis.com/freebase/v1/search'; $params = arra>y( 'query' = >39;Blue Bottle', 'key' = $freebase_api_key ); $url = $service_url . '?' . http_build_query($params); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = json_decode(curl_exec($ch), true); curl_close($ch); foreach($response['<;re>sult'>;<] as >$<resul>t) { echo $result['name'] . 'br/'; } ? /body /html
นอกจากนี้ โปรดดูหน้าไลบรารีของไคลเอ็นต์เพื่อดูว่าระบบรองรับภาษาที่คุณชื่นชอบหรือไม่
เอกสารประกอบของ Search API
ดูเอกสารประกอบต่อไปนี้ด้วย
- ดูรายละเอียดวิธีใช้ API ได้ที่เอกสารอ้างอิงของ Search
- ดูข้อมูลเพิ่มเติมเกี่ยวกับวิธีสร้างคำค้นหาแบบละเอียดได้ที่ตำราการค้นหา
- ดูข้อมูลเกี่ยวกับวิธีอธิบายความสัมพันธ์ระหว่างเอนทิตีและพร็อพเพอร์ตี้ได้ในเอกสาร Metaschema ของ Search
ข้อควรพิจารณาด้านความปลอดภัย
Search API จะจัดทำดัชนีและค้นหาเนื้อหาที่ผู้ใช้สร้างขึ้นซึ่งจัดเก็บไว้ในกราฟ Freebase ซึ่งหมายความว่าคุณไม่สามารถใช้เนื้อหาในหน้าเว็บโดยตรงได้โดยไม่ต้องหลีกเลี่ยงอย่างปลอดภัยก่อน
ดูข้อมูลเพิ่มเติมได้ที่เริ่มต้นใช้งาน: ความปลอดภัย
การกรองขั้นสูง
Search API รองรับข้อจํากัดของตัวกรองจํานวนมากเพื่อให้การค้นหามุ่งเป้าไปที่เอนทิตีที่ถูกต้องได้ดียิ่งขึ้น
ตัวอย่างเช่น การใช้ข้อจำกัดตัวกรอง "ประเภท" จะช่วยให้เราแสดงรายชื่อบุคคลที่โดดเด่นที่สุดใน Freebase ได้
filter=(any type:/people/person)
ข้อจำกัดของตัวกรองยอมรับอินพุตที่หลากหลาย ดังนี้
- รหัสที่มนุษย์อ่านได้สำหรับเอนทิตีสคีมาหรือผู้ใช้ เช่น
/people/person
สำหรับข้อจำกัดประเภท/film
สำหรับข้อจำกัดของโดเมน
- เช่น MIDs ของ Freebase
/m/01g317
สำหรับข้อจำกัดประเภท/people/person
เดียวกัน/m/010s
สำหรับข้อจำกัดของโดเมน/film
ข้างต้น
- ชื่อเอนทิตี เช่น
"person"
สำหรับข้อจำกัดประเภท/people/person
ที่มีความแม่นยำน้อยกว่า"film"
สำหรับ/film
ข้อจำกัดของโดเมนที่แม่นยำน้อยกว่า
ข้อจํากัดของตัวกรองสามารถแบ่งออกเป็น 2-3 หมวดหมู่ ดูรายละเอียดเพิ่มเติมได้ที่ตำราการค้นหา
คุณสามารถรวมและทำซ้ำข้อจำกัดของตัวกรองได้อย่างอิสระใน SearchRequest
โดยตรง ระบบจะรวมพารามิเตอร์ข้อจํากัดของตัวกรองที่ซ้ำกันไว้ในคําค้นหา OR ระบบจะรวมพารามิเตอร์หรือกลุ่มข้อจำกัดของตัวกรองต่างๆ ไว้ในคำค้นหา AND
เช่น
หากต้องการค้นหา "ผู้คนหรือเมืองที่ชื่อ Gore" ให้ลองใช้คำค้นหาต่อไปนี้
query=gore &filter=(any type:/people/person type:/location/citytown)
คุณสามารถลบล้างลักษณะการทำงานของการรวมนี้และควบคุมได้ดียิ่งขึ้นด้วยพารามิเตอร์ตัวกรอง ซึ่งมีอินเทอร์เฟซที่สมบูรณ์ยิ่งขึ้นสำหรับข้อจำกัดในการรวม ซึ่งเป็นนิพจน์ S ที่อาจซ้อนกันโดยพลการ โดยโอเปอเรเตอร์จะเป็นอย่างใดอย่างหนึ่งต่อไปนี้
any
ซึ่งเป็นตรรกะ ORall
ซึ่งเป็น AND โดยตรรกะnot
should
ซึ่งใช้ได้เฉพาะที่ระดับบนสุดและระบุว่าข้อจำกัดเป็นแบบไม่บังคับ ในระหว่างการให้คะแนน แมตช์ที่ไม่ตรงกับข้อจำกัดที่ไม่บังคับจะมีคะแนนหารครึ่งสำหรับข้อจำกัดที่ไม่บังคับแต่ละรายการที่ไม่ตรงกัน
เช่น
หากต้องการจับคู่กับประเภท /people/person
หรือโดเมน /film
ให้ลองทำดังนี้
query=gore &filter=(any type:/people/person domain:/film)