packagecom.freebase.samples;importcom.google.api.client.http.GenericUrl;importcom.google.api.client.http.HttpRequest;importcom.google.api.client.http.HttpRequestFactory;importcom.google.api.client.http.HttpResponse;importcom.google.api.client.http.HttpTransport;importcom.google.api.client.http.javanet.NetHttpTransport;importcom.jayway.jsonpath.JsonPath;importjava.io.FileInputStream;importjava.util.Properties;importorg.json.simple.JSONArray;importorg.json.simple.JSONObject;importorg.json.simple.parser.JSONParser;publicclassSearchExample{publicstaticPropertiesproperties=newProperties();publicstaticvoidmain(String[]args){try{properties.load(newFileInputStream("freebase.properties"));HttpTransporthttpTransport=newNetHttpTransport();HttpRequestFactoryrequestFactory=httpTransport.createRequestFactory();JSONParserparser=newJSONParser();GenericUrlurl=newGenericUrl("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"));HttpRequestrequest=requestFactory.buildGetRequest(url);HttpResponsehttpResponse=request.execute();JSONObjectresponse=(JSONObject)parser.parse(httpResponse.parseAsString());JSONArrayresults=(JSONArray)response.get("result");for(Objectresult:results){System.out.println(JsonPath.read(result,"$.name").toString());}}catch(Exceptionex){ex.printStackTrace();}}}
JavaScript
<!DOCTYPEhtml>
<html>
<head>
<scriptsrc="https://www.gstatic.com/external_hosted/jquery2.min.js"></script>
</head>
<body><asideclass="warning"><strong>Warning:</strong>The Freebase API will be retired on June 30,2015.</aside>
<script>
varservice_url='https://www.googleapis.com/freebase/v1/search';varparams={'query':'Cee Lo Green','filter':'(all type:/music/artist created:"The Lady Killer")','limit':10,'indent':true};$.getJSON(service_url+'?callback=?',params).done(function(response){$.each(response.result,function(i,result){$('<div>',{text:result['name']}).appendTo(document.body);});});
</script>
</body>
</html>
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003eThe Freebase API is retired, but this documentation remains for the data dumps.\u003c/p\u003e\n"],["\u003cp\u003eThe Freebase Search API allows you to query Freebase data using free text and apply filters to refine results.\u003c/p\u003e\n"],["\u003cp\u003eCode samples are provided in Python, Ruby, Java, Javascript, and PHP to demonstrate how to use the Search API.\u003c/p\u003e\n"],["\u003cp\u003eThe documentation includes security considerations and advanced filtering options.\u003c/p\u003e\n"],["\u003cp\u003eFilter constraints can be combined using operators like \u003ccode\u003eany\u003c/code\u003e, \u003ccode\u003eall\u003c/code\u003e, \u003ccode\u003enot\u003c/code\u003e, and \u003ccode\u003eshould\u003c/code\u003e for more complex queries.\u003c/p\u003e\n"]]],[],null,["# Search Overview\n\n| **Warning:** The Freebase API is no longer available. This documentation is left online as supporting information for the [data dumps](/freebase).\n\n1. [Overview](#overview)\n - [Search API documentation](#search-api-documentation)\n2. [Security considerations](#security-considerations)\n3. [Advanced filtering](#advanced-filtering)\n\nOverview\n--------\n\nThe 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.\n\nDevelopers can apply filters to constrain the search results to certain types of data. See the [Search Cookbook](/freebase/v1/search-cookbook) for more information on how to construct detailed search queries.\n\nSome examples of how developers may want to use the Search API include:\n\n- Autosuggesting entities (e.g. [Freebase Suggest Widget](/freebase/v1/suggest))\n- Getting a ranked list of the most notable entities with a given name.\n- Finding entities using [Search Metaschema](/freebase/v1/search-metaschema).\n\nThe 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\". \n\n### Python\n\n```python\nimport json\nimport urllib\n\napi_key = open(\".api_key\").read()\nquery = 'blue bottle'\nservice_url = 'https://www.googleapis.com/freebase/v1/search'\nparams = {\n 'query': query,\n 'key': api_key\n}\nurl = service_url + '?' + urllib.urlencode(params)\nresponse = json.loads(urllib.urlopen(url).read())\nfor result in response['result']:\n print(result['name'] + ' (' + str(result['score']) + ')')\n```\n\n### Ruby\n\n```ruby\nrequire 'rubygems'\nrequire 'cgi'\nrequire 'httparty'\nrequire 'json'\nrequire 'addressable/uri'\n\nAPI_KEY = open(\".freebase_api_key\").read()\nurl = Addressable::URI.parse('https://www.googleapis.com/freebase/v1/search')\nurl.query_values = {\n\t'query' =\u003e 'Blue Bottle',\n\t'key'=\u003e API_KEY\n}\nresponse = HTTParty.get(url, :format =\u003e :json)\nresponse['result'].each { |topic|\n puts topic['name']\n} \n```\nThis example uses the [Httparty](http://httparty.rubyforge.org/) and [Addressable](http://addressable.rubyforge.org/) libraries.\n\n### Java\n\n```java\npackage com.freebase.samples;\n\nimport com.google.api.client.http.GenericUrl;\nimport com.google.api.client.http.HttpRequest;\nimport com.google.api.client.http.HttpRequestFactory;\nimport com.google.api.client.http.HttpResponse;\nimport com.google.api.client.http.HttpTransport;\nimport com.google.api.client.http.javanet.NetHttpTransport;\nimport com.jayway.jsonpath.JsonPath;\nimport java.io.FileInputStream;\nimport java.util.Properties;\nimport org.json.simple.JSONArray;\nimport org.json.simple.JSONObject;\nimport org.json.simple.parser.JSONParser;\n\npublic class SearchExample {\n public static Properties properties = new Properties();\n public static void main(String[] args) {\n try {\n properties.load(new FileInputStream(\"freebase.properties\"));\n HttpTransport httpTransport = new NetHttpTransport();\n HttpRequestFactory requestFactory = httpTransport.createRequestFactory();\n JSONParser parser = new JSONParser();\n GenericUrl url = new GenericUrl(\"https://www.googleapis.com/freebase/v1/search\");\n url.put(\"query\", \"Cee Lo Green\");\n url.put(\"filter\", \"(all type:/music/artist created:\\\"The Lady Killer\\\")\");\n url.put(\"limit\", \"10\");\n url.put(\"indent\", \"true\");\n url.put(\"key\", properties.get(\"API_KEY\"));\n HttpRequest request = requestFactory.buildGetRequest(url);\n HttpResponse httpResponse = request.execute();\n JSONObject response = (JSONObject)parser.parse(httpResponse.parseAsString());\n JSONArray results = (JSONArray)response.get(\"result\");\n for (Object result : results) {\n System.out.println(JsonPath.read(result,\"$.name\").toString());\n }\n } catch (Exception ex) {\n ex.printStackTrace();\n }\n }\n}\n```\n\n### Javascript\n\n```javascript\n\u003c!DOCTYPE html\u003e\n\u003chtml\u003e\n\u003chead\u003e\n \u003cscript src=\"https://www.gstatic.com/external_hosted/jquery2.min.js\"\u003e\u003c/script\u003e\n\u003c/head\u003e\n\u003cbody\u003e\u003caside class=\"warning\"\u003e\u003cstrong\u003eWarning: \u003c/strong\u003eThe Freebase API will be retired on June 30,\n 2015.\u003c/aside\u003e\n\u003cscript\u003e\n var service_url = 'https://www.googleapis.com/freebase/v1/search';\n var params = {\n 'query': 'Cee Lo Green',\n 'filter': '(all type:/music/artist created:\"The Lady Killer\")',\n 'limit': 10,\n 'indent': true\n };\n $.getJSON(service_url + '?callback=?', params).done(function(response) {\n $.each(response.result, function(i, result) {\n $('\u003cdiv\u003e', {text:result['name']}).appendTo(document.body);\n });\n });\n\u003c/script\u003e\n\u003c/body\u003e\n\u003c/html\u003e\n```\nThis example uses the [jQuery]() library.\n\n### PHP\n\n```php\n\u003c!DOCTYPE html\u003e\n\u003chtml\u003e\n\u003cbody\u003e\n\u003c?php\n include('.freebase-api-key');\n $service_url = 'https://www.googleapis.com/freebase/v1/search';\n $params = array(\n 'query' =\u003e 'Blue Bottle',\n 'key' =\u003e $freebase_api_key\n );\n $url = $service_url . '?' . http_build_query($params);\n $ch = curl_init();\n curl_setopt($ch, CURLOPT_URL, $url);\n curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\n $response = json_decode(curl_exec($ch), true);\n curl_close($ch);\n foreach($response['result'] as $result) {\n echo $result['name'] . '\u003cbr/\u003e';\n }\n?\u003e\n\u003c/body\u003e\n\u003c/html\u003e\n```\n\nAlso see the [Client Libraries](/freebase/v1/libraries) page to see if your favorite language is supported.\n\n### Search API documentation\n\nAlso see the following documentation:\n\n- See the [Search reference documents](/freebase/v1/search) for details on how to use the API.\n- See the [Search Cookbook](/freebase/v1/search-cookbook) for more information on how to construct detailed search queries.\n- See the [Search Metaschema document](/freebase/v1/search-metaschema) for information on how the relationships between entities and properties are described.\n\nSecurity considerations\n-----------------------\n\nThe Search API indexes and searches user generated content stored in the Freebase graph. This means that you cannot directly use the content on a web page without safely escaping it first.\n\nSee [Getting Started: Security](/freebase/v1/getting-started#security) for more information.\n\nAdvanced filtering\n------------------\n\nThe Search API supports a large number of filter constraints to better aim the search at the correct entities.\n\nFor example, using a \"type\" filter constraint, we can show a list of the most notable people in Freebase.\n\n```\nfilter=(any type:/people/person)\n```\n\nTry it\n\nFilter constraints accept a variety of inputs:\n\n- Human readable IDs for schema entities or users, for example:\n - `/people/person` for a type constraint\n - `/film` for a domain constraint\n- Freebase MIDs, for example:\n - `/m/01g317` for the same `/people/person` type constraint\n - `/m/010s` for the above `/film` domain constraint\n- Entity names, for example:\n - `\"person\"` for a less precise `/people/person` type constraint\n - `\"film\"` for a less precise `/film` domain constraint\n\nFilter constraints can be classified into a few categories. See the [Search Cookbook](/freebase/v1/search-cookbook) for more details.\n\nFilter constraints can be freely combined and repeated in the `SearchRequest` directly. Repeated filter constraint parameters are combined into an OR query. Different filter constraint parameters or groups are combined into an AND query.\n\n**For example:**\n\nTo search for \"people or cities named *Gore*\", try:\n\n```\nquery=gore\n&filter=(any type:/people/person type:/location/citytown)\n```\n\nTry it\n\nThis combining behavior can be overriden and better controlled with the filter parameter which offers a richer interface to combining constraints. It is an s-expression, possibly arbitrarily nested, where the operator is one of:\n\n- `any`, logically an OR\n- `all`, logically an AND\n- `not`\n- `should`, which can only be used at the top level and which denotes that the constraint is optional. During scoring, matches that don't match optional constraints have their score divided in half for each optional constraint they don't match.\n\n**For example:**\n\nTo match on the `/people/person` type or the `/film` domain, try:\n\n```\nquery=gore\n&filter=(any type:/people/person domain:/film)\n```\n\nTry it"]]