3

The goal is to get from the string "Lugano", the entity Q7024, knowing that I am searching for Swiss locations, by name in German.

So far this is how I do it but it doesn't look very efficient.

1) Get entities that match "Lugano" in German language

https://www.wikidata.org/w/api.php?action=wbsearchentities&search=Lugano&language=de&format=json

Parsing the result, I get the following entities: Q7024, Q660612, Q298642, Q34937366, Q661389, Q440182, Q686002 (in reality, I would actually need to continue the search because not all matches are returned in the same response, but for this example it suffices to get the values in the first page of results)

2) Run a query to filter for Swiss settlements among the collected entities in step 1

SELECT ?location ?locationLabel WHERE {
  ?location wdt:P17 wd:Q39.
  ?location wdt:P31 ?settlement .
  ?settlement wdt:P279 wd:Q486972
  FILTER (?location IN (wd:Q7024, wd:Q660612, wd:Q298642, wd:Q34937366, wd:Q661389, wd:Q440182, wd:Q686002))
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
  }

That returns the desired entity, Q7024.

Is there a better way to achieve that result, possibly in a single query?

3
SELECT ?location ?locationLabel WHERE {
  ?location wdt:P17 wd:Q39.
  ?location wdt:P31 ?settlement .
  ?settlement wdt:P279 wd:Q486972 .
  ?location rdfs:label ?de_label .
  FILTER (lang(?de_label) = "de") .
  FILTER (contains(?de_label, "Lugano")) .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
  }

Try it!


In some cases, you might be interested in the REGEX SPARQL function.

As for efficiency, see e. g. this question on Stack Overflow.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.