在 SharePoint Server 中規劃轉換查詢和順序結果Plan to transform queries and order results in SharePoint Server

摘要: 了解如何轉換使用者查詢以提供正確性更高的 SharePoint Server 搜尋結果,並改善搜尋結果的排序及顯示方式。Summary: Learn how you can transform queries to provide more targeted SharePoint Server search results and how you can influence the way search results are ordered and displayed.

您可以將查詢轉換新增至網頁組件、新增滿足某些條件時的查詢規則,以及將所有查詢直接轉換成結果來源,以建立特定的搜尋經驗。You can add query transforms to a Web Part, add query rules that transform queries when certain conditions are met, and you can transform all queries directed to a result source to create a specialized search experience.

SharePoint Server 包含大量預先設計好的搜尋經驗 (搜尋類別),如:「視訊」、「人員」和「對話」。這些全都包含預先定義的查詢轉換,以最佳化搜尋經驗。您也可以設計自己的搜尋經驗,包括您自己的查詢轉換,如:「音樂」或「圖片」。SharePoint Server contains a number of predesigned search experiences, or search verticals, such as "Videos", "People" and "Conversations". These all contain predefined query transforms to optimize the search experience. You can also design your own search experiences that include your own query transforms, for instance for "Music" or "Pictures".

了解查詢轉換和查詢變數Understanding query transforms and query variables

您可以設定查詢轉換,以取代查詢的特定內容,如:該查詢將用來取得搜尋結果的結果來源,或是該查詢顯示搜尋結果時的排序。You can configure a query transform to replace certain properties of a query, such as the result source that the query will use to get search results from, or the sort order that it will use when it displays search results.

查詢轉換可包含查詢變數。查詢變數為數值的預留位置,當確實執行查詢時,特定數值將會取代該查詢變數。A query transform can contain query variables. Query variables are placeholders for values, and when a query is actually run, the query variables are replaced with specific values.

下表為查詢變數的範例。The following table shows some examples of query variables.

查詢轉換取代此查詢變數:A query transform replaces this query variable: 使用這個:With this:
{User.Name}Username 輸入查詢的使用者名稱。The name of the user who typed the query.
{Site.URL}$siteurl 使用者輸入查詢的網站。The site where the user typed the query.
{Today}{Today} 今天的日期。Today's date.
{SearchBoxQuery}{SearchBoxQuery} 使用者輸入的查詢。The query that the user typed.
{searchTerms}{searchTerms} 使用者輸入的查詢已變更為最近一次的轉換。The query that the user typed, as changed by the most recent transform.

請參閱 SharePoint Server 中的查詢變數了解所有可用查詢變數的概觀。See Query variables in SharePoint Server for an overview of all the available query variables.

當查詢轉換取代傳入查詢時,將會使用「查詢範本」*。查詢範本是包含查詢變數的查詢,例如:"{searchTerms} contenttype:picture"。When a query transform replaces the incoming query, it uses a *query template. A query template is a query that includes query variables, for example "{searchTerms} contenttype:picture".

舉例來說,如果您想要建立一個只會在搜尋結果內傳回圖片的圖片搜尋類別,那麼您可以設定一個使用查詢範本 "{searchTerms} contenttype:picture" 的查詢轉換,以將 "contenttype:picture" 新增至所有查詢。如果使用者接著在「圖片」類別內輸入查詢 "moon",該轉換會將查詢變數 "{searchTerms}" 改為由 "moon" 取代,並且將該查詢變更為 "moon contenttype:picture"。If you, for example, want to create a Pictures search vertical that only returns pictures in the search results, you could configure a query transform that uses the query template "{searchTerms} contenttype:picture" to add "contenttype:picture" to all queries. If a user then types the query "moon" in the Pictures vertical, the transform replaces the query variable "{searchTerms}" with "moon" and changes the query to "moon contenttype:picture".

您可以在三個地方設定查詢轉換:You can configure query transforms in three places:

  • 網頁組件內In a Web Part

  • 查詢規則內In a query rule

  • 結果來源內In the result source

使用者查詢首先由網頁組件進行轉換,然後由任何適用的查詢規則轉換,最後由結果來源轉換。當您在結果來源內設定轉換時,由於結果來源最後才會轉換查詢,因此將不會捨棄或覆寫轉換變更。A user query is transformed first by the Web Part, then by any query rules that apply, and finally by the result source. When you configure a transform in a result source, you know that the transform changes will not be discarded or overridden, because the result source transforms the query last.

使用查詢產生器寫入及測試查詢轉換Using the Query Builder to write and test query transforms

查詢產生器將協助您寫入及測試查詢轉換。如要建置查詢,您可以使用「關鍵字查詢語言」(KQL),也可以新增查詢變數。您可以透過設定查詢變數的暫時測試值,使用查詢產生器測試查詢、執行查詢,並預覽搜尋結果。The Query Builder helps you write and test query transforms. To build queries you use Keyword Query Language (KQL), and you can also add query variables. You can test the query from within the Query Builder by setting temporary test values for the query variables, run the query and preview the search results.

如需有關建置搜尋查詢以及 KQL 語法範例的詳細資訊,請參閱<在 SharePoint 2013 中建立搜尋查詢 (MSDN)>。如需所有可用查詢變數的概觀,請參閱< Query variables in SharePoint Server>。For more information about building search queries and for KQL syntax examples, see Building search queries in SharePoint 2013 (MSDN). For an overview of all the available query variables, see Query variables in SharePoint Server.

針對網頁組件轉換查詢Transforming queries for a Web Part

您可以在網頁組件內轉換搜尋,例如:「內容搜尋網頁組件」和「搜尋結果網頁組件」。可透過查詢規則或結果來源的查詢轉換,覆寫網頁組件的查詢轉換。You can transform queries in search Web Parts, such as the Content Search Web Part and the Search Results Web Part. Query transforms on a Web Part can be overridden by a query rule or by a query transform on the result source.

網頁組件內的查詢轉換最常用於指定查詢應傳送至哪個結果來源。舉例來說,如果您要建立僅針對圖片搜尋的自訂搜尋經驗,首先要透過只傳回圖片的查詢轉換建立結果來源。接著,您要建立網頁組件,且該組件具備查詢轉換,可變更該網頁組件內任何執行的查詢,以使用您的新 圖片 結果來源,而非預設值。Query transforms in a Web Part are most often used to specify the result source that the queries should be sent to. For example, if you want to create a search experience that is customized for searching for pictures only, you would first create a result source with a query transform that returns only pictures. Then, you would create a Web Part that has a query transform that changes any query run in that Web Part to use your new Pictures result source instead of the default one.

在網頁組件內進行查詢轉換的另一個常用方法是變更特定的網頁組件。舉例來說,建立 圖片 結果來源之後,您可以透過使用 圖片 結果來源的查詢轉換新增網頁組件,另外,將搜尋結果限制為「只顯示最近修改過的圖片」。Another common use of query transforms in Web Parts is to make changes that are specific to one Web Part. For example, after creating the Pictures result source, you could add a Web Part with a query transform that uses the Pictures result source and in addition restrict the search results to only show recently modified pictures.

利用查詢規則轉換查詢Transforming queries with query rules

您可以使用查詢規則,嘗試了解使用者查詢的真正目的,並回應使用者想要尋找的項目。針對每個查詢規則,您可以指定在哪些情況下應該使用該規則,以及使用該規則時應觸發的動作。通常,您可建立適用於網站的查詢規則,不過,您也可以建立適用於網站集合或是在 Search Service 應用程式中所有網站集合的查詢規則。You use query rules to try to capture the real intent behind a user query, and to return results that better match that intent. For each query rule you can specify under which conditions the rule should be applied, and also which actions the rule should trigger when it is applied. Most often you create query rules that apply to one site, but you can also create query rules that apply to a site collection or to all site collections in a Search service application.

建立查詢規則的第一個步驟是指定規則的 內容 。最低需求是您必須指定查詢所使用的規則將對應至哪一個結果來源。舉例來說,如要建立一個僅適用於人員搜尋的規則,您必須指定該項內容為 本機人員結果 結果來源。您可以選擇性地在查詢規則內容中包含一個使用者區段或是主題類別。The first step in creating a query rule is to specify the context of the rule. The minimum requirement is that you specify which result source the query must target for the query rule to be applied. To create a rule that only applies to people search, for example, you would specify that the context is the result source Local People Results. Optionally, you can include a user segment or topic category in the context of a query rule.

下一步是指定將套用之規則的 條件 。如果您希望該項查詢可套用至所有查詢,可以移除所有條件。The next step is to specify the conditions that will cause the rule to be applied. If you want the query rule to apply to all queries, you can remove all conditions.

下表為可用查詢規則的條件。The following table shows the available query rule conditions.

查詢規則條件Query rule condition 描述Description 範例Example
查詢完全符合關鍵字Query matches keyword exactly 如果查詢完全符合指定的單字或片語,請套用該查詢規則。Apply the query rule when the query exactly matches a word or phrase that you specify. 例如:您指定 "picture; pic" 為關鍵字。當使用者在搜尋框中輸入查詢 "picture" 或 "pic" 時,將會套用該查詢規則。如果使用者輸入 "picture" 或 "sunny picture" 時,將不會套用該規則。You specify "picture; pic" as the keywords. The query rule will apply when users type the query "picture" or "pic" in a search box. The rule will not apply if a user types "pictures" or "sunny picture".
查詢包含動作字詞Query contains action term 當查詢包含單字或片語形式的字詞,表示使用者可能有特殊的查詢意圖,此時,請套用該查詢規則。該字詞必須位於查詢的開頭或結尾,且可能是動詞、命令或篩選。Apply the query rule when the query contains a term in the form of a single word or phrase that indicates that the user is trying to do something. The term must be at the beginning or end of the query and might be a verb, a command, or a filter. 如果查詢包含「下載」這個片語,表示使用者可能不是在尋找包含「下載」一詞的項目,而是試圖下載某些項目。If a query contains the phrase "download", the user is probably not looking for items that contain the word "download", but is probably trying to download something.
查詢完全符合字典Query matches dictionary exactly 當查詢完全符合字典項目,請套用該查詢規則。此項目可以是字詞庫中的一個字詞,或是人員名稱字典中的一個字詞。Apply the query rule when the query exactly matches a dictionary entry. This entry can be a term in the term store, or an entry in the people names dictionary.
查詢較常用於來源Query more common in source 對於不同結果來源,如果使用者的查詢在執行上比目前的查詢更為普遍,請套用該項查詢規則。此情況將針對使用者輸入的各種結果來源,使用查詢分析。Apply the query rule if the user's query is more commonly performed against a different result source than the current one. This condition uses an analysis of queries that users entered in the various result sources. 您可以建立查詢規則,檢查查詢在 視訊 類別內是否更普遍執行。如果是,該查詢規則將能使視訊的結果更為顯著。 You can create a query rule that checks if a query is more commonly performed in a Video vertical. It will make video results more prominent if it is.
使用者常點選的結果類型Result type commonly clicked 如果查詢常常在使用者點選特定結果類型的結果之下結束,請套用該查詢規則。當您建立新的結果類型時,可以指定記錄這些點選動作,以用於查詢規則。Apply the query rule if the query often ends in users clicking results of a particular result type. When you create a new result type, you can indicate that these clicks should be recorded to be used in query rules. 如果使用者常在這個查詢點選「圖片」結果類型,可在結果區塊中提供與圖片相關的結果較為恰當。If this is a query where people often click the result type "pictures", it may be appropriate to provide picture-related results in a result block.
進階查詢文字相符Advanced query text match 如果查詢符合規則運算式,請套用該查詢規則。您還可以使用不同的關鍵字、字典與動作字詞條件 (之前解釋過),不過,是使用更進階的控制。Apply the query rule if the query matches a regular expression. It also allows you to use variations of the keyword, dictionary and action term conditions explained earlier, but with more advanced control. 若要比對格式為 nnn-nnn-nnnn 的所有電話號碼,您可以指定規則運算式為 "(?(\d{3}))?-?(\d{3})-(\d{4})"。To match all phone numbers that are in the format nnn-nnn-nnnn, you specify the regular expression "(?(\d{3}))?-?(\d{3})-(\d{4})".

最後一個步驟是指定套用該查詢規則時所觸發的 動作 。您可以選擇性地指定查詢規則啟用的開始日期與結束日期。The final step is to specify which actions the query rule should trigger when it is applied. Optionally, you can specify the start and end date for a query rule to be active.

下表為可用的查詢規則動作。The following table shows the available query rule actions.

查詢規則動作Query rule action 描述Description 範例Example
新增升階的結果Add promoted results 在排名結果之上顯示升級的結果 (在 SharePoint Server 較舊版本中的別名為「首選」)。當某個項目並未建立索引,或是該項目的文件摘要較為劣等時,最好使用升級的結果。在其他情況下,請考慮變更結果排名。Show promoted results (known as Best Bets in earlier versions of SharePoint Server) above ranked results. Promoted results are best used when an item is not indexed or if it has a poor document summary. In other cases, consider changing the ranking of the results. 舉例來說,針對「病假」查詢,您可以將連結新增至高於所有排名結果的「人力資源」網站。For the query "sick leave", you can for instance add a link to a Human Resources site above all ranked results.
新增結果區塊Add result blocks 新增一個結果區塊,其中包含一個以特定方式與查詢相關聯的小組結果。您可以針對結果區塊進行升級,或者您可以針對其他搜尋結果進行排名。Add a block of results that contains a small subset of results that are related to a query in a specific way. You can promote a result block, or you can rank it with other search results.

針對結果區塊指定的查詢轉換,將會轉換原始查詢的複本。The query transform specified for the result block transforms a copy of the original query.

您還可以指定應使用哪個顯示範本以顯示結果區塊。You can also specify which display template should be used to display the result block.
針對含有「Contoso 銷售報告」的查詢,查詢規則可使用分類字典,將 "Contoso" 識別為客戶,然後顯示與 "Contoso" 有關的結果區塊,且是來自您的客戶關係管理 (CRM) 系統。For a query that contains "Contoso sales report", a query rule could use a taxonomy dictionary to recognize "Contoso" as a customer, and then display a result block with results about "Contoso" from your customer relationship management (CRM) system.
透過變更查詢來變更排名的結果Change ranked results by changing the query 新增變更原始查詢的查詢轉換。舉例來說,該轉換可針對某些結果進行升級或降級。 Add a query transform that changes the original query. For example, the transform can promote or demote certain results.

變更搜尋結果排名 (例如:透過網站或 URL 提升適當的結果) 是用來新增升級結果的常用替代方式。透過變更查詢變更排名結果,其優點是安全性調整與精簡。此外,如果文件無法再使用,則搜尋結果會消失。您可以根據一些變數 (如:副檔名或特定關鍵字),以動態的方式變更搜尋結果的排列順序。您可以針對結果進行升級或降級,也可以指定有多少結果應升級或降級。Changing the ranking of search results, such as boosting appropriate results by their site or URL, is a common alternative to adding promoted results. Changing ranked results by changing the query has the advantage that the results are security trimmed and refinable. Also, the search results will disappear if the document is no longer available. You can change the sorting order of the search results dynamically, based on several variables such as file extension or specific keywords. You can either promote or demote results, and you can specify how much the results should be promoted or demoted.

如需相關詳細資訊,請參閱<SharePoint Server 中的搜尋結果排名概觀>中的<以查詢規則影響搜尋結果的排名>章節。For more information, see the section Influence the ranking of search results with query rules in Overview of search result ranking in SharePoint Server.

針對包含「下載工具箱」的查詢,查詢規則可將「下載」一詞識別為動作字詞,並提高指向內部網路之特定下載網站的搜尋結果。For a query that contains "download toolbox", a query rule could recognize the word "download" as an action term and boost search results that point to a particular download site on your intranet.

在結果來源中轉換查詢Transforming queries in result sources

針對每個結果來源,您可以指定所有來自結果來源的搜尋結果必須以特定方式轉換。例如,預先設定的「本機視訊結果」結果來源會使用查詢轉換,只傳回從本機 SharePoint 索引的視訊結果。For each result source, you can specify that all search results from that result source should be transformed in a specific way. For example, the pre-configured "Local Video Results" result source uses a query transform to return only video results from the local SharePoint index.

SharePoint Server 透過預先定義的立即使用查詢轉換,提供許多預先設定的結果來源。您還可以建立新的結果來源,並且套用不同的查詢轉換。您可以針對每個搜尋提供者建立多個結果來源,也可以在每個結果來源設定不同的查詢轉換。SharePoint Server provides a number of preconfigured result sources with predefined query transforms out-of-the-box. You can also create new result sources and apply different query transforms on them. You can create more than one result source per search provider, and you can set different query transforms on each result source.

使用者查詢首先由網頁組件進行轉換,然後由任何適用的查詢規則轉換,最後由結果來源轉換。當您在結果來源內設定轉換時,由於結果來源最後才會轉換查詢,因此將不會捨棄或覆寫轉換變更。您可以重複使用網頁組件或結果區塊中的結果來源查詢轉換,也可以建立僅適用於來自某些結果來源的查詢規則或結果類型。A user query is transformed first by the Web Part, then by any query rules that apply, and finally by the result source. When you configure a transform in a result source, you know that the transform changes will not be discarded or overridden, because the result source transforms the query last. You can re-use a result source query transform in Web Parts or result blocks, and you can create query rules or result types that are only applied to results from certain result sources.

使用結果類型來變更結果的顯示方式Changing the way results are shown by using result types

透過結果類型,您可以有條件地變更搜尋結果的顯示數量。如要自訂一組相關結果的外觀,您可以建立顯示範本 (HTML),並讓顯示範本與結果類型產生關聯。您也可以建立規則以指定顯示範本的顯示時機,並設定這些規則的優先權。With result types, you can conditionally change how search results are displayed. To customize the appearance of a group of related results, you can create a display template in HTML and associate the display template with a result type. You can create rules to specify when to show the display template, and you can prioritize these rules.

搜尋系統處理查詢的方式How the search system processes a query

輸入查詢或按一下會觸發查詢的元素之後,搜尋系統就會將查詢傳送到查詢處理元件。此元件會處理查詢,然後將它傳送到適當的搜尋提供者以擷取結果。搜尋提供者可能是本機搜尋索引或遠端來源。從搜尋提供者收集結果之後,查詢處理元件就會執行另一次處理,然後傳回結果以供顯示。When someone enters a query or clicks on an element that triggers a query, the search system sends the query to the query processing component. This component processes the query and then sends it to the appropriate search providers to retrieve results. A search provider can be a local search index or a remote source. After the results are collected from the search providers, the query processing component performs additional processing and then returns the results so that they can be displayed.

搜尋系統會進行下列各項以處理查詢:The search system processes a query by doing the following:

  1. 套用任何網頁組件轉換 。Applying any Web Part transforms.

  2. 套用任何查詢規則。查詢規則動作可以轉換原始查詢,也可以觸發針對結果區塊轉換的平行查詢。Applying any query rules. A query rule action can either transform the original query or it can trigger a parallel query that is transformed for a result block.

  3. 在結果來源套用任何查詢轉換。Applying any query transforms on result sources.

  4. 剖析查詢並建立查詢語法樹以供內部使用。Parsing the query and creating a query syntax tree for internal use.

  5. 執行文字分行、詞幹分析、拼字校正及同義字擴充,以依照語言處理查詢。Processing the query linguistically by performing word breaking, stemming, spelling correction, and synonym expansion.

  6. 將使用者存取資訊附加到查詢。這樣可指定執行查詢的使用者,以及該使用者擁有的權限。Appending user-access information to the query. This specifies the user who is performing the query and the permissions that the user has.

  7. 將查詢傳送到搜尋索引或其他搜尋提供者。Sending the query to the search index or another search provider.

  8. 收集並合併來自所有搜尋提供者的搜尋結果,並傳送回查詢處理元件。Collecting and merging search results from all search providers and sending them back to the query processing component.

  9. 針對結果類型評估搜尋結果。如果結果符合特定的結果類型,則會使用針對結果類型所指定的顯示範本以顯示結果。Evaluating the search results against result types. If a result matches a particular result type, the result is displayed by using the display template that you have specified for the result type.

  10. 套用其他安全性修剪 (如果適用)。Applying additional security trimming, if appropriate.

另請參閱See also

在 SharePoint Server 中管理查詢規則Manage query rules in SharePoint Server

在 SharePoint Server 中設定搜尋的結果來源Configure result sources for search in SharePoint Server

在 SharePoint Server 中管理搜尋中心Manage the Search Center in SharePoint Server