API:Opensearch

From MediaWiki.org
Jump to navigation Jump to search
This page is a translated version of the page API:Opensearch and the translation is 94% complete.

Other languages:
English • ‎dansk • ‎français • ‎polski • ‎日本語 • ‎한국어

GET request to search the wiki and obtain results in an OpenSearch format.

このAPIの使用対象はウェブブラウザ開発に限定されます。 使用により標準的で適用しやすい結果を抽出し、ネット受けまとめ検索で使えるようになります。

APIの説明文書


action=opensearch

(main | opensearch)

Search the wiki using the OpenSearch protocol.

Parameters:
search

Search string.

This parameter is required.
namespace

Namespaces to search. Ignored if search begins with a valid namespace prefix.

Values (separate with | or alternative): 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 90, 91, 92, 93, 100, 101, 102, 103, 104, 105, 106, 107, 486, 487, 828, 829, 1198, 1199, 2300, 2301, 2302, 2303, 2600, 5500, 5501
To specify all values, use *.
Default: 0
limit

Maximum number of results to return.

No more than 500 (5,000 for bots) allowed.
Type: integer or max
Default: 10
profile

Search profile to use.

strict
Strict profile with few punctuation characters removed but diacritics and stress marks are kept.
normal
Few punctuation characters, some diacritics and stopwords removed.
normal-subphrases
Few punctuation characters, some diacritics and stopwords removed. It will match also subphrases (can be subphrases or subpages depending on internal wiki configuration).
fuzzy
Similar to normal with typo correction (two typos supported).
fast-fuzzy
Experimental fuzzy profile (may be removed at any time)
fuzzy-subphrases
Similar to normal with typo correction (two typos supported). It will match also subphrases (can be subphrases or subpages depending on internal wiki configuration).
classic
Classic prefix, few punctuation characters and some diacritics removed.
engine_autoselect
Let the search engine decide on the best profile to use.
One of the following values: strict, normal, normal-subphrases, fuzzy, fast-fuzzy, fuzzy-subphrases, classic, engine_autoselect
Default: engine_autoselect
suggest

Do nothing if $wgEnableOpenSearchSuggest is false.

Type: boolean (details)
redirects

How to handle redirects:

return
Return the redirect itself.
resolve
Return the target page. May return fewer than limit results.

For historical reasons, the default is "return" for format=json and "resolve" for other formats.

One of the following values: return, resolve
format

The format of the output.

One of the following values: json, jsonfm, xml, xmlfm
Default: json
warningsaserror

If warnings are raised with format=json, return an API error instead of ignoring them.

Type: boolean (details)
Example:
Find pages beginning with Te.
api.php?action=opensearch&search=Te [open in sandbox]

GET リクエスト

レスポンス

[
    "Hampi",
    [
        "Hampi",
        "Hampi (town)",
        "Hampi Express",
        ...
    ],
    [
        "Hampi, also referred to as the Group of Monuments at Hampi, is a UNESCO World Heritage Site located in east-central Karnataka, India.",
        "Hampi  is a town in Hospet taluk of the Ballari district in the Indian state of Karnataka. Located along the Tungabhadra River in the east and center part of the state, near the border of Andhra Pradesh, Hampi is near the city of Hosapete.",
        "The Hampi Express is a daily express train running between the Mysooru and Hubballi Junction, the headquarters of the South Western Railway in India.",
        ...
    ],
    [
        "https://en.wikipedia.org/wiki/Hampi",
        "https://en.wikipedia.org/wiki/Hampi_(town)",
        "https://en.wikipedia.org/wiki/Hampi_Express",
        ...
    ]
]

サンプル コード

opensearch.py

#!/usr/bin/python3

"""
    opensearch.py

    MediaWiki Action API Code Samples
    Demo of `Opensearch` module: Search the wiki and obtain
    results in an OpenSearch (http://www.opensearch.org) format
    MIT license
"""

import requests

S = requests.Session()

URL = "https://en.wikipedia.org/w/api.php"

SEARCHTERM = "Hampi"

PARAMS = {
    'action':"opensearch",
    'search':SEARCHTERM,
    'limit': 5,
    'namespace':0,
    'format':"json"
}

R = S.get(url=URL, params=PARAMS)
DATA = R.json()

print(DATA)

起こりうるエラー

コード 情報
nosearch パラメーター search を設定してください。
unknown_format Unrecognized value for parameter format: aaa.

追加的な注記

このAPI関連でMediaWiki サイト管理者拡張機能の開発者向けのその他のヒント:

  • Extension:TitleKey - このAPIの示す検索結果一覧で大文字小文字を区別。
  • Extension:TextExtracts $wgExtractsExtendOpenSearchXmlLocalSettings.phpで true にするとそれぞれのItem項目をXML形式で示し、記事から抽出したテキストと<Description>タグ付けします。
  • LocalSettings.phpExtension:PageImages $wgPageImagesExpandOpenSearchXmlを true するとそれぞれの項目をXML形式で表示、記事から抽出した<Image>のサムネイル画像にタグ付け。

関連項目