Skip to content

Search

Search lives on the resource whose records it finds: client.entities.search() runs wbsearchentities, and the REST routes answer through client.items.search() and client.properties.search(). There is no client.search. The examples on this page use one client from the first block; later blocks reuse it.

import asyncio

from bibliofabric.wikidata import WikidataClient

client = WikidataClient()

Find entities by label

page = await client.entities.search(
    query="douglas adams", language="en", entity_type="item", page_size=3
)
for hit in page.results:
    print(hit.id, hit.label, "-", hit.description)
Q28421831 Douglas Adams - American environmental engineer
Q42 Douglas Adams - British science fiction writer and humorist (1952
Q117249850 Douglas Adams - Global Co-Head of Equity Capital Markets, Citi

The output above trims the Q42 description, which ends with the years Douglas Adams lived. language sets the language to search in (default en), entity_type picks item, property, or lexeme, and strict_language=True searches only in language. The result records are EntitySearchResult models with id, label, description, match, aliases, and concept_uri.

The module reports no total, so count() raises ConfigurationError, and Page.total is None. iterate() and collect() walk continue offset pages until the API returns an empty page, and first() returns the first hit or None:

count = 0
async for hit in client.entities.iterate(
    query="crispr", entity_type="item", page_size=5, limit=10
):
    count += 1
    print(hit.id, hit.label)
print("iterated:", count)
Q412563 CRISPR
Q17310682 CRISPR-Cas method
Q16965677 Cas9
iterated: 10

Fetch a searched id as a full entity through client.entities.get(), which also offers exists() and batch_get() on the same resource.

Search through the REST routes

The Wikibase REST API answers /search/items and /search/properties with a language-fallback display label. Each call returns one page, so page_size maps to the route's limit parameter, offset is the row index, and Page.total is None because the route reports no total:

items = await client.items.search("Douglas Adams", language="en", page_size=3)
for record in items.results:
    print("item:", record.id, record.display_label.value)

props = await client.properties.search("instance", language="en", page_size=3)
for record in props.results:
    print("prop:", record.id, record.display_label.value)
item: Q42 Douglas Adams
item: Q28421831 Douglas Adams
item: Q61853920 Douglas H Adams
prop: P31 instance of
prop: P279 subclass of
prop: P4224 category contains

Both return RestSearchResult records with id, display_label (a RestTerm, None when no language serves a label), description (also a RestTerm or None), and match.

Suggest entities and properties

The REST suggest routes answer the same result shape as the search routes and match aliases too:

items = await client.items.suggest("einstein", language="en", page_size=3)
print([(record.id, record.display_label.value) for record in items.results])

props = await client.properties.suggest("author", language="en", page_size=3)
print([(record.id, record.display_label.value) for record in props.results])
[('Q16834800', 'Einstein'), ('Q937', 'Albert Einstein'), ('Q901448', 'einstein')]
[('P50', 'author'), ('P676', 'lyricist'), ('P2093', 'author name string')]

Their query parameter is q; the alternative search= spelling answers 400 parameter-validation-failed, so the client sends q.