舊文件

此處文件僅供參考,請自行考量時效性與適用程度,其他庫藏文件請參考文件頁面
我們亟需您的協助,進行共筆系統搬移、及文件整理工作,詳情請查閱參與我們

「讓搜尋模組支援搜尋建議」修訂間的差異

出自 MozTW Wiki

行 1: 行 1:
MozSearch supports search suggestions; as the user types in the search bar, [[Firefox 2]] queries the URL specified by the search engine plugin to fetch live search suggestions.
+
MozSearch 格式的搜尋模組可以支援搜尋建議,當使用者在搜尋欄中逐字鍵入關鍵詞時,Firefox 2 將依據搜尋模組內指定的 URL 送出查詢並取回結果。
  
Once the list has been retrieved, it's displayed in a popup box that appears under the search bar, which lets the user select a suggested search term.  If the user continues to type, a new set of suggestions is requested from the search engine, and the displayed list is refreshed.
+
取回結果之後,Firefox 將其顯示為一個下拉選單,讓使用者從中挑選搜尋引擎建議的關鍵詞。如果使用者繼續輸入關鍵詞,則會重新取回結果並更新選單。
  
The Yahoo and Google search plugins included with Firefox 2 support search suggestions.
+
Firefox 2 中內建的 Yahoo! 與 Google 搜尋模組都支援搜尋建議功能。
  
=Implementing suggestion support in the search plugin=
+
=搜尋模組的工作=
To support search suggestions, a search plugin needs to define an extra <code>&lt;Url&gt;</code> element with its <code>type</code> attribute set to <code>"application/x-suggestions+json"</code>.  (This means that a suggestion-supporting engine plugin will have two <code>&lt;Url&gt;</code> elements, the other one being the main <code>text/html</code> search URL.)
+
搜尋模組必須額外定義一個 <code>&lt;Url&gt;</code> 元素以便支援搜尋建議功能,此元素之 <code>type</code> 屬性需設定為 <code>"application/x-suggestions+json"</code>。支援搜尋建議功能的模組都會有兩個 <code>&lt;Url&gt;</code> 元素,另一個元素指定主要的 code>text/html</code> 搜尋 URL。
  
For example, the Yahoo search plugin has this <code>&lt;Url&gt;</code> entry:
+
舉例來說,Yahoo! 搜尋模組的 <code>&lt;Url&gt;</code> 長得像這樣:
  
 
<pre>
 
<pre>
行 14: 行 14:
 
</pre>
 
</pre>
  
If the user types "fir" into the search bar, then pauses, Firefox inserts "fir" in place of <code>{searchTerms}</code> and queries that URL:
+
如果使用者在搜尋欄中輸入「fir」,則 Firefox 會將此 URL 的 <code>{searchTerms}</code> 代換為「fir」後送出搜尋:
  
 
<pre>
 
<pre>
行 20: 行 20:
 
</pre>
 
</pre>
  
The results are used to construct the suggestion list box.
+
搜尋回來的結果將構成建議詞清單。
  
See [[Creating MozSearch plugins]] to learn more about how to implement a search plugin.
+
製作搜尋模組的方法可以參考[[製作 MozSearch 搜尋模組]]一文。
  
=Implementing search suggestion support on the server=
+
=伺服器端的工作=
The majority of the work in handling search suggestions is actually implemented on the server side.  If you're a web site designer, and want to support search suggestions, you need to implement support for returning the suggestions in [http://www.json.org/ JavaScript Object Notation] (JSON) given a search term.
+
搜尋建議功能的重點在伺服端的程式,如果你是網站設計師而想支援搜尋建議功能,則必須實做出依據搜尋字串給予進一步搜尋建議詞彙、並以 [http://www.json.org/ JSON] 格式傳回的功能。
  
When the browser wants to fetch possible matches for a search term, it then sends an HTTP GET request to the URL specified by the <code>&lt;Url&gt;</code> element.
+
當瀏覽器想取回搜尋字串時,會對 <code>&lt;Url&gt;</code> 元素指定的 URL 送出 HTTP GET 要求;此時伺服器則需依據字串決定相關字詞為何,並以 JSON 傳回 2 到 4 個建議字詞,如下:
  
Your server should then decide upon the suggestions to offer using whatever means it sees fit, and construct a JSON that consists of at least two, and as many as four, elements:
+
; '''查詢字串'''
 +
: JSON 的第一個元素是原始查詢字串,這樣 Firefox 可以得知傳回值是否與目前查閱的字詞相符。
  
; '''query string'''
+
; '''建議字詞列表'''
: The first element in the JSON is the original query string.  This allows Firefox to verify that the suggestions match the current search term.
+
: 建議字詞的陣列,此陣列需以「[」及「]」括起,例如 <tt>["建議詞 1", "建議詞 2", "建議詞 3", "建議詞 4"]</tt>
  
; '''completion list'''
+
; '''說明'''
: An array of suggested search terms.  The array should be enclosed in square brackets.  For example: <tt>["term 1", "term 2", "term 3", "term 4"]</tt>
+
: 此元素為選用,亦為一個陣列,分別針對每個「建議字詞列表」的建議詞設定說明,其中可以是任何搜尋引擎想提供瀏覽器顯示的資訊,例如此建議詞有多少搜尋結果等等。
  
; '''descriptions'''
+
<div class="note">Firefox 2 尚不支援「說明」功能,會忽略此傳回值。</div>
: This optional element is an array of descriptions for each of the suggestions in the ''completion list''.  These can be any additional information the search engine might want to return to be displayed by the browser, such as the number of results available for that search.
 
  
<div class="note">Descriptions are not supported in Firefox 2, and are ignored if any are specified.</div>
+
; '''查詢網址'''
 +
: 此元素為選用,還是一個陣列,內容是各建議詞的替代網址。打個比方:如果除搜尋結果頁面之外還想直接提供建議詞的地圖連結,那就可以將地圖的網址列在此陣列裡。
  
; '''query URLs'''
+
: 若沒有額外指定此元素,則將依據搜尋模組 XML 中的 <code>&lt;Url&gt;</code> 元素決定查詢網址。
: This optional element is an array of alternate URLs for each of the suggestions in the ''completion list''.  For example, if you want to offer a map link instead of just a search result page for a given suggestion, you can return an URL to a map in this array.
 
  
: If you don't specify a query URL, the default query is used based on the search described by the <code>&lt;Url&gt;</code> element in the search plugin's XML description.
+
<div class="note">Firefox 2 尚不支援「查詢網址」功能,會忽略此傳回值。</div>
  
<div class="note">Query URLs are not supported in Firefox 2, and are ignored.</div>
+
也就是說,若搜尋詞為「fir」且不傳回說明及查詢網址,則傳回的 JSON 應該是長這樣:
 
 
For example, if the search term is "fir", and you don't need to return descriptions or alternate query URLs, you might return the following JSON:
 
  
 
  ["fir", ["firefox", "first choice", "mozilla firefox"]]
 
  ["fir", ["firefox", "first choice", "mozilla firefox"]]
  
Note that in this example, only the query string and completion array are specified, leaving out the optional elements.
+
在此範例中只涵括查詢字串及建議字詞列表兩元素,其他選用的都不列入。
  
Your completion list can include as many suggestions as you like, although it should be kept manageable, given that the display will be updating live while the user is typing their search string.  In addition, the method you use to select suggestions is entirely up to you.
+
雖然建議的字詞想放多少就可以放多少,但由於每次使用者多打一個字都會查詢一次,所以還是有所限度比較穩當。至於怎麼依據使用者的要求提出建議,就全交給你自己決定了。

於 2006年11月23日 (四) 00:51 的修訂

MozSearch 格式的搜尋模組可以支援搜尋建議,當使用者在搜尋欄中逐字鍵入關鍵詞時,Firefox 2 將依據搜尋模組內指定的 URL 送出查詢並取回結果。

取回結果之後,Firefox 將其顯示為一個下拉選單,讓使用者從中挑選搜尋引擎建議的關鍵詞。如果使用者繼續輸入關鍵詞,則會重新取回結果並更新選單。

Firefox 2 中內建的 Yahoo! 與 Google 搜尋模組都支援搜尋建議功能。

搜尋模組的工作

搜尋模組必須額外定義一個 <Url> 元素以便支援搜尋建議功能,此元素之 type 屬性需設定為 "application/x-suggestions+json"。支援搜尋建議功能的模組都會有兩個 <Url> 元素,另一個元素指定主要的 code>text/html 搜尋 URL。

舉例來說,Yahoo! 搜尋模組的 <Url> 長得像這樣:

<Url type="application/x-suggestions+json" template="http://ff.search.yahoo.com/gossip?output=fxjson&command={searchTerms}"/>

如果使用者在搜尋欄中輸入「fir」,則 Firefox 會將此 URL 的 {searchTerms} 代換為「fir」後送出搜尋:

<Url type="application/x-suggestions+json" template="http://ff.search.yahoo.com/gossip?output=fxjson&command=fir"/>

搜尋回來的結果將構成建議詞清單。

製作搜尋模組的方法可以參考製作 MozSearch 搜尋模組一文。

伺服器端的工作

搜尋建議功能的重點在伺服端的程式,如果你是網站設計師而想支援搜尋建議功能,則必須實做出依據搜尋字串給予進一步搜尋建議詞彙、並以 JSON 格式傳回的功能。

當瀏覽器想取回搜尋字串時,會對 <Url> 元素指定的 URL 送出 HTTP GET 要求;此時伺服器則需依據字串決定相關字詞為何,並以 JSON 傳回 2 到 4 個建議字詞,如下:

查詢字串
JSON 的第一個元素是原始查詢字串,這樣 Firefox 可以得知傳回值是否與目前查閱的字詞相符。
建議字詞列表
建議字詞的陣列,此陣列需以「[」及「]」括起,例如 ["建議詞 1", "建議詞 2", "建議詞 3", "建議詞 4"]
說明
此元素為選用,亦為一個陣列,分別針對每個「建議字詞列表」的建議詞設定說明,其中可以是任何搜尋引擎想提供瀏覽器顯示的資訊,例如此建議詞有多少搜尋結果等等。
Firefox 2 尚不支援「說明」功能,會忽略此傳回值。
查詢網址
此元素為選用,還是一個陣列,內容是各建議詞的替代網址。打個比方:如果除搜尋結果頁面之外還想直接提供建議詞的地圖連結,那就可以將地圖的網址列在此陣列裡。
若沒有額外指定此元素,則將依據搜尋模組 XML 中的 <Url> 元素決定查詢網址。
Firefox 2 尚不支援「查詢網址」功能,會忽略此傳回值。

也就是說,若搜尋詞為「fir」且不傳回說明及查詢網址,則傳回的 JSON 應該是長這樣:

["fir", ["firefox", "first choice", "mozilla firefox"]]

在此範例中只涵括查詢字串及建議字詞列表兩元素,其他選用的都不列入。

雖然建議的字詞想放多少就可以放多少,但由於每次使用者多打一個字都會查詢一次,所以還是有所限度比較穩當。至於怎麼依據使用者的要求提出建議,就全交給你自己決定了。

個人工具