API:Search and discovery

MediaWiki, its extensions, and its sibling projects hold tremendous potential for knowledge discovery through search. The Discovery department maintains the mechanisms, tools, and services for doing so.

Users can find information in MediaWiki by looking it up directly, and in Wikidata by reading  Help:Navigating Wikidata.

MediaWiki
The MediaWiki API has several search-related modules. You can make requests and view generated help at any wiki's /w/api.php entry point, or fill in API request parameters at Special:ApiSandbox.

Search modules

 * action=opensearch
 * See API:Opensearch. Returns search results in OpenSearch format, each with text extract  on Wikimedia projects.   View generated API help


 * action=languagesearch
 * Search for language names in any script. View generated API help

Query list submodules
These Query submodules return a list of wiki pages matching the search criteria, and some return additional information about each page. Furthermore, you can use each as a generator to provide many other  Properties of the set of returned pages, such as a lead image, snippet, and/or page description.


 * action=query list=prefixsearch
 * Retrieves wiki page titles with the given prefix. See the showcase article  Page info in search results. See module documentation for API:Prefixsearch and  View generated API help.


 * action=query list=search
 * Uses the wiki search engine to find matching pages. On Wikimedia wikis it provides search results from CirrusSearch, returning typical search result information such as text snippets and page size.  See module documentation for API:Search and  View generated API help


 * action=query list=geosearch
 * If the GeoData extension is installed on the wiki, then this returns wiki pages near a location, with their geographical information. See the showcase article  Showing nearby wiki information, module documentation for  geosearch, and  View generated API help.

Command line
Using the command line, we can query the API using `curl` and parse results using `jq`.

For example, let's try looking up item Q39246 on Wikidata:

We find that Q39246 is the Wikidata identifier for Richard Feynman, and that there are 48 claims made about him.

JavaScript
To write a MediaWiki API client in JavaScript, all that's needed is a JSONP handler. Many libraries (e.g. jQuery) include JSONP clients, or one can be written independently.

Within the MediaWiki ecosystem, jQuery can be used directly:

'This uses jQuery's  which is available in many interactive JavaScript coding environments and makes sense if your eventual goal is a separate standalone project.

If your eventual goal is code running on a wiki, e.g. as a Gadget, then you should use the higher-level  function provided by the  'mediawiki.api' ResourceLoader module.

In other environments, a simple JSONP handler can be written:

CirrusSearch
CirrusSearch is a MediaWiki extension to enable Elastic-based search of MediaWiki content. It acts as a search back-end, so  is the main interface to this.

You can use the same Cirrus features in API queries that users can enter in the search box. For example, you can use the  special prefix to find related pages.

Additional CirrusSearch API modules
In addition, CirrusSearch can report its configuration and internal information. These APIs are probably only useful if you're familiar with Elasticsearch and want to see how CirrusSearch uses it.
 * page parameter
 * This is an action parameter to index.php, for example https://en.wikipedia.org/wiki/Special:Search/cat%20dog%20chicken?cirrusDumpQuery


 * parameter to Special:Search queries
 * For example, https://en.wikipedia.org/wiki/2014?action=cirrusdump


 * API modules cirrus-config-dump, cirrus-settings-dump, cirrus-mapping-dump
 * These dump the CirrusSearch setup.

Wikidata
Wikidata's API includes a few actions (wbgetentities, wbgetclaims, wbsearchentities) that can be used to search for information about entities, properties, statements, and claims.

Wikidata query service
Wikidata query service performs graph-based searching of Wikidata via a SPARQL API. It's available at https://query.wikidata.org/

WDQS Explorer (demo) (source code) provides in-browser graph exploration using SPARQL queries against the Wikidata query service.

Wikipedia
Browse to https://en.wikipedia.org/wiki/Main_Page, open up the JavaScript console, and run the following:

This logs the string  to the JavaScript console.

If the MediaWiki libraries and environment are unavailable, this can be done using the  function above:

Wikidata
Using JSONP, we can perform the above steps right from the Web browser's JavaScript console. On Wikipedia, the Wikidata item identifier is available via the MediaWiki configuration value.

Browse to https://en.wikipedia.org/wiki/Richard_Feynman, open up the JavaScript console, and run the following:

This logs the string  and the  Wikidata entry description string "American quantum physicist" to the JavaScript console.

If the MediaWiki libraries and environment are unavailable, this can be done using the  function above:

Wiktionary
Browse to https://en.wikipedia.org/wiki/Main_Page, open up the JavaScript console, and run the following:

This logs the string  to the JavaScript console.

If the MediaWiki libraries and environment are unavailable, this can be done using the  function above:

External examples
These approaches can be used to build external Web applications that can search MediaWiki instances via the API.

Wikipedia
Wikipedia Query (demo) (source code) provides in-browser full-text searching using the  search.

Wikidata
Wikidata Explorer (demo) (source code) provides in-browser graph exploration using the  action.

Wiktionary
Wiktionary Query (demo) (source code) provides in-browser word searching using the  property.