階段 9: 設定內容搜尋網頁組件的 SharePoint Server 中的類別頁面上的查詢Stage 9: Configure the query in a Content Search Web Part on a category page in SharePoint Server

摘要:了解如何在 SharePoint Server 2016 類別] 頁面上內容搜尋網頁組件中設定的查詢。Summary: Learn how to configure the query in a Content Search Web Part on a category page in SharePoint Server 2016.

注意

大部分的這一系列中所述的功能是僅適用於 SharePoint Online 中的私人每個網站集合。Most of the features described in this series are available only for private sites collections in SharePoint Online.

快速概觀 (英文)Quick Overview

下列清楚的這一系列的上一個階段:Previous stages of this series explained the following:

當我們瀏覽至 「 音訊 」 類別時,我們無法看見任何內容。這是因為我們建立 [類別] 頁面上,我們沒有加入任何網頁組件。When we browsed to the "Audio" category, we couldn't see any content. That was because when we created the category page, we didn't add any Web Parts.

在此階段中,我們將啟動合併的我們已在前一個階段中完成部分。在本文中,您將了解:In this stage, we'll start to merge pieces of what we have done in previous stages. In this article, you'll learn:

啟動階段 9Start stage 9

在我們的 Contoso 網站上顯示的內容,我們將使用 「 內容搜尋網頁組件。To display content on our Contoso website, we'll use the Content Search Web Part.

關於內容搜尋網頁組件About the Content Search Web Part

顧名思義,內容搜尋網頁組件 (CSWP) 會搜尋技術。The Content Search Web Part (CSWP) uses, as its name implies, search technology.

充分運用我們每天使用搜尋技術。關於您輸入的查詢一天上bing.com,例如 [搜尋] 方塊中搜尋文字的次數思考如何您按下Enter之後, 您掃描幾乎立即顯示在搜尋結果頁面的搜尋結果。Most of us use search technology daily. Think about how many times a day that you enter query terms in a search box, for example on bing.com, how after you press Enter, you scan search results that are almost immediately displayed on a search results page.

當訪客瀏覽至包含 CSWP] 頁面上時,它們可能不知道,但他們實際上發出查詢。不過,CSWPs 不同的事情而不是在搜尋方塊中輸入查詢字詞,查詢包含在網頁組件本身。這表示的訪客瀏覽至包含 CSWP 頁面、 時便會自動發出此查詢。When visitors browse to a page that contains a CSWP, they're probably not aware of this, but they're actually issuing a query. But, the thing that differs with CSWPs is that instead of entering query terms in a search box, the query is contained within the Web Part itself. This means that when a visitor browses to a page that contains a CSWP, this query is issued automatically.

不同於 bing.com 搜尋分析藍本的另一項重點是的搜尋結果不顯示個別搜尋結果頁面上,但內 CSWP。在大多數情況下,訪客即使不知道搜尋技術會被用來顯示他們正在檢視的內容。給他們,它會行為與任何其他網頁一樣。Another thing that differs from the bing.com search scenario is that search results aren't displayed on a separate search results page, but within the CSWP. In most cases, visitors won't even know that search technology is being used to display the content they're viewing. To them, it will behave like any other webpage.

如何新增內容搜尋網頁組件至頁面How to add a Content Search Web Part to a page

瀏覽至您要新增的 CSWP。在我們的案例中,我們瀏覽至 「 音訊 」。Browse to the page where you want to add the CSWP. In our scenario, let's browse to "Audio."

  1. 按一下 [設定] 功能表及 [編輯頁面]Click the Settings menu, and then click Edit Page.

  2. 在您要新增網頁組件網頁組件區域,按一下 [新增網頁組件In the Web Part Zone where you want to add the Web Part, click Add a Web Part.

  3. 在 [類別] 清單中,按一下 [內容彙總In the Categories list, click Content Rollup.

  4. 在 [組件] 清單中,按一下 [內容搜尋] 和 [新增In the Parts list, click Content Search, and then click Add.

在我們的案例中,我們將新增至區域 3 的 CSWP。In our scenario, we'll add a CSWP to Zone 3.

已新增 CSWP

CSWP 包含預設查詢。因此,它已經顯示某些內容 (音訊、 照相機和電腦)。但不會顯示我們想要顯示的內容。若要讓網頁組件中顯示 Contoso 目錄內容,我們必須設定網頁組件中的查詢。The CSWP contains a default query. Therefore, it already displays some content (Audio, Cameras and Computers). But it does not display the content we want to display. To make the Web Part display Contoso catalog content, we must configure the query in the Web Part.

如何設定內容搜尋網頁組件的類別頁面上的查詢How to configure a query in a Content Search Web Part on a category page

  1. 在 [網頁組件中按一下 [網頁組件功能表] 和 [編輯網頁組件In the Web Part, click the Web Part Menu, and then click Edit Web Part.

    Edit WP

  2. 在 [網頁組件] 工具窗格中,按一下 [變更查詢]。這會開啟一個對話方塊。In the Web Part tool pane, click Change query. This opens a dialog box.

    變更查詢

在對話方塊中,會發現 「 音訊 」 會顯示在左上角相對。這是我們導覽] 及 [從我們選取編輯頁面的類別。也會發現RelevantResults的區段、 前三個結果、音訊照相機、 及電腦] 的列。這些是相同的三個結果我們新增時所顯示的網頁組件。In the dialog box, notice that "Audio" is shown in the upper-left corner. This is the category we navigated to, and from which we selected to edit the page. Also notice that in the RelevantResults section, the top three results, Audio , Cameras , and Computers , are listed. These are the same three results that were shown in the Web Part when we added it.

 ![Default Query](../media/OTCSP_DefautQuery.png)
  1. [選取查詢] 清單中,選取您的目錄結果來源。在我們的案例中,它是型錄-產品結果From the Select a query list, select your catalog result source. In our scenario, it's catalog - Products Results .

    查詢結果來源

結果來源會縮減可以從中擷取搜尋結果的範圍。在階段 5: 將您的發佈網站連線至 SharePoint Server 中目錄、 我們我們目錄的連線我們我們目錄的發佈網站、 SharePoint Server 2016 自動建立結果來源時。在我們的案例中,這種結果來源是名為目錄-產品結果。選取此結果來源,擷取只從我們目錄搜尋結果。A result source narrows the scope from which search results can be retrieved. In Stage 5: Connect your publishing site to a catalog in SharePoint Server, when we connected our publishing site to our catalog, SharePoint Server 2016 automatically created a result source for our catalog. In our scenario, this result source is named catalog - Products Results . By selecting this result source, only search results from our catalog are retrieved.

For more information about result sources, see [Plan result sources and query rules](plan-search-for-sharepoint-cross-site-publishing-sites.md#BKMK_PlanResultSourcesAndQueryRules).

When we selected this result source, the number of **RelevantResults** changed from 864 to 775. Because 775 is the number of items we have in our catalog, selecting this result source confirms that we're on the right way to configuring the query. 
  1. 依標籤來限制] 區段中選取 [ Restrict 由目前和子導覽字詞In the Restrict by tag section, select Restrict by current and child navigation terms.

    查詢標記

此選取範圍中的關鍵片語是導覽字詞。這是指訪客會瀏覽網站瀏覽窗格中的類別。在此特定的情況下訪客會瀏覽 「 音訊 」 類別。A key phrase in this selection is navigation terms . This refers to the category in the site navigation the visitor is browsing. In this particular case, the visitor is browsing the "Audio" category.

 ![Audio URL](../media/OTCSP_AudioURL.png)

請記住,我們進行了此系列中的第一個事項其中已匯入到清單的目錄內容。我們也會匯入字詞的字詞組產品階層。在階段 2: 清單內容匯入至 SharePoint Server 中的產品目錄網站集合,我們與字詞組中相關聯的清單中的每個項目。在階段 5: 將您的發佈網站連線至 SharePoint Server 中目錄,我們指定完整的網站導覽應該包含產品階層的字詞組中的條款。因為我們已使用相同的字詞組來標記我們目錄中的項目及建立我們網站導覽,我們可以使用從我們網站導覽字詞搜尋以該相同的字詞的目錄項目。Remember, one of the first things we did in this series was import catalog content into a list. We also imported terms into the term set Product Hierarchy. In Stage 2: Import list content into the Product Catalog Site Collection in SharePoint Server, we associated each item in the list with a term from the term set. In Stage 5: Connect your publishing site to a catalog in SharePoint Server, we specified that the full site navigation should contain terms from the Product Hierarchy term set. Because we have used the same term set to tag the items in our catalog and to build our site navigation, we can use a term from our site navigation to search for catalog items that are tagged with that same term.

Our query in the CSWP will therefore display search results for items that are in the  *catalog - Products Results result source*  , and are tagged with either "Audio", or any child of "Audio", for example "MP3 players" or "Speakers". 

This selection reduced the relevant search results to 114, which is the number of items in our catalog that belong to the "Audio" group.

Another key phrase from the selection **Restrict by current and child navigation** terms is  *current*  . More information about the importance of this phrase is provided in [About the query configuration](stage-9-configure-the-query-in-a-content-search-web-part-on-a-category-page.md#BKMK_AboutTheQueryConfiguration) in the next section 
  1. 按一下 [確定] 並儲存] 頁面。Click OK, and save the page.

    在 「 音訊 」 類別中,搜尋結果已變更要顯示不同的結果。On the "Audio" category, the search results have changed to show different results.

    音訊結果

如果我們瀏覽至 「 相機 」 類別,我們會看到三個其他顯示的搜尋結果。If we browse to the "Cameras" category, we'll see three other search results displayed.

 ![Cameras Results](../media/OTCSP_CamerasResults.png)

如果我們瀏覽至 「 MP3"類別,我們會看到其他三個不同的搜尋結果的顯示。If we browse to the "MP3" category, we'll see three other different search results are displayed.

 ![MP3 Results](../media/OTCSP_MP3Results.png)

如果您現在考慮"現在,我了解如何取得正確的搜尋結果的 「 音訊 」 類別,因為這是類別我們點擊和已變更之網頁組件中的查詢。為什麼要選擇不要我們看到不同,但是當我們瀏覽該目錄的搜尋結果?與不應該我們也變更所有其他類別的查詢吗? 」If you are now thinking "OK, I understand how we got the correct search results for the "Audio" category, because that is the category we clicked, and where we changed the query in the Web Part. But why do we see different search results when we browse the catalog? And shouldn't we change the query for all the other categories also?"

我們看接近項目將要上。Let's take a closer look at what's going on.

查詢設定的相關About the query configuration

我們只需要設定一個查詢因為同一頁上使用的所有類別。請記住,在階段 8: 將類別頁面和目錄項目頁面指派給 SharePoint Server 中某個字詞、 我們指派給網站的導覽字詞組中的所有字詞的頁面ContosoCategoryPage.aspx時。我們會將此頁面給所有字詞。因此,即使我們編輯此頁面 「 音訊 」 類別中的,我們可以編輯它在任何其他類別中,並達成相同的結果。We only had to configure one query because the same page is used for all categories. Remember, in Stage 8: Assign a category page and a catalog item page to a term in SharePoint Server, when we assigned the page ContosoCategoryPage.aspx to all terms within the Site Navigation term set. We assigned this page to all terms . Therefore, even though we edited this page in the "Audio" category, we could have edited it in any other category, and achieved the same result.

我們僅有設定的查詢一次因為發行的網頁組件的查詢不同哪一個類別根據我們瀏覽]。請記住 CSWP 包含某人瀏覽至包含 CSWP 頁面時自動發出的查詢和搜尋結果的顯示在網頁組件。此外,請記得我們當我們設定查詢網頁組件中已選取目前和子導覽字詞來限制。"Current"word 是非常重要以下,因為它表示 CSWP 所發出的查詢會變更類別根據訪客是目前瀏覽。如果您編輯網頁組件從另一個類別,您可以看到 [網頁組件已變更。We only had to configure the query one time, because the query issued from the Web Part differs depending on which category we browse to. Remember that the CSWP contains a query that is automatically issued when someone browses to a page that contains a CSWP, and that search results are displayed in the Web Part. Also, remember that we selected Restrict by current and child navigation terms when we configured the query in the Web Part. The word "current" is very important here, because it means that the query issued by the CSWP will change depending on the category the visitor is currently browsing. If you edit the Web Part from another category, you can see that the Web Part has changed.

例如,如果我們瀏覽至 「 相機 」 類別並仔細接近 CSWP,我們看到:For example, if we browse to the "Cameras" category and take a closer look at the CSWP, we see that:

  • 在 URL 中包含"照相機"。"cameras" is included in the URL.

  • 右上角的查詢設定為 「 相機 」。"Cameras" is in the upper-right corner of the query configuration.

  • RelevantResults數目已變更為 118,這是在資料庫目錄中對 「 相機 」 群組的項目數。The number of RelevantResults has changed to 118, which is the number of items in the catalog that belong to the "Cameras" group.

相機查詢

如此,當我們瀏覽至 「 音訊 」 類別,CSWP 發出查詢時的目錄項目與"音訊 」 或 「 音訊 」、 任何子,並顯示搜尋結果。當我們瀏覽至 「 相機 」] 類別中,相同的 CSWP (請記住,我們只用於一頁的所有類別) 發出不同查詢時,這次是針對 「 相機 」 或 「 相機 」,因此不同的結果的任何子標記的項目顯示的目錄。So, when we browse to the "Audio" category, the CSWP issues a query for catalog items that are tagged with "Audio" or any child of "Audio", and displays search results. When we browse to the "Cameras" category, the same CSWP (remember, we only used one page for all categories) issues a different query, this time for catalog items that are tagged with "Cameras" or any child of "Cameras", therefore different results are displayed.

若要檢視的查詢設定的詳細資訊的方式How to view details of the query configuration

若要檢視的查詢設定的詳細資訊,請按一下 [測試] 索引標籤。CSWP 所發出的實際查詢會顯示在 [查詢文字] 欄位。To view details of the query configuration, click on the TEST tab. The actual query issued by the CSWP, is shown in the Query text field.

測試 2

在我們的案例中,從 「 音訊 」 類別的 CSWP 發出的查詢看起來如下:In our scenario, the query that is issued by the CSWP from the "Audio" category looks as follows:

(contentclass:sts_listitem OR IsDocument:True) SPSiteUrl:http://contoso/sites/catalog ListId:3a3f66cd-9741-4f15-b53a-b4b23c3187ea owstaxidProductCatalogItemCategory:#c771504f-6a2f-423f-98de-0e12fcfa08c9

如果這並沒有任何意義任何現在,不用擔心 !邏輯,且我們將會自動換行它向下進行較清晰。If this doesn't make any sense now, don't worry! There is logic to it, and we'll break it down to make it clearer.

  • (contentclass:sts_listitem OR IsDocument:True) SPSiteUrl:http://contoso/sites/catalog ListId:3a3f66cd-9741-4f15-b53a-b4b23c3187ea是我們目錄結果來源目錄-產品結果(contentclass:sts_listitem OR IsDocument:True) SPSiteUrl:http://contoso/sites/catalog ListId:3a3f66cd-9741-4f15-b53a-b4b23c3187ea is our catalog result source, catalog - Products Results

  • owstaxidProductCatalogItemCategory是 managed 的屬性的網站欄項目分類(記住從階段 2: 清單內容匯入至 SharePoint Server 中的產品目錄網站集合我們產品階層的字詞組如何繫結至受管理的中繼資料網站欄項目類別)。owstaxidProductCatalogItemCategoryis the managed property for the site column Item Category (remember from Stage 2: Import list content into the Product Catalog Site Collection in SharePoint Server how our Product Hierarchy term set is tied to the Managed Metadata site column Item Category ).

  • #c771504f-6a2f-423f-98de-0e12fcfa08c9會在目前導覽中,在此例中 「 音訊。 」 字詞的 GUID#c771504f-6a2f-423f-98de-0e12fcfa08c9is the GUID of the term in the current navigation, in this case "Audio."

我們產品目錄網站集合中的產品階層字詞組,您可以看到 GUID 代表音訊的字詞。In our Product catalog site collection, in the Product Hierarchy term set, you can see that the GUID represents the term Audio.

GUID 音訊字詞

現在我們已針對 CSWP 設定查詢我們類別頁面上。我們仍須執行一些設定使其顯示三個以上的搜尋結果,並也可以將其授與 「 Contoso 看起來。 」這會清楚稍後的這一系列。So now we have configured the query for the CSWP on our category page. We still have to do some configuration to make it display more than three search results, and also give it a "Contoso look." This will be explained later in this series.

下一步是將 CSWP 新增至我們目錄項目頁面The next step is to add a CSWP to our catalog item page

本系列中的下一篇文章Next article in this series

階段 10: 設定內容搜尋網頁組件的 SharePoint Server 中的目錄項目頁面上的查詢Stage 10: Configure the query in a Content Search Web Part on a catalog item page in SharePoint Server

另請參閱See also

概念Concepts

在 SharePoint Server 中設定搜尋網頁組件Configure Search Web Parts in SharePoint Server