Freebase Suggest

From Freebase

Jump to: navigation, search

Contents

Overview

Freebase Suggest is a jQuery plugin that adds Freebase topic autocomplete to search boxes on your site. Start typing text and the widget suggests relevant matches from the millions of topics on Freebase.com or any subset of types like People, Locations or Animals. Topic flyouts help the user select the correct item which is uniquely identified with a Freebase id..

Freebase Suggest.png

Features

  • Cross browser - based on jQuery, tested on IE7+, FF2+, Safari 3+ and Chrome (min jquery version is 1.4.4)
  • Cross-domain. No proxy servers required thanks to JSONP.
  • Hosted by Google on gstatic.com
  • Free! (The standard Freebase ToS apply.)


Try it out!

Add to your site

It's easy to add Freebase Suggest to your web page. Just include this html in your document head:

<link type="text/css" rel="stylesheet" href="https://www.gstatic.com/freebase/suggest/3_1/suggest.min.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script>
<script type="text/javascript" src="https://www.gstatic.com/freebase/suggest/3_1/suggest.min.js"></script>
<script type="text/javascript">
$(function() {
  $("#myinput").suggest({filter:'(all type:/film/director)'});
});
</script>

Assuming this text input in the document body:

<input type="text" id="myinput"/>

You need to obtain an api key in order to make Suggest requests. The widget will work for very limited testing purposes without a key. To obtain a key, follow the instructions here: http://wiki.freebase.com/wiki/API#API_Keys

Once you obtain a key, you can pass it to suggest like this:
$(function() {
  $("#myinput").suggest({ "key" : "<your api key>"});
});

Why do I want Freebase Suggest for my data application?

  • With autocomplete, your users type less to enter more data
  • Data entry is fun and accurate!
  • Reduce the cognitive load on your users thanks to the images and descriptions in the topic flyouts
  • Use strong identifiers rather than text keywords. The name "Sting" is ambiguous, but the Freebase IDs /en/sting and /en/sting_1959 are not.
  • Avoid duplicate names for the same entity. Puff Daddy, P. Diddy, Sean Combs all refer to /en/sean_combs

Questions?

Questions, bug reports and feedback very welcome on the Freebase Developers Mailing List or the Issues List (choose Search/Suggest component).


Configuration options

Name Type Default Description
advanced Boolean true If TRUE, suggest will parse and handle inline name:value pairs in the input as additional filter constraints. For example, for 'bob type:artist contributed_to:"Love and Theft"', an additional filter constraint, '(all type:artist contributed_to:"Love and Theft")' will passed to search. Additionally when advanced is TRUE, suggest will recognize freebase ids/mids so that one can directly search for an entity by it's id or mid.
exact Boolean false If TRUE, you can specify the search service to return only exact matches (of what's in the input). This value is transparently passed to the search service.
filter String null You can specify default search filter constraint(s) that will be transparently passed to the search service as a filter parameter(s). For example, to constrain suggest to only search on colleges and/or universities, you could use "(all type:/education/university)" as a filter. See Search API or Textual Constraints for a comprehensive list of available search filters.
key String null You must provide an api key for the default search service specified by service_url + service_path. You can get one from the Google Developer Console
lang String null The lang parameter accepts a comma-separated list of language codes that cause the search to be done in all the languages specified and the results to be ranked in the first language listed and displayed in the first language of the list that has a name for the entity. Currently, 18 languages are supported: en (english), es (spanish), fr (french), de (german), it (italian), pt (portuguese), zh (chinese), ja (japanese), ko (korean), ru (russian), sv (swedish), fi (finnish), da (danish), nl (dutch), el (greek), ro (romanian), tr (turkish), hu (hungarian). English has by far the most coverage and is the default language. This value is transparently passed to the search service.
scoring String null The scoring parameter makes it possible to control what relevance score components are used to compute the final score. This value is transparently passed to the search service.
  • freebase: Use only the Freebase relevance score.
  • entity: Use both FREEBASE and Google relevance scores, defaulting missing Google scores to 1.0. This is the default.
  • schema: Use when looking for schema entities like types, properties or domains. The link counts of schema entities is computed differently.
spell String "always" Valid values are "always", "no_results", "no_spelling". If spelling is requested and the search returns a spelling correction, suggest will display the correction in the suggest list. This value is transparently passed to the search service.
flyout Boolean, "bottom" true Whether or not to show a flyout description on hover. If "bottom", show flyout at the bottom of the suggest list. If the suggest list is displayed above the input box, the flyout will be displayed on top of the list. If TRUE, suggest will do it's best to show the flyout either on the left or right side of the list.
suggest_new String null This can be any text to be shown below the suggestion list. On selection, an 'fb-select-new' will be triggered.
css Object You can overwrite default css class names used for the various suggest elements. See CSS classes for details.
css_prefix String null You can specify a prefix to be pre-pended to the class names of the the suggest elements. For example if css_prefix is "foo-" the container names will be "foo-fbs-pane" and "foo-fbs-flyoutpane".
soft Boolean false Carry over from previous freebaseSuggest to enable search box behavior without forcing a selection from the suggest list. If true, DO NOT auto-select first item on ENTER key. Otherwise, select first item.
service_url String https://www.googleapis.com This is the base url for the suggest service.
service_path String /freebase/v1/search service_url + service_path = url to the suggest service.
flyout_service_url String http://dev.freebase.com The base url for the flyout service. If null, defaults to service_url.
flyout_service_path String /flyout?id=${id} flyout_service_url + flyout_service_path = url to flyout service.
flyout_parent String (jQuery selector) null By default, the flyout container is appended to the document body and positioned absolutely. You can specify a different parent and the flyout will not be absolutely positioned.
align String null The suggest list is aligned to the "left" or "right" of the input box depending on it's position in the document. To override this behavior you can set align to "left" or "right" of the input box.
status Array[4](String) ["Start typing to get suggestions...", "Searching...", "Select an item from the list:", "Sorry, something went wrong. Please try again later"] Status messages that appear at the 4 different stages of suggest; [0] When input box is empty and gets focus. [1] when retrieving results, [2] when displaying the results, [3] when there is an error from the suggest service. You can override these default status messages by passing in a different Array of string corresponding to the 4 different stages.
parent String (jQuery selector) null By default, the suggest list is appended to the document body and positioned absolutely. You can specify a different parent and the list will not be absolutely positioned.
animate Boolean false If true, the showing of the suggest list will be animated using jQuery slideDown effect.
xhr_delay Integer (ms) 200 This is mostly useful in the case where mql_filters are complex and suggest service might take more or similar time to answer than the hardcoded delay, causing a somewhat degraded user experience and unnecessary load.
zIndex Integer null You can specifically set the z-index of the outer most containers (fbs-pane, fbs-flyoutpane). This is useful when using suggest in dialog boxes so that the suggest elements display on top.

CSS classes

The default CSS classes used by the suggest widget can be overidden by passing in a map of new the class names using the css configuration option.

pane The outer container of the suggest list.

Default: 'fbs-pane'

list The suggest list.

Default: 'fbs-list'

item The suggest list items.

Default: 'fbs-item'.

item_name The element containing the the name of the item.

Default: 'fbs-item-name'

selected The current highlighted/selected item. Default: 'fbs-selected'.
status The element containing the status messages. Default: 'fbs-status'.
item_type The element containing the notable type of an item. Default: 'fbs-item-type'.
flyoutpane The flyout outer container.

Default: 'fbs-flyoutpane'

For example:

$("#myinput").suggest({
  "css": {
    "pane": "custom-pane-class",
    "list": "custom-list-class"
  }
});

Events

Freebase Suggest triggers the following events within the context of the input that it is initialized with.

fb-select - When an item is selected from the suggest list. The event is accompanied by a data object where data.name and data.id represent the name and id of the item that was selected.

$("#myinput").suggest().bind("fb-select", function(e, data) { ... });

fb-select-new - When the suggest_new option is enabled, this event is triggered when the suggest_new item is selected. The event is accompanied by the input value.

$("#myinput").suggest({'suggest_new': 'This is the suggest new text'}).bind("fb-select-new", function(e, val) { ... });

Examples

Basic usage

$("#example1")
 .suggest()
 .bind("fb-select", function(e, data) {
   alert(data.name + ", " + data.id);
});

Try it out!

Suggest new

$("#example2")
 .suggest({
   "suggest_new": "Click on me if you don't see anything in the list"
 })
 .bind("fb-select", function(e, data) {
   alert(data.name + ", " + data.id);
 })
 .bind("fb-select-new", function(e, val) {
   alert("Suggest new: " + val);
 });

Try it out!

Constraining suggestions using a filter

Suggest films directed by Steven Spielberg.

$("#example5")
.suggest({
   "filter": "(all type:/film/film contributor:\"Steven Spielberg #directed_by\")"
})
.bind("fb-select", function(e, data) {
   alert(data.name + ", " + data.id);
});

Try it out!


Filtering from the textbox

It is possible to filter the results by typing any of the filter constraints directly into the suggest box, e.g. to search for books on gardening:

gardening type:/book/book
gardening type:book


Freebase Suggest in the wild

Sites which are known to use Freebase Suggest:


FAQs

I've just created a new topic, but it isn't showing up in Suggest - what's going on?

Freebase Suggest uses the search API to provide its results, and that is updated in near-real-time. New topics usually appear within a minute or so, but this can be longer if the system is under high load.

Personal tools