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

From Wikidata
Jump to navigation Jump to search
This page is a translated version of the page Wikidata:SPARQL query service/Query Helper and the translation is 43% complete.
Other languages:
English • ‎dansk • ‎français • ‎polski • ‎svenska • ‎русский • ‎العربية • ‎中文 • ‎日本語

查询助手允许您创建或修改现有的查询而无需了解SPARQL。

使用此工具时,它可基于您使用可视化工具输入的内容修改所需的SPARQL查询语句,修改语句也会反之改变可视化内容。

它会显示在查询编辑器的左侧。

有什么限制?

这是一个实验性的工具,并且也许会弄坏你的SPARQL查询

  • JSON序列化不应包含新行(换行)。
  • 模板可以放在查询中的任何位置:例如顶部或底部。
  • 模板的内容将添加到查询的URL中。也许这会导致一些问题。

怎样创建一个查询?

创建一个在动物园中的北极熊和狮子的查询

本章节展示如何使用查询助手从头创建一个查询。

此例中我们将用查询留下所有动物园中的北极熊和狮子。

Add 'instance of zoo', 'species kept polar bear' and 'species kept lion' filter

  1. Click on 'Filter'
  2. Click on the combo box and enter 'zoo'
  3. Select the 'zoo (Q43501)' item from the combo box
  4. Click on 'Filter' and select 'polar bear (Q33609)'
  5. Click on 'Filter' and select 'lion (Q140)'

Add 'official website', 'image', 'coordinate location', 'inception' and 'species kept' columns

  1. Click on 'Show' and select 'official website (P856)'
  2. Click on 'Show' and select 'image (P18)'
  3. Click on 'Show' and select 'coordinate location (P625)'
  4. Click on 'Show' and select 'inception (P571)'
  5. Click on 'Show' and select 'species kept (P1990)'

如何修改一个查询?

Query Helper - Edit 'Cat Query'

This section will show how you can edit a query with the Query Helper

Change 'cat' to 'zoo'

  1. Select the 'Cat Query' from the examples
  2. Click on 'cat' to modify the value
  3. Click on the combo box and enter 'zoo'
  4. 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'

添加“国家:德国”过滤器

  1. Click on 'Filter' button
  2. Enter 'Germany'
  3. Select 'Germany (Q183)'
  4. Click on 'instance of' from 'instance of Germany' to modify the value
  5. Click on the select box and enter 'Country'
  6. Select country (P17) item

The query will now only find items that have 'instance of zoo' and 'country Germany'

Add 'inception' and 'image' columns

  1. Click on 'Show' button
  2. Enter 'inception'
  3. Select 'inception (P571)' item
  4. Click on 'Show' button
  5. Enter 'image'
  6. Select 'image (P18)'

The query result now has two additional columns 'image' and 'inception' and can be displayed as a Timeline or Image Grid.

查询模板

使用查询模板

查询助手 - 查询模板 女性担任政府首脑的最大的城市

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. }
The SPARQL query from the Query Template will be embedded into this query to display the result with label, description. If ?count is provided then it will be sorted by desc(?count):
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