You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何通过Wikidata API查找城市实体?含名称片段查询需求

Great question! Let's break down how to efficiently find city entities using the Wikidata API, especially for partial name searches like your "Lavoute" example. Wikidata's structured data lets you cut out extra API calls and directly target the entities you need—here are the best approaches:

1. Quick Partial Search with wbsearchentities API

Wikidata has a dedicated search endpoint (wbsearchentities) built for finding entities by name fragments, aliases, or labels. It’s perfect for your use case because it handles spelling variants (like missing accents) and lets you filter directly for city entities in one request.

Example Request

https://www.wikidata.org/w/api.php?action=wbsearchentities&search=Lavoute&language=fr&type=item&limit=5&format=json&entitytypes=item&props=info|sitelinks&includeAliases=1

Key Parameters Explained

  • search=Lavoute: Your partial name query (works with or without accents)
  • language=fr: Prioritizes French labels/aliases for more relevant results
  • type=item: Targets Wikidata items (entities) instead of properties
  • includeAliases=1: Searches through aliases too, not just official labels
  • props=info|sitelinks: Returns basic entity info + links to Wikipedia articles (like your original French Wikipedia target)
  • limit=5: Controls how many results to fetch (adjust as needed)

This will directly return the Wikidata item for Lavoûte-sur-Loire (and other matching cities) without needing a second API call to map Wikipedia pages to Wikidata items.

2. Advanced Filtering with SPARQL Queries

If you need more control—like restricting results to cities in France, or pulling extra metadata (e.g., region, population)—use Wikidata’s SPARQL endpoint. It lets you write structured queries to precisely target the entities you want.

Example SPARQL Query

SELECT ?item ?itemLabel ?wikipediaLink
WHERE {
  # Filter for cities (or subclasses of cities)
  ?item wdt:P31/wdt:P279* wd:Q515.
  # Restrict to cities in France
  ?item wdt:P17 wd:Q142.
  # Match partial name (case-insensitive, handles accents)
  ?item rdfs:label ?label.
  FILTER(CONTAINS(LCASE(?label), LCASE("lavoute"))).
  # Get French Wikipedia link
  OPTIONAL {
    ?wikipediaLink schema:about ?item;
                   schema:isPartOf <https://fr.wikipedia.org/>.
  }
  # Return labels in French
  SERVICE wikibase:label { bd:serviceParam wikibase:language "fr". }
}
LIMIT 10

Why This Works

  • wdt:P31/wdt:P279* wd:Q515: Ensures results are cities (or any subclass of city, like "communes in France")
  • wdt:P17 wd:Q142: Filters results to only French cities
  • CONTAINS(LCASE(...)): Makes the search case-insensitive and works with accent variations
  • The optional clause pulls the French Wikipedia link directly, so you don’t need extra requests

For even better fuzzy matching, replace the CONTAINS line with Wikidata’s built-in search index:

?item wikibase:search "lavoute"

This handles typos, spelling variants, and accent differences more reliably than manual string matching.

Optimal Workflow Summary

  • Simple partial searches: Use wbsearchentities for fast, direct results with minimal setup. It’s the closest drop-in replacement for your original Wikipedia API workflow but more efficient.
  • Complex filtering/metadata needs: Use SPARQL to combine name searches with geographic, category, or property filters, and fetch all required data in one go.

Both methods eliminate the need for multiple API calls—you get the city’s Wikidata entity (and associated data) directly from the first request.

内容的提问来源于stack exchange,提问作者William R

火山引擎 最新活动