问题
How can I query Wikidata to get all items that have labels contain a word? I tried this but didn't work; it retrieved nothing.
SELECT ?item ?itemLabel WHERE {
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en".
?item rdfs:label ?itemLabel.
}
FILTER(CONTAINS(LCASE(?itemLabel), "keyword"))
}
LIMIT 1000
回答1:
Following your question and the useful comments provided, I ended up with this query
SELECT ?item ?itemLabel
WHERE {
?item rdfs:label ?itemLabel.
FILTER(CONTAINS(LCASE(?itemLabel), "city"@en)).
} limit 10
For which I got those results
item itemLabel
wd:Q515 city
wd:Q7930989 city
wd:Q15253706 city
wd:Q532039 The Eternal City
wd:Q1969820 The Eternal City
wd:Q3986838 The Eternal City
wd:Q7732543 The Eternal City
wd:Q7737016 The Golden City
wd:Q5119 capital city
wd:Q1555 Guatemala City
try it here
回答2:
Yes, you can search by label, for example:
SELECT distinct ?item ?itemLabel ?itemDescription WHERE{
?item ?label "Something"@en.
?article schema:about ?item .
?article schema:inLanguage "en" .
?article schema:isPartOf <https://en.wikipedia.org/>.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
see it on Query page.
回答3:
As of today (June 2020), the best way to do this seems to be using these CirrusSearch extensions. The following does a substring search in all English labels and comes back with 10,000 results in <20 seconds. I believe it also searches in aliases and descriptions.
SELECT DISTINCT ?item ?label
WHERE
{
SERVICE wikibase:mwapi
{
bd:serviceParam wikibase:endpoint "www.wikidata.org";
wikibase:api "Generator";
mwapi:generator "search";
mwapi:gsrsearch "inlabel:city"@en;
mwapi:gsrlimit "max".
?item wikibase:apiOutputItem mwapi:title.
}
?item rdfs:label ?label. FILTER( LANG(?label)="en" )
}
回答4:
As stated above, querying with case-insensitivity and truncation is very slow in SPARQL query service. I found this project on github: https://github.com/inventaire/entities-search-engine It sets up an ElasticSearch index which allows fast queries for use-cases like autocompletion.
来源:https://stackoverflow.com/questions/38527828/how-to-query-wikidata-items-using-its-labels