Skip to content

Latest commit

 

History

History
206 lines (168 loc) · 6.79 KB

search_entities.md

File metadata and controls

206 lines (168 loc) · 6.79 KB

Search entities

Summary

searchEntities

associated Wikibase API doc: wbsearchentities

const url = wbk.searchEntities('Ingmar Bergman')

this returns a query url that you are then free to request with the tool you like

https://www.wikidata.org/w/api.php?action=wbsearchentities&search=Ingmar%20Bergman&language=en&limit=20&format=json

This search endpoint, wbsearchentities, is the one used by the search bar and autocomplete fields on Wikibase GUI.

With more parameters:

const search = 'Ingmar Bergman'
const language = 'fr' // will default to 'en'
const limit = 10 // defaults to 20
const format = 'json' // defaults to json

const url = wbk.searchEntities(search, language, limit, format)

which can also be passed as an object:

const url = wbk.searchEntities({
  search: 'Ingmar Bergman',
  format: 'xml',
  language: 'sv',
  limit: 30,
  continue: 10
})

By default, the uselang parameter (the language in which the search results are returned) is set to the same as the language passed, but if for some weird use case you need to set a different language, you can still pass a 2 letters language code:

  • as last argument (inline interface)
const uselang = 'eo'
const url = wbk.searchEntities(search, language, limit, format, uselang)
  • or set uselang in the option object (object interface).
const url = wbk.searchEntities({
  search: 'Ingmar Bergman',
  language: 'sv',
  uselang: 'eo'
})

If the values aren't available in the desired language, it will fallback to the English value if available.

entities type

You can request a specific type of entity by setting the type parameter to either item (default), property, lexeme, form, or sense:

const url = wbk.searchEntities({ search: 'alphabet', type: 'property' })

cirrusSearchPages

associated Wikibase API doc: action=query&list=search

This alternative search mode allows to use the WikibaseCirrusSearch features, such as haswbstatement.

// Note that the search is case-insensitive
const url = wbk.cirrusSearchPages({ search: 'ingmar bergman' })

Due to the endpoint not returning much data other than the page title, a full example could look something like this:

const fetch = require('node-fetch')
// or just window.fetch, if you are working in the browser

const url = wbk.cirrusSearchPages({ search: 'Ingmar Bergman' })

fetch(url)
.then(res => res.json())
.then(wbk.parse.wb.pagesTitles)
.then(titles => {
  // If you where searching in an entity namespace, which is the default namespace on Wikibase instances,
  // those titles are either entities ids (ex: Q1) or prefixed entities ids (ex: Item:Q1)
  // In the first case, we can just do
  const ids = titles
  // In the second case, to get the ids, we need to drop the prefix
  const ids = titles.map(title => title.split(':')[1])
  // From there, to get the full entities data, you could do
  const entitiesUrl = wbk.getEntities({ ids })
  return fetch(entitiesUrl)
})
.then(res => res.json())
.then(entities => {
  // Yeah data!
})

haswbstatement

associated Wikibase API doc: haswbstatement

This feature requires that the Wikibase instance you are targetting has the WikibaseCirrusSearch extension installed: you can check that on /wiki/Special:Version

// Search for instance of (P31) filmographies (Q1371849) matching "Ingmar Bergman"
const url = wbk.cirrusSearchPages({ search: 'Ingmar Bergman', haswbstatement: 'P31=Q1371849' })
// Search for instance of (P31) filmographies (Q1371849)
const url = wbk.cirrusSearchPages({ haswbstatement: 'P31=Q1371849' })

// AND
// Search for instance of (P31) filmographies (Q1371849) AND that have a main subject (P921)
const url = wbk.cirrusSearchPages({ haswbstatement: [ 'P31=Q1371849', 'P921' ] })

// OR
// Search for instance of (P31) filmographies (Q1371849) OR that have a main subject (P921)
const url = wbk.cirrusSearchPages({ haswbstatement: 'P31=Q1371849|P921' })

// NOT
// Search for entities that are not instance of (P31) human (Q5)
const url = wbk.cirrusSearchPages({ haswbstatement: '-P31=Q5' })

// Qualifiers
// Search for entities that depict (P180) a cat (Q146) of color (P462) black (P462)
const url = wbk.cirrusSearchPages({ haswbstatement: 'P180=Q146[P462=Q23445]' })

This can also be used to lookup external ids:

const url = wbk.cirrusSearchPages({ haswbstatement: 'P227=4079154-3' })

but note that if your Wikibase instance offers a SPARQL endpoint, this can also be done with getReverseClaims

const url = wbk.getReverseClaims('P227', '4079154-3')

namespace

By default, the search returns is run only on the main namespace (0), but it is possible to customize that, and use cirrusSearchPages to search pages from any namespace

const url = wbk.cirrusSearchPages({
  search: 'Ingmar Bergman',
  // Only one namespace
  namespace: 2
  // Multiple namespaces
  namespace: [ 0, 1, 2, 3 ]
})

profile

Customise the search profile:

const url = wbk.cirrusSearchPages({
  search: 'Ingmar Bergman',
  profile: 'wikibase_config_entity_weight',
})

See srqiprofile for possible values

sort

Customise the search sort algorithm:

const url = wbk.cirrusSearchPages({
  search: 'Ingmar Bergman',
  sort: 'last_edit_asc',
})

See srsort for possible values

prop

Customise the prop parameter:

const url = wbk.cirrusSearchPages({
  search: 'Ingmar Bergman',
  prop: [ 'snippet', 'titlesnippet' ],
})

See srprop for possible values

other parameters

const url = wbk.cirrusSearchPages({
  search: 'Ingmar Bergman', // Becomes 'srsearch' in the url
  limit: 100, // Default: 10. Becomes 'srlimit' in the url
  offset: 500, // Default: 0. Becomes 'sroffset' in the url
  format: 'xml', // Default: json
})