Page Summary
-
The Freebase Search API allows access to Freebase data through free text queries, providing ordered results with a numerical relevancy score.
-
Developers can apply filters to refine search results to specific data types and combine multiple filter constraints using "any", "all", "not", or "should" operators.
-
The documentation includes information on the Search API, security considerations, and advanced filtering techniques, with examples provided in various programming languages.
Overview
The Freebase Search API provides access to Freebase data given a free text query. The results of the query are ordered and have a numerical relevancy score.
Developers can apply filters to constrain the search results to certain types of data. See the Search Cookbook for more information on how to construct detailed search queries.
Some examples of how developers may want to use the Search API include:
- Autosuggesting entities (e.g. Freebase Suggest Widget)
- Getting a ranked list of the most notable entities with a given name.
- Finding entities using Search Metaschema.
The following code samples in several supported languages show how to perform a search for a musical artist that matches the text “Cee Lo Green”. An additional constraint is that they created something called “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.min.js"></script> </head> <body>
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-08-08 UTC.