0

I am trying to form an efficient filter query in SPARQL on Wikidata. Let me explain my process:

  1. I query the search-entities API using key words e.g. (Apple, Orange)
  2. The API query returns a list of relevant item ID's e.g. (wd:Q629269, wd:Q154950, wd:Q312, wd:Q95, wd:Q4878289, wd:Q10817602)
  3. With this list of ID's, I then query SPARQL and to return items that are CLASS or are SUBLCASS of certain types e.g. (p:P31/ps:P31/wdt:P279* wd:Q43229) - which returns everything if it is an Organisation or subclass thereof.
  4. Then for items in the list of ID's, that are of certain CLASS, return information items if they exists e.g. (OPTIONAL).

I am new to SPARQL. My Question is, is this the most efficient method to achieve this output? It seems to me to be quite inefficient and I cannot find a similar type of problem in the tutorial examples.

You can try the query here.

    SELECT distinct ?item ?itemLabel ?itemDescription ?web ?inception ?ISIN
    WHERE{
      FILTER (?item IN (wd:Q629269, wd:Q154950, wd:Q312, wd:Q95, wd:Q4878289, wd:Q10817602))
      ?item p:P31/ps:P31/wdt:P279* wd:Q43229.
      OPTIONAL {
        ?item wdt:P856 ?web. # get item-web
        ?item wdt:P571 ?inception. # get item-web
        ?item wdt:P946 ?ISIN. # get item-isin
            }
      SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
        }
LIMIT 10
BenP
  • 825
  • 1
  • 10
  • 30
  • 1
    `VALUES` instead of `FILTER`: https://w.wiki/425 – Stanislav Kralin May 16 '19 at 09:37
  • Awesome. I know it may be teaching to suck eggs, but could you provide that as an answer and provide some explanation? I will then accept. – BenP May 16 '19 at 11:46
  • 2
    you can also directly call the search API inside the SPARQL query: `VALUES ?searchTerm { "apple" "orange" } SERVICE wikibase:mwapi { bd:serviceParam wikibase:api "EntitySearch". bd:serviceParam wikibase:endpoint "www.wikidata.org". bd:serviceParam wikibase:limit 3 . bd:serviceParam mwapi:search ?searchTerm. bd:serviceParam mwapi:language "en". ?item wikibase:apiOutputItem mwapi:item. ?num wikibase:apiOrdinal true. }` – UninformedUser May 16 '19 at 12:40

0 Answers0