0

is it possible to search multiple term at the same time ? Using the OR operator, such as if I want to search Chicago OR New York City, in media wiki api at https://query.wikidata.org/

Below is example of search just Chicago, also how to search exact term ? I don't want "123 Chicago" to be returned, I just want "Chicago"

SELECT ?item ?itemLabel WHERE {
 SERVICE wikibase:mwapi {
   bd:serviceParam wikibase:endpoint "www.wikidata.org";
   wikibase:api "EntitySearch";
   mwapi:search "Chicago"; 
   mwapi:language "en".
   ?item wikibase:apiOutputItem mwapi:item.
 }
  
 SERVICE wikibase:label {bd:serviceParam wikibase:language "en".}
}
logi-kal
  • 7,107
  • 6
  • 31
  • 43
William
  • 5,526
  • 6
  • 20
  • 42
  • 1
    multiple terms: `SELECT ?item ?itemLabel WHERE { VALUES ?term {"Chicago" "Boston"} SERVICE wikibase:mwapi { bd:serviceParam wikibase:endpoint "www.wikidata.org"; wikibase:api "EntitySearch"; mwapi:search ?term; mwapi:language "en". ?item wikibase:apiOutputItem mwapi:item. } SERVICE wikibase:label {bd:serviceParam wikibase:language "en".} }` – UninformedUser Nov 12 '20 at 14:27
  • for exact match, isn't it better to just use standard SPARQL? `SELECT ?item ?itemLabel WHERE { VALUES ?term {"Chicago"@en "Boston"@en} ?item rdfs:label|skos:altLabel ?term SERVICE wikibase:label {bd:serviceParam wikibase:language "en".} }` – UninformedUser Nov 12 '20 at 14:28
  • @UninformedUser Thanks a lot, that multiple terms works well. For the exact match, searching the label directly is very slow in wikidata, according to their sparql optimization tips, they recommend using mwapi to be faster https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/query_optimization#Searching_labels – William Nov 12 '20 at 14:43
  • @UninformedUser I am trying to improve this this type of query that takes about 20 seconds `SELECT DISTINCT ?ent ?wdtProperty ?val ?valLabel WHERE { ?val wdt:P31|wdt:P106 [ rdfs:label|skos:altLabel 'journalist'@en ]. ?ent rdfs:label|skos:altLabel "Chicago"@en. ?wdProperty2 rdfs:label|skos:altLabel "place of birth"@en; wikibase:directClaim ?wdtProperty2. ?val ?wdtProperty2 ?ent . OPTIONAL { ?val rdfs:label ?valLabel FILTER(lang(?valLabel) = "en") } } LIMIT 10` – William Nov 12 '20 at 14:43
  • Why are you adding four levels of indirection? It's no wonder it won't work. There are hundreds of examples integrated into the query interface, so I really don't understand where you picked up that convolution scheme of yours. – Matthias Winkelmann Nov 13 '20 at 10:57
  • @MatthiasWinkelmann hi, did you mean it should have been done like this ? `SELECT DISTINCT ?val ?valLabel WHERE { ?val wdt:P106 wd:Q1930187. ?val wdt:P19 wd:Q1297 . OPTIONAL { ?val rdfs:label ?valLabel FILTER(lang(?valLabel) = "en") } } LIMIT 10` If so, this is not what I am trying to do, in my application, the Q ID or Property ID is unknown before hand. I need to lookup the ID by label. I know many examples in the interface but most of them are using direct Q ID, not searching by label. – William Nov 13 '20 at 11:44
  • The number of properties is quite limited. So those, at least, should be retrieved once and substituted locally. Items should be cached. You are using labels as identifiers, and that's just not what they are meant to be. – Matthias Winkelmann Nov 13 '20 at 15:09
  • @MatthiasWinkelmann for the properties, it can be replaced before running the sparql. But for the entity there are so many and many synonyms, how this can be better achieved then when the goal is to try convert natural language to sparql ? – William Nov 16 '20 at 12:08

0 Answers0