Examples

These examples use PLOS’ Search API to search through PLOS’ archive of scientific articles. The main work is done in PLoSSearch.js, which defines the object that enables searching. That script relies on PLoSLib.js for some basic functions like event listening, toggling hidden content, and managing jsonp callback functions.

For each example, we present the call to the function in PLoSSearch.js that created them. For each widget created, we also call widget.activate() on DOM load (see the code included at the head of the web page by right-clicking and selecting view source). The meat of PLoSSearch.js is in the request to PLOS’ Search server. The following code formats the query:

var query = document.getElementById(args.input).value;
if (!query || query === '') {
	query = "*:*"
}
var theQuery = 'q=' + (args.field ? args.field + ':"' + query + '"' : query)
var params = [
  'json.wrf=PLoS._jsonpCallbacks[' + that.id + ']', //name of our callback function
  'wt=json', //return type
  'start=0',
  'rows='+that.numResults, //number of results to return
  'fl=id,title,counter_total_month,author,abstract,journal,article_type,publication_date',
        //fields we want from solr
  'fq=!article_type_facet:"Issue Image" AND doc_type:full', //only return full articles
  'sort=counter_total_month desc', //sort by articles with the most views
  'api_key=plos_api_test' // PLOS API key
];
params = params.concat(theQuery);
var strData = params.join('&');

Then, since we need to make a cross-domain request to PLOS’ SOLR Server, we have to create a script tag and insert it into the DOM:

var script = document.createElement('script');
script.type = "text/javascript";
if(query) {
  script.src = searchUrl +'?'+strData; // setting the search url and query string
  document.getElementById(args.resultsDiv).appendChild(script); // inserting the results
	...
}

Python Examples

There are also some examples in python here. The python examples also include functions to use dates in searches, namely plos_search.graphPubs() and plos_search.pubsOn(). The former outputs the number of PLOS publications on each day in a given range to a CSV file, and the latter lists the articles published on a given day.

Search Limit

By default, the PLOS Search API will return 999 rows of data.  Search queries should be written so that they don’t request larger result sets from our database. If you want to retrieve more rows, you’ll need to page and stream the results.  You can use the parameters start and rows to return the next 999 rows of data.  For example:

&start=1&rows=2

Have Questions?

If you have any questions, please join the PLOS API developers group and post your questions to the group.