维基数据:SPARQL查询服务/查询助手


.
查询助手允许您创建或修改现有的查询而无需了解SPARQL。
使用此工具时,它可基于您使用可视化工具输入的内容修改所需的SPARQL查询语句,修改语句也会反之改变可视化内容。
有什么限制?
这是一个实验性的工具,并且也许会弄坏你的SPARQL查询。
- JSON序列化不应包含新行(换行)。
- 模板可以放在查询中的任何位置:例如顶部或底部。
- 模板的内容将添加到查询的URL中。也许这会导致一些问题。
怎样创建一个查询?
本章节展示如何使用查询助手从头创建一个查询。
此例中我们将用查询留下所有动物园中的北极熊和狮子。
You can use identifiers Pxxx or Qxxx directly in the interface input fields to access the items correctly.
Add 'instance of zoo', 'species kept polar bear' and 'species kept lion' filter
- Click on 'Filter'
- Click on the combo box and enter 'zoo'
- Select the 'zoo (Q43501)' item from the combo box
- Click on 'Filter' and select 'polar bear (Q33609)'
- Click on 'Filter' and select 'lion (Q140)'
Add 'official website', 'image', 'coordinate location', 'inception' and 'species kept' columns
- Click on 'Show' and select 'official website (P856)'
- Click on 'Show' and select 'image (P18)'
- Click on 'Show' and select 'coordinate location (P625)'
- Click on 'Show' and select 'inception (P571)'
- Click on 'Show' and select 'species kept (P1990)'
Check your source |
---|
SELECT ?zoo ?official_website ?image ?coordinate_location ?inception ?species_kept ?species_keptLabel WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
?zoo wdt:P31 wd:Q43501;
wdt:P1990 wd:Q33609, wd:Q140.
OPTIONAL { ?zoo wdt:P856 ?official_website. }
OPTIONAL { ?zoo wdt:P18 ?image. }
OPTIONAL { ?zoo wdt:P625 ?coordinate_location. }
OPTIONAL { ?zoo wdt:P571 ?inception. }
OPTIONAL { ?zoo wdt:P1990 ?species_kept. }
}
LIMIT 200
|
如何修改一个查询?

This section will show how you can edit a query with the Query Helper
Change 'cat' to 'zoo'
- Select the 'Cats Query' from the examples
- Click on 'cat' to modify the value
- Click on the combo box and enter 'zoo'
- Select the 'zoo (Q43501)' item from the combo box
The query will now find items that have 'instance of zoo' instead of 'instance of cat'
添加“国家:德国”过滤器
- Click on 'Filter' button
- Enter 'Germany'
- Select 'Germany (Q183)'
- Click on 'instance of' from 'instance of Germany' to modify the value
- Click on the select box and enter 'Country'
- Select country (P17) item
The query will now only find items that have 'instance of zoo' and 'country Germany'
Add 'inception' and 'image' columns
- Click on 'Show' button
- Enter 'inception'
- Select 'inception (P571)' item
- Click on 'Show' button
- Enter 'image'
- Select 'image (P18)'
The query result now has two additional columns 'image' and 'inception' and can be displayed as a Timeline or Image Grid.
Add a title
In the code view, in the right-hand pane, add #title: Your title
at the beginning of the query; changing "Your title" to whatever you would like the title to be.
查询模板
使用查询模板

Queries that have a Query Template defined will show the defined textual representation of this query inside of the Query Helper.Query Helper allows you to create or modify an existing query without knowing SPARQL.
When working with the tool it will modify the SPARQL query, based on the input you provide in the visual interface, and vice versa.
It is displayed on the left side of the query editor.
Blue items in this textual representation can be clicked to modify them.
Defined suggestions will be shown or a text can be entered to search for a replacement.
When selecting one of the items in the list it will replace the item in the textual representation and the SPARQL query.
You can find all the queries in the Query Examples List that have a defined Query Template by searching for '#TEMPLATE' in the page text.
Create a Query Template
Query templates can be created by providing a template definition. Here is an example query with a template definition.
The definition is expressed in JSON and inserted as SPARQL comment to the query like this:
#TEMPLATE=[JSON_DEFINITION]
JSON definition example:
{
"template": "Largest ?c with ?sex head of government",
"variables": {
"?sex": {},
"?c": {
"query": "SELECT DISTINCT ?id WHERE { ?c wdt:P31 ?id. ?c p:P6 ?mayor. }"
}
}
}
鍵 | 值 | 示例 | 描述 | SPARQL查询 |
---|---|---|---|---|
模板 | 这个文本将作为查询的描述显示给用户。
在文本中,您可以定义变量,这些变量将被实际值取代。 |
Largest ?c with ?sex head of government | This will define two variables within the text: ?c and ?sex The values for this variables must be defined in the SPARQL query. |
BIND(wd:Q6581072 AS ?sex)
BIND(wd:Q515 AS ?c)
|
In this section you have to define the variables from the template. You can optionally define a SPARQL query that is used for suggesting values for that variable. |
"?sex": {},
"?c": {
"query": "[SPARQL]"
}
|
This will define two variables: ?c and ?sex For the variable ?c there is a SPARQL query defined that will suggest Items. |
SELECT DISTINCT ?id WHERE
{ ?c wdt:P31 ?id.?c p:P6 ?mayor. }
SELECT ?id ?label ?description WITH {
[QUERY]
} AS %inner
WHERE {
INCLUDE %inner
?id rdfs:label ?label.
?id schema:description ?description.
FILTER((LANG(?label)) = "en")
FILTER((LANG(?description)) = "en") }
ORDER BY DESC(?count)
LIMIT 20
|